Advertisement
justaCprogrammer

listaencadedComplete.c

Jul 18th, 2022
1,042
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.05 KB | None | 0 0
  1.  #include <stdio.h>
  2.     #include <stdlib.h>
  3.     #include <string.h>
  4.     #include <stdbool.h>
  5.     struct node
  6.     {
  7.         char name[100];
  8.         int id;
  9.         struct node *next;
  10.     };
  11.  
  12.     typedef struct node no;
  13.  
  14.     bool vazio(no * reader)
  15.     {
  16.  
  17.     if(reader = NULL)
  18.     {
  19.         return 1;
  20.     }else
  21.     {
  22.         return 0;
  23.     }
  24.  
  25.     }
  26.  
  27.     void inicia (no * reader)
  28.     {
  29.         reader->next = NULL;
  30.     }
  31.  
  32.     void liberaEspaco(no * reader)
  33.     {
  34.         if(!vazio(reader))
  35.         {
  36.         no *proxEl, *atual;
  37.         atual = reader->next;
  38.         while(atual != NULL)
  39.         {
  40.             proxEl = atual->next;
  41.             free(atual);
  42.             atual = proxEl;
  43.         }
  44.         }
  45.  
  46.     }
  47.  
  48.     void showList(no * reader)
  49.     {
  50.         no * tmp;
  51.         tmp = reader->next;
  52.         while(tmp !=NULL)
  53.         {
  54.             printf("Nome:%s\n",tmp->name);
  55.             printf("Idade:%d\n",tmp->id);
  56.             printf("=========================\n");
  57.             tmp = tmp->next;
  58.         }
  59.     }
  60.  
  61.     void adicionaInicio (no * reader)
  62.     {
  63.         no * novoEl;
  64.         int cont;
  65.         char **tempName;
  66.         tempName = malloc(1 * sizeof(int));
  67.         novoEl =(no*)malloc(sizeof(no));
  68.         if(vazio(novoEl))
  69.         {
  70.             printf("Sem espaco man!\n");
  71.             exit(1);
  72.         }
  73.     fflush(stdin);
  74.      for(cont = 0;cont < 1; cont++)
  75.     {
  76.         printf("Adicione um Elemento\n");
  77.         tempName[cont] = malloc(100);
  78.         fgets(tempName[cont],100,stdin);
  79.         strcpy(&novoEl->name[cont],tempName[cont]);
  80.     }
  81.     printf("De a idade ao elemento\n");
  82.     scanf("%d",&novoEl->id);
  83.  
  84.     no *velhoTopo = reader->next;
  85.     reader -> next = novoEl;
  86.     novoEl ->next = velhoTopo;
  87.     }
  88.  
  89.     void adicionaFim(no * reader)
  90.     {
  91.  
  92.     no * novoEl;
  93.     int cont;
  94.     char **tempName;
  95.     tempName = malloc(1 * sizeof(int));
  96.     novoEl = (no*)malloc(sizeof(no));
  97.     if(vazio(novoEl))
  98.     {
  99.         printf("Sem espaco man\n");
  100.         exit(1);
  101.     }
  102.     fflush(stdin);
  103.     for(cont = 0;cont < 1;cont++)
  104.     {
  105.     printf("Digite o nome do elemento\n");
  106.     tempName[cont] = malloc(100);
  107.     fgets(tempName[cont],100,stdin);
  108.     strcpy(&novoEl->name[cont],tempName[cont]);
  109.     }
  110.     printf("De uma idade ao elemento\n");
  111.     scanf("%d",&novoEl->id);
  112.     novoEl -> next = NULL;
  113.         if(vazio(reader))
  114.         {
  115.             reader->next = novoEl;
  116.         }else
  117.         {
  118.             no* tmp = reader->next;
  119.  
  120.             while(tmp->next != NULL)
  121.             {
  122.                 tmp = tmp->next;
  123.             }
  124.             tmp->next = novoEl;
  125.         }
  126.  
  127.     }
  128.  
  129.     int ui()
  130.     {
  131.         system("CLS");
  132.         int opt;
  133.         printf("-----------|Menu|-----------------\n");
  134.         printf("===================================\n");
  135.         printf("[1] - Adicione um elemento no inicio\n");
  136.         printf("[2] - Adicione um elemento no fim \n");
  137.         printf("[3] - Mostre a lista de elementos \n");
  138.         printf("[4] - Finalizar o programa \n");
  139.         printf("----------------------------------------\n");
  140.         printf("A sua opcao:");
  141.         scanf("%d",&opt);
  142.         return opt;
  143.     }
  144.  
  145.     void opcao(no * reader,int opt)
  146.     {
  147.         switch(opt)
  148.         {
  149.         case 1:
  150.             adicionaInicio(reader);
  151.             break;
  152.  
  153.         case 2:
  154.             adicionaFim(reader);
  155.             break;
  156.  
  157.         case 3:
  158.             showList(reader);
  159.             system("pause");
  160.             break;
  161.  
  162.         case 4:
  163.             printf("Programa sendo encerrado!\n");
  164.             break;
  165.  
  166.         default:
  167.             printf("Opcao inválida! Escolha uma opcao válida\n");
  168.         }
  169.     }
  170.  
  171.     int main ()
  172.     {
  173.         int option;
  174.         no * reader =(no*)malloc(sizeof(no));
  175.     if(!reader)
  176.     {
  177.         printf("Sem memoria man!\n");
  178.         exit(1);
  179.     }
  180.             inicia(reader);
  181.         do
  182.         {
  183.             option = ui();
  184.             opcao(reader,option);
  185.  
  186.         }while(option != 4);
  187.  
  188.         return 0;
  189.     }
  190.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement