Guest User

Untitled

a guest
Feb 21st, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.52 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct bloco
  5. {
  6.    int num;
  7.    struct bloco *prox;
  8. };
  9.  
  10. struct bloco *aux, *aux2, *fim, *inicio;
  11.  
  12. void iniciar_lista(void);
  13. void inserir(void);
  14. void retirar(void);
  15. void pesquisar(int);
  16. void listar(void);
  17.  
  18. void desalocar_lista(void);
  19.  
  20. int main()
  21. {
  22.    int opcao = -1;
  23.    iniciar_lista();
  24.    while(opcao != 4)
  25.    {
  26.       printf("\nO que quer fazer?\n");
  27.       printf("\n1. Inserir elemento na lista.\n");
  28.       printf("\n2. Retirar elemento da lista.\n");
  29.       printf("\n3. Ver a lista.\n");
  30.       printf("\n4. Sair\n");
  31.       printf("\nOpção desejada: ");
  32.       scanf("%d", &opcao);
  33.  
  34.       switch(opcao)
  35.       {
  36.          case 1: inserir(); break;
  37.          case 2: retirar(); break;
  38.          case 3: listar(); break;
  39.          case 4: desalocar_lista(); return 0; break;
  40.          default: printf("\nOpção inválida.\n");
  41.       }
  42.    }
  43. }
  44.  
  45. void iniciar_lista(void)
  46. {
  47.    inicio = (struct bloco *)malloc(sizeof(struct bloco));
  48.    inicio->num = -1;
  49.    inicio->prox = NULL;
  50. }
  51.  
  52. void inserir(void)
  53. {
  54.    int numero;
  55.    aux = (struct bloco *)malloc(sizeof(struct bloco));
  56.    printf("\nQue número quer inserir?\n");
  57.    scanf("%d", &numero);
  58.    aux->num = numero;
  59.    aux->prox = NULL;
  60.    if(inicio->prox == NULL)
  61.    {
  62.       inicio->prox = aux;
  63.       fim = aux;
  64.    }
  65.    else
  66.    {
  67.       fim->prox = aux;
  68.       fim = aux;
  69.    }
  70. }
  71.  
  72. void retirar(void)
  73. {
  74.    int numero;
  75.    if(inicio->prox != NULL)
  76.    {
  77.       printf("\nQue número quer retirar?\n");
  78.       scanf("%d", &numero);
  79.       pesquisar(numero);
  80.       if(aux == NULL)
  81.          printf("\nNúmero não encontrado.\n");
  82.       else
  83.       {
  84.          aux2->prox = aux->prox;
  85.  
  86.          if(aux == fim) fim = aux2;
  87.          free(aux);
  88.       }
  89.    }
  90.    else
  91.       printf("\nLista vazia.\n");
  92. }
  93.  
  94. void pesquisar(int x)
  95. {
  96.  
  97.    aux2 = inicio;
  98.    aux = inicio->prox;
  99.    while(aux->num != x)
  100.    {
  101.       if(aux == fim) break;
  102.  
  103.       aux = aux->prox;
  104.  
  105.       aux2 = aux2->prox;
  106.    }
  107.    if(aux->num != x)
  108.       aux = NULL;
  109. }
  110.  
  111. void listar(void)
  112. {
  113.    aux = inicio->prox;
  114.  
  115.    if(aux != NULL)
  116.  
  117.    {
  118.       while(aux != NULL)
  119.       {
  120.          printf("%d -> ", aux->num);
  121.          aux = aux->prox;
  122.       }
  123.  
  124.       printf("NULL\n\n");
  125.  
  126.    }
  127.  
  128.    else
  129.  
  130.       printf("\nLista vazia.\n\n");
  131. }
  132.  
  133.  
  134. void desalocar_lista(void)
  135.  
  136. {
  137.  
  138.    aux2 = inicio;
  139.  
  140.    aux = inicio->prox;
  141.  
  142.  
  143.  
  144.    while(aux2 != NULL)
  145.  
  146.    {
  147.  
  148.       free(aux2);
  149.  
  150.       aux2 = aux;
  151.  
  152.       if(aux != NULL) aux = aux->prox;
  153.  
  154.    }
  155.  
  156. }
Add Comment
Please, Sign In to add comment