fork download
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. typedef struct dadoNo {
  6.  
  7. char nome[20];
  8. char endereco[20]; //
  9. int idade; // Estes parâmetros são para funções exigidas pelo professor.
  10. int indice; // As outras funções estão funcionando perfeitamente e só interessam à estes parâmetros
  11.  
  12. } DadoNo;
  13.  
  14. typedef struct no{
  15.  
  16. struct no* prox;
  17. struct no* ant;
  18. int pos;
  19. DadoNo dado;
  20.  
  21. } No;
  22.  
  23. typedef struct list {
  24.  
  25. int size;
  26. No* head;
  27.  
  28. } Lista;
  29.  
  30. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  31.  
  32. int listVazia (Lista* list){
  33.  
  34. return(list->size == 0);
  35. }
  36.  
  37. Lista* criarlist (){
  38.  
  39. Lista* list = (Lista*)malloc(sizeof(Lista));
  40. list->size = 0;
  41. list->head = NULL;
  42. return list;
  43. }
  44.  
  45. No* Busca ( Lista *list, char *nome){
  46.  
  47. No* ptr = list->head;
  48.  
  49. while (ptr != NULL){
  50.  
  51. if (strcmp( nome, ptr->dado.nome) == 0){
  52.  
  53. return ptr;
  54. }
  55.  
  56. ptr = ptr->prox;
  57. }
  58.  
  59. return NULL;
  60. }
  61.  
  62. int Soma (char nome[20]){
  63.  
  64. int soma = 0, i = 0;
  65.  
  66. for (i ; i < (int)strlen(nome) ; i++ ){
  67.  
  68. soma += (nome[i]-96);
  69. }
  70.  
  71. return soma;
  72. }
  73.  
  74. void Insere (Lista* list){
  75.  
  76. int i, p = (list->size);
  77. DadoNo dado;
  78. No* n = (No*) malloc(sizeof(No));
  79. n->ant = list->head;
  80.  
  81. scanf("%s", dado.nome);
  82.  
  83. if (Busca(list, dado.nome) == NULL){
  84.  
  85. scanf("%s", dado.endereco);
  86. scanf("%d", &dado.idade);
  87.  
  88. dado.indice = Soma(dado.nome)%11;
  89.  
  90. n->dado = dado;
  91. n->prox = list->head;
  92. list->head = n;
  93. list->size++;
  94.  
  95. n->pos = p;
  96. }
  97.  
  98. else{
  99.  
  100. printf("ESTE NOME JA EXISTE !\n");
  101. }
  102. }
  103.  
  104. void OrdenaNome(Lista *list){
  105.  
  106. int j = (list->size) ;
  107.  
  108. No* aux = (No*)malloc(sizeof(No));
  109. No* ptr = (No*)malloc(sizeof(No));
  110. No* aux2 = (No*)malloc(sizeof(No));
  111.  
  112. ptr = (list->head);
  113.  
  114. while (ptr != NULL){
  115.  
  116. aux = ptr->prox;
  117.  
  118. while( aux != NULL){
  119.  
  120. if (ptr->dado.nome[0] > aux->dado.nome[0]){
  121.  
  122. aux2->dado = ptr->dado;
  123. ptr->dado = aux->dado;
  124. aux->dado = aux2->dado;
  125. }
  126.  
  127. aux = aux->prox;
  128.  
  129. }
  130.  
  131. ptr = ptr->prox;
  132.  
  133. }
  134. }
  135.  
  136. ////////////////////////////////////////////////////////////////////////////////////////////////////
  137.  
  138. void InsereList(Lista* list, int Num_pessoas){
  139.  
  140. //printf("i \n");
  141.  
  142. int i;
  143.  
  144. for (i = 0; i < Num_pessoas ; i++){
  145.  
  146. Insere(list);
  147. }
  148. }
  149.  
  150. void RemoverDados ( Lista *list){
  151.  
  152. //printf("r \n");
  153. char nome[20];
  154. int i = 0;
  155. printf("DIGITE O NOME DA PESSOA QUE DESEJA REMOVER : \n");
  156. scanf("%s", nome);
  157.  
  158. No* atual = Busca(list, nome);
  159. No* aux = (No*)malloc(sizeof(No));
  160. No* aux1 = (No*)malloc(sizeof(No));
  161.  
  162. if (!listVazia(list)){
  163.  
  164. if (atual != NULL){
  165.  
  166. if( atual == list->head){ // Não tem funcionado para nó de pos = 0
  167.  
  168. list->head = atual->ant;
  169. }
  170.  
  171. else { // não remove o determinado nó
  172.  
  173. atual->prox->ant = atual->ant;
  174. atual->ant->prox = atual->prox;
  175. }
  176.  
  177. free(atual);
  178. list->size--;
  179. }
  180.  
  181. else{
  182.  
  183. printf("ESTE NOME NAO PERTENCE A LISTA .\n");
  184. printf("\n");
  185. }
  186. }
  187. }
  188.  
  189.  
  190. void ConsultarDados (Lista *list){
  191.  
  192. //printf("c \n");
  193. char nome[20];
  194.  
  195. printf("DIGITE O NOME A SER BUSCADO :\n");
  196. printf("\n");
  197.  
  198. scanf("%s", nome);
  199.  
  200. No* atual = Busca(list, nome);
  201.  
  202. if( atual != NULL){
  203.  
  204. printf("%s \n", atual->dado.nome);
  205. printf("%d \n", atual->dado.idade);
  206. printf("%s \n", atual->dado.endereco);
  207. }
  208.  
  209. else{
  210.  
  211. printf("ESTE NOME NAO PERTENCE A LISTA .\n");
  212. printf("\n");
  213. }
  214. }
  215.  
  216. void ListarPorIndice (Lista *list, int pos){
  217.  
  218. //printf("T \n");
  219.  
  220. if ( pos > 0){
  221.  
  222. int j, i ;
  223.  
  224. No* aux = (No*)malloc(sizeof(No));
  225. No* ptr = (No*)malloc(sizeof(No));
  226. No* aux2 = (No*)malloc(sizeof(No));
  227.  
  228. ptr = (list->head);
  229.  
  230. while (ptr != NULL){
  231.  
  232. aux = ptr->prox;
  233.  
  234. while( aux != NULL){
  235.  
  236. if (ptr->dado.indice > aux->dado.indice){
  237.  
  238. aux2->dado = ptr->dado;
  239. ptr->dado = aux->dado;
  240. aux->dado = aux2->dado;
  241. }
  242.  
  243. aux = aux->prox;
  244. }
  245.  
  246. ptr = ptr->prox;
  247. }
  248.  
  249. ptr = list->head;
  250.  
  251. for (i = 0; i < pos; i++){
  252.  
  253. printf("%s \n", ptr->dado.nome);
  254. ptr = ptr->prox;
  255. }
  256. }
  257. }
  258.  
  259. void ListarNomesCrescente (Lista *list){
  260.  
  261. //printf("o \n");
  262.  
  263. if (!listVazia(list)){
  264.  
  265. OrdenaNome(list);
  266.  
  267. No* ptr = list->head;
  268.  
  269. while(ptr != NULL){
  270.  
  271. printf("%s \n", ptr->dado.nome );
  272.  
  273. ptr = ptr->prox;
  274. }
  275. }
  276. }
  277.  
  278. //////////////////////////////////////////////////////////////////////////////////////////////////
  279.  
  280. int main (){
  281.  
  282. Lista* l = criarlist();
  283.  
  284. InsereList(l,3);
  285. printf("\n");
  286. RemoverDados(l);
  287.  
  288. //ListarPorIndice(l,3);
  289. //ConsultarDados(l);
  290. //ListarNomesCrescente(l);
  291.  
  292. No* ptr = l->head;
  293.  
  294. while( ptr != NULL){ // Somente para percorrer a lista e imprimir o que resta nela.
  295.  
  296. printf("NOME : %s \n", ptr->dado.nome);
  297. //printf("IDADE : %d \n", ptr->dado.idade);
  298. //printf("ENDERECO : %s \n", ptr->dado.endereco);
  299. //printf("POSICAO : %d \n", ptr->pos);
  300. //printf("INDICE : %d\n", ptr->dado.indice);
  301.  
  302. ptr = ptr->prox;
  303. printf("\n");
  304. }
  305.  
  306. return 0;
  307. }
Success #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
ESTE NOME JA EXISTE !
ESTE NOME JA EXISTE !

DIGITE O NOME DA PESSOA QUE DESEJA REMOVER : 
ESTE NOME NAO PERTENCE A LISTA .

NOME :