Advertisement
mdlib

Funções Principais de Listas Simplesmente Encadeadas

Sep 20th, 2019
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int COD=1;
  6.  
  7. typedef struct estrutura
  8. {
  9.     int cod;
  10.     char nome[20];
  11.     int idade;
  12.     struct estrutura *prox;
  13. }estrutura;
  14.  
  15. estrutura *alocaNo(char *nome, int idade)
  16. {
  17.     estrutura *no=(estrutura*)malloc(sizeof(estrutura));
  18.  
  19.     no->cod=COD;
  20.     COD++;
  21.  
  22.     strcpy(no->nome, nome);
  23.     no->idade=idade;
  24.  
  25.     return no;
  26. }
  27.  
  28. void insereInicio(estrutura **lista, char *nome, int idade)
  29. {
  30.     estrutura *no=alocaNo(nome, idade);
  31.  
  32.     if((*lista)==NULL){
  33.         (*lista)=no;
  34.         no->prox=NULL;
  35.         return;
  36.     }
  37.    
  38.     no->prox=(*lista);
  39.     (*lista)=no;
  40.  
  41.     return;
  42.  
  43. }
  44.  
  45. void insereFim(estrutura **lista, char *nome, int idade)
  46. {
  47.  
  48.     estrutura *no=alocaNo(nome, idade);
  49.  
  50.     no->prox=NULL;
  51.  
  52.     if((*lista)==NULL)
  53.     {
  54.         (*lista)=no;
  55.         return;
  56.     }
  57.  
  58.     estrutura *aux=(*lista);
  59.  
  60.     while(aux->prox!=NULL)
  61.     {
  62.         aux=aux->prox;
  63.     }
  64.  
  65.     aux->prox=no;
  66.  
  67.     return;
  68. }
  69.  
  70. void removeInicio(estrutura **lista)
  71. {
  72.     estrutura *aux=(*lista);
  73.  
  74.     (*lista)=aux->prox;
  75.     free(aux);
  76.  
  77.     return;
  78. }
  79.  
  80. void removeFim(estrutura **lista)
  81. {
  82.     estrutura *aux=(*lista);
  83.  
  84.     while(aux->prox->prox!=NULL)
  85.     {
  86.         aux=aux->prox;
  87.     }
  88.  
  89.     free(aux->prox);
  90.     aux->prox=NULL;
  91.  
  92.     return;
  93. }
  94.  
  95. void removeNomeIdade(estrutura **lista, char *nome, int idade)
  96. {
  97.     estrutura *aux=(*lista);
  98.     estrutura *aux2=(*lista);
  99.  
  100.     if(strcmp(aux->nome, nome)==0 && aux->idade==idade)
  101.     {
  102.         (*lista)=aux->prox;
  103.         free(aux);
  104.         return;
  105.     }
  106.  
  107.     aux=aux->prox;
  108.  
  109.     while(aux!=NULL)
  110.     {
  111.         if(strcmp(aux->nome, nome)==0 && aux->idade==idade)
  112.         {
  113.             aux2->prox=aux->prox;
  114.             free(aux);
  115.             return;
  116.         }
  117.         aux2=aux;
  118.         aux=aux->prox;
  119.     }
  120.    
  121.     return;
  122. }
  123.  
  124. estrutura *buscaNome(estrutura **lista, char *nome)
  125. {
  126.     estrutura *aux=(*lista);
  127.  
  128.     while(aux!=NULL && strcmp(aux->nome, nome)!=0)
  129.     {
  130.         aux=aux->prox;
  131.     }
  132.  
  133.     if(aux==NULL)
  134.     {
  135.         return NULL;
  136.     }
  137.  
  138.     return aux;
  139. }
  140.  
  141. void imprimeLista(estrutura *lista)
  142. {
  143.     estrutura *aux=lista;
  144.  
  145.     while(aux!=NULL)
  146.     {
  147.         printf("  Nome: %s\n  Idade: %d\n\n", aux->nome, aux->idade);
  148.         aux=aux->prox;
  149.     }
  150. }
  151.  
  152. int main(){
  153.     estrutura *lista=NULL;
  154.     int esc=1;
  155.  
  156.     while(esc!=0)
  157.     {
  158.         printf("-------------------------------------------------------------------------------------\n");
  159.         printf("           Menu:\n\n  1- Inserir Inicio\n  2- Inserir Fim"
  160.             "\n  3- Remover Inicio\n  4- Remover Fim\n  5- Remover Busca"
  161.             "\n  6- Buscar pelo Nome\n  7- Imprime Lista\n\n  Digite a sua escolha: ");
  162.         scanf("%d", &esc);
  163.  
  164.         switch(esc)
  165.         {
  166.             case 1:
  167.             {
  168.                 char nome[20];
  169.                 int idade;
  170.  
  171.                 printf("\n  Digite o nome: ");
  172.                 scanf("%s", nome);
  173.  
  174.                 printf("\n  Digite a idade: ");
  175.                 scanf("%d", &idade);
  176.  
  177.                 insereInicio(&lista, nome, idade);
  178.  
  179.                 printf("\n  Inserido com sucesso!\n\n");
  180.  
  181.                 break;
  182.             }
  183.  
  184.             case 2:
  185.             {
  186.                 char nome[20];
  187.                 int idade;
  188.  
  189.                 printf("\n  Digite o nome: ");
  190.                 scanf("%s", nome);
  191.  
  192.                 printf("\n  Digite a idade: ");
  193.                 scanf("%d", &idade);
  194.  
  195.                 insereFim(&lista, nome, idade);
  196.                 printf("\n  Inserido com sucesso!\n\n");
  197.  
  198.                 break;
  199.             }
  200.  
  201.             case 3:
  202.             {
  203.                 removeInicio(&lista);
  204.                 printf("\n  Removido com sucesso!\n\n");
  205.  
  206.                 break;
  207.             }
  208.  
  209.             case 4:
  210.             {
  211.                 removeFim(&lista);
  212.                 printf("\n  Removido com sucesso!\n\n");
  213.  
  214.                 break;
  215.             }
  216.  
  217.             case 5:
  218.             {
  219.                 char nome[20];
  220.                 int idade;
  221.  
  222.                 printf("\n  Digite o nome: ");
  223.                 scanf("%s", nome);
  224.  
  225.                 printf("\n  Digite a idade: ");
  226.                 scanf("%d", &idade);
  227.  
  228.                 removeNomeIdade(&lista, nome, idade);
  229.  
  230.                 printf("\n  Removido com sucesso!\n\n");
  231.  
  232.                 break;
  233.             }
  234.  
  235.             case 6:
  236.             {
  237.                 char nome[20];
  238.  
  239.                 estrutura *no;
  240.                 printf("\n  Digite o nome: ");
  241.                 scanf("%s", nome);
  242.  
  243.                 no=buscaNome(&lista, nome);
  244.                 printf("\n  Nome: %s\n  Idade: %d\n\n", no->nome, no->idade);
  245.  
  246.                 break;
  247.             }
  248.  
  249.             case 7:
  250.             {
  251.                 printf("\n");
  252.                 imprimeLista(lista);
  253.                 break;
  254.             }
  255.         }
  256.     }
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement