Advertisement
Ot_Motta

LISTA MODELO - SIMPLESMENTE ENCADEADA

Sep 23rd, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct
  5. {
  6.     int item;
  7.     struct Tlista *prox;
  8. }Tlista;
  9.  
  10. typedef struct
  11. {
  12.     Tlista *fim;
  13.     Tlista *inicio;
  14. }lista;
  15.  
  16. void create_lista(lista *lista)
  17. {
  18.     lista->fim    = NULL;
  19.     lista->inicio = NULL;
  20. }
  21.  
  22. int ver_lista_vazia(lista *lista)
  23. {
  24.     if(lista->inicio == NULL)
  25.         return 1;
  26.     else
  27.         return 0;
  28. }
  29.  
  30. void listar_fim(lista *lista)
  31. {
  32.     int valor;
  33.     //CRIAR UMA NOVA CÉLULA PARA SER ADICIONADA A LISTA
  34.     Tlista *novo;
  35.     novo = (Tlista*)malloc(sizeof(Tlista));
  36.     novo->prox = NULL;
  37.  
  38.     printf("\nINFORME UM VALOR A SER ADICIONADO NA LISTA: ");
  39.     fflush(stdin);
  40.     scanf("%d", &valor);
  41.  
  42.     novo->item = valor;
  43.  
  44.     int result;
  45.     result = ver_lista_vazia(lista);
  46.  
  47.     if(result)
  48.         lista->inicio = novo;
  49.     else
  50.         lista->fim->prox = novo;
  51.     lista->fim = novo;
  52.  
  53.     printf("\n%d FOI LISTADO COM SUCCESS...\n", lista->fim->item);
  54. }
  55.  
  56. void listar_inicio(lista *lista)
  57. {
  58.     int valor;
  59.     //CRIAR UMA NOVA CÉLULA PARA SER ADICIONADA A LISTA
  60.     Tlista *novo;
  61.     novo = (Tlista*)malloc(sizeof(Tlista));
  62.     novo->prox = NULL;
  63.  
  64.     printf("\nINFORME UM VALOR A SER ADICIONADO NA LISTA: ");
  65.     fflush(stdin);
  66.     scanf("%d", &valor);
  67.  
  68.     novo->item = valor;
  69.  
  70.     int result;
  71.     result = ver_lista_vazia(lista);
  72.  
  73.     if(result)
  74.     {
  75.         lista->inicio = novo;
  76.         lista->fim = novo;
  77.     }
  78.     else
  79.     {
  80.         novo->prox = lista->inicio;
  81.         lista->inicio = novo;
  82.     }
  83.  
  84.  
  85.     printf("\n%d FOI LISTADO COM SUCCESS...\n", lista->inicio->item);
  86. }
  87.  
  88. void listar_meio(lista *lista)
  89. {
  90.     int valor;
  91.     //CRIAR UMA NOVA CÉLULA PARA SER ADICIONADA A LISTA
  92.     Tlista *novo;
  93.     novo = (Tlista*)malloc(sizeof(Tlista));
  94.     novo->prox = NULL;
  95.  
  96.     printf("\nINFORME UM VALOR A SER ADICIONADO NA LISTA: ");
  97.     fflush(stdin);
  98.     scanf("%d", &valor);
  99.  
  100.     novo->item = valor;
  101.  
  102.     int result;
  103.     result = ver_lista_vazia(lista);
  104.  
  105.     if(result)
  106.     {
  107.         lista->inicio = novo;
  108.         lista->fim = novo;
  109.     }
  110.     else
  111.     {
  112.         Tlista  *aux;
  113.         aux = (Tlista*)malloc(sizeof(Tlista));
  114.  
  115.         int antec = 0;
  116.  
  117.         printf("\nDIGITE O ANTECESSOR: ");
  118.         fflush(stdin);
  119.         scanf("%d", &antec);
  120.  
  121.         aux = lista->inicio;
  122.  
  123.         while(aux->item != antec)
  124.         {
  125.             aux = aux->prox;
  126.         }
  127.  
  128.         novo->prox = aux->prox;
  129.         aux->prox = novo;
  130.     }
  131.  
  132.     printf("\n%d FOI LISTADO COM SUCCESS...\n", novo->item);
  133. }
  134.  
  135. void retira_inicio_lista(lista *lista)
  136. {
  137.     int result;
  138.     result = ver_lista_vazia(lista);
  139.     int retirado;
  140.     Tlista *aux;
  141.     aux = (Tlista*)malloc(sizeof(Tlista));
  142.  
  143.     if(result)
  144.         printf("\nLISTA VAZIA! NADA PARA DESLISTAR...\n");
  145.     else
  146.     {
  147.         aux = lista->inicio;
  148.         lista->inicio = aux->prox;
  149.         retirado = aux->item;
  150.         aux->prox = NULL;
  151.         free(aux);
  152.  
  153.         printf("\n%d FOI RETIRADO COM SUCCESS", retirado);
  154.     }
  155.  
  156. }
  157.  
  158. void retira_meio_lista(lista *lista)
  159. {
  160.     int result;
  161.     result = ver_lista_vazia(lista);
  162.     int retirado;
  163.     Tlista *aux;
  164.     aux = (Tlista*)malloc(sizeof(Tlista));
  165.  
  166.     if(result)
  167.         printf("\nLISTA VAZIA! NADA PARA DESLISTAR...\n");
  168.     else
  169.     {
  170.         Tlista *aux = (Tlista*)malloc(sizeof(Tlista));
  171.         Tlista *ant = (Tlista*)malloc(sizeof(Tlista));
  172.         //O ANTERIOR RECEBER O INÍCIO DA LISTA
  173.         ant = lista->inicio;
  174.         //E O AUXILIAR RECEBE A PRÓXIMA POSIÇÃO DO INÍCIO
  175.         aux = ant->prox;
  176.  
  177.         int retirar;
  178.  
  179.         printf("\nINFORME O NUMERO QUE DESEJA RETIRAR: ");
  180.         fflush(stdin);
  181.         scanf("%d", &retirar);
  182.  
  183.         while(aux->item != retirar)
  184.         {
  185.             //MODIFICANDO AS "POSIÇÕES" DO ANTERIOR E DO AUXILIAR
  186.             ant = aux;
  187.             aux = aux->prox;
  188.         }
  189.         //RECONECTANDO A LISTA
  190.         ant->prox = aux->prox;
  191.         //DESCONECTANDO O AUXILIAR -> ELEMENTO A SER RETIRADO
  192.         aux->prox = NULL;
  193.         free(aux);
  194.         printf("\n%d FOI RETIRADO COM SUCCESS", retirar);
  195.     }
  196. }
  197.  
  198. void retira_fim_lista(lista *lista)
  199. {
  200.     int result;
  201.     result = ver_lista_vazia(lista);
  202.     int retirado;
  203.     Tlista *aux;
  204.     aux = (Tlista*)malloc(sizeof(Tlista));
  205.  
  206.     if(result)
  207.         printf("\nLISTA VAZIA! NADA PARA DESLISTAR...\n");
  208.     else
  209.     {
  210.         aux = lista->inicio;
  211.  
  212.         if(lista->inicio == lista->fim)
  213.         {
  214.             lista->inicio == NULL;
  215.             lista->fim == NULL;
  216.         }
  217.         else
  218.         {
  219.             while(aux->prox != lista->fim)
  220.             {
  221.                 aux = aux->prox;
  222.             }
  223.             retirado = lista->fim->item;
  224.             lista->fim = aux;
  225.             aux->prox = NULL;
  226.  
  227.             printf("\n%d FOI RETIRADO COM SUCCESS", retirado);
  228.         }
  229.     }
  230. }
  231.  
  232. void mostrar(lista *lista)
  233. {
  234.     int result;
  235.     result = ver_lista_vazia(lista);
  236.  
  237.     if(result)
  238.         printf("\LISTA VAZIA! NADA PARA MOSTRAR...\n");
  239.     else
  240.     {
  241.         Tlista *aux;
  242.         aux = (Tlista*)malloc(sizeof(Tlista));
  243.         aux = lista->inicio;
  244.  
  245.         while(aux != NULL)
  246.         {
  247.             printf("\n%d\n", aux->item);
  248.             aux = aux->prox;
  249.         }
  250.     }
  251. }
  252.  
  253. void menu(lista *lista)
  254. {
  255.     int opc;
  256.     //LAÇO PRINCIPAL
  257.     do
  258.     {
  259.         system("cls");
  260.  
  261.         printf("\n========MENU DE OPCOES========\n");
  262.         printf("\n1 - ADD AO INICIO DA LISTA");
  263.         printf("\n2 - ADD AO MEIO DA LISTA");
  264.         printf("\n3 - ADD AO FIM DA LISTA");
  265.         printf("\n4 - RETIRAR DO INICIO DA LISTA");
  266.         printf("\n5 - RETIRAR DO MEIO DA LISTA");
  267.         printf("\n6 - RETIRAR DO FIM DA LISTA");
  268.         printf("\n7 - MOSTRAR A LISTA");
  269.         printf("\n8 - SAIR");
  270.         printf("\n\nINFORME A OPCAO DESEJADA: ");
  271.         fflush(stdin);
  272.         scanf("%d", &opc);
  273.         //VALIDAR CORRETIVAMENTE A OPÇÃO
  274.         while(opc < 1 || opc > 8)
  275.         {
  276.             printf("\n\nOPCAO INVALIDA!\nINFORME A OPCAO DESEJADA: ");
  277.             fflush(stdin);
  278.             scanf("%d", &opc);
  279.         }
  280.         system("cls");
  281.  
  282.         switch(opc)
  283.         {
  284.         case 1:
  285.             listar_inicio(lista);
  286.             break;
  287.         case 2:
  288.             listar_meio(lista);
  289.             break;
  290.         case 3:
  291.             listar_fim(lista);
  292.             break;
  293.         case 4:
  294.             retira_inicio_lista(lista);
  295.             break;
  296.         case 5:
  297.             retira_meio_lista(lista);
  298.             break;
  299.         case 6:
  300.             retira_fim_lista(lista);
  301.             break;
  302.         case 7:
  303.             mostrar(lista);
  304.             break;
  305.         }
  306.  
  307.         if(opc != 8)
  308.             getch();
  309.  
  310.     }while(opc != 8);
  311. }
  312.  
  313. int main()
  314. {
  315.     lista *l;
  316.  
  317.     l = (lista*)malloc(sizeof(lista));
  318.  
  319.     create_lista(l);
  320.     menu(l);
  321.  
  322.     printf("\n\nFIM DO PROGRAMA...\n\n");
  323.  
  324.     return 0;
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement