Advertisement
justaCprogrammer

listaencadedString.C

Jun 22nd, 2022
1,000
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.08 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. struct st_next
  6. {
  7.     int id;
  8.     char name[100];
  9.  
  10.     struct st_next *nextI;
  11.     struct st_next *nextS;
  12. };
  13.  
  14. typedef struct st_next *no;
  15.  
  16. int vazio (no * reader)
  17. {
  18.     if((reader->nextI == NULL)&&(reader->nextS == NULL))
  19.     {
  20.         return 1;
  21.     }else
  22.     {
  23.         return 0;
  24.     }
  25. }
  26.  
  27. void initial (no * reader)
  28. {
  29.     reader->nextI = NULL;
  30.     reader->nextS = NULL;
  31. }
  32.  
  33. void freeUsage(no * reader)
  34. {
  35.     if(!vazio(reader))
  36.     {
  37.         no *proxEl,*proxN, *atualI,*atualS;
  38.         atualI = reader->nextI;
  39.         atualS = reader->nextS;
  40.         while((atualI != NULL)&&(atualS != NULL))
  41.         {
  42.             proxEl = atualI->nextI;
  43.             free(atualI);
  44.             atualI = proxEl;
  45.             proxN = atualS->nextS;
  46.             free(atualS);
  47.             atualS = proxN;
  48.         }
  49.     }
  50. }
  51.  
  52. void showList (no * reader)
  53. {
  54.     if(vazio(reader))
  55.     {
  56.         printf("Primeiro adicione algum elemento na lista\n");
  57.         exit(1);
  58.     }else
  59.     {
  60.         no *tmpI,tmpS;
  61.         tmpS = malloc(100 * sizeof(char*));
  62.         tmpI = reader->next;
  63.         while((tmpI != NULL)&&(tmpS != NULL))
  64.         {
  65.             printf("%d",tmpI->id);
  66.             tmpI = tmpI->nextI;
  67.             printf("%s",tmpS->name);
  68.             tmpS = tmpS->nextS;
  69.         }
  70.  
  71.     }
  72. }
  73.  
  74. void adicionaInicio (no * reader)
  75. {
  76.     no *novoElI = (no*)malloc(sizeof(no));
  77.     no *novoElS = (no*)malloc(sizeof(no));
  78.  
  79.     if((!novoElI)&&(!novoElS))
  80.     {
  81.         printf("Sem espaço! Libere um pouco antes de executar\n");
  82.         exit(1);
  83.     }else
  84.     {
  85.         printf("Adicione o elemento\n");
  86.         novoElS->name= malloc(100);
  87.         fgets(novoElS->name,100,stdin);
  88.         scanf("%d",&novoElI->id);
  89.  
  90.         no * velhoTopI = reader->nextI;
  91.         reader->nextI = novoElI;
  92.         novoElI->nextI =velhoTopI;
  93.  
  94.         novoElS->name[reader];
  95.  
  96.         no *velhoTopS = reader ->nextS;
  97.         reader->nextS = novoElS;
  98.         novoElS->nextS = velhoTopS;
  99.  
  100.     }
  101.  
  102. }
  103.  
  104. void adicionaFim (no * reader)
  105. {
  106.     no *novoElI = (no*)malloc(sizeof(no));
  107.     no *novoElS = (no*)malloc(sizeof(no));
  108.  
  109.     if((!novoElI)&&(!novoElS))
  110.     {
  111.         printf("Sem memoria disponivel\n");
  112.         exit(1);
  113.     }
  114.  
  115.     printf("Adicione o elemento do fim\n");
  116.     novoElS->name = malloc(100);
  117.     fgets(novoElS->name,100,stdin);
  118.     scanf("%d",&novoElS->id);
  119.     novoElI->nextI = NULL;
  120.     novoElS->nextS = NULL;
  121.  
  122.     if(vazio(reader))
  123.     {
  124.         reader->nextI = novoElI;
  125.         reader->nextS = novoElS;
  126.     }else
  127.     {
  128.         no *tmpI = reader->nextI;
  129.         no *tmpS = reader->nextS;
  130.  
  131.         while((tmpI->nextI != NULL)&&(tmpS->nextS != NULL))
  132.         {
  133.             tmpI = tmpI->nextI;
  134.             tmpS = tmpS->nextS;
  135.         }
  136.  
  137.         tmpI->nextI = novoElI;
  138.         tmpS->nextS = novoElS;
  139.     }
  140. }
  141.  
  142. int userInterface()
  143. {
  144.     system("Cls");
  145.     int opt;
  146.     printf("----------|Menu|--------------\n");
  147.     printf("[1] - Adicionar um elemento no inicio da lista\n");
  148.     printf("[2] - Adicionar um elemento no fim da lista\n");
  149.     printf("[3] - Imprimir elementos na tela\n");
  150.     printf("[4] - Terminar programa\n");
  151.     printf("Sua opcao: ");
  152.     scanf("%d",&opt);
  153.     return opt;
  154. }
  155.  
  156. void choosenOne (no * reader, int opt)
  157. {
  158.     switch(opt)
  159.     {
  160.     case 1:
  161.         adicionaInicio(reader);
  162.         break;
  163.  
  164.     case 2:
  165.         adicionaFim(reader);
  166.         break;
  167.  
  168.     case 3:
  169.         showList(reader);
  170.         system("PAUSE");
  171.         break;
  172.  
  173.     case 4:
  174.          printf("Fechando o programa!\n");
  175.          freeUsage(reader);
  176.          break;
  177.  
  178.     default:
  179.         printf("Opcao invalida,por favor escolha uma opcao valida\n");
  180.         break;
  181.     }
  182. }
  183.  
  184. int main ()
  185. {
  186.     int opcao;
  187.     no * reader= (no*) malloc(sizeof(no));
  188.  
  189.     if(!reader)
  190.     {
  191.         printf("Sem memoria man!\n");
  192.         exit(1);
  193.     }
  194.     initial(reader);
  195.     do
  196.     {
  197.     opcao = userInterface();
  198.     choosenOne(reader,opcao);
  199.  
  200.     }while(opcao != 4);
  201.  
  202.     free(reader);
  203.  
  204.     return 0;
  205. }
  206.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement