Advertisement
KDOXG

LinkedList_exercise

Nov 19th, 2018
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4.  
  5. typedef struct{
  6.   int cod;
  7.   char nome[40];
  8. }SDado;
  9.  
  10. struct SNodo{
  11.   SDado info;
  12.   struct SNodo *pNext;
  13. };
  14.  
  15. typedef struct{
  16.   struct SNodo *pFirst;
  17. }SLista;
  18.  
  19. void Clear(struct SNodo *pFirst)
  20. {
  21.     struct SNodo *pAtual, *pAnterior;
  22.  
  23.     if (pFirst->pNext == NULL)
  24.         return;
  25.    
  26.     pAtual = pFirst;
  27.     for (; pAtual->pNext != NULL;)
  28.     {
  29.         pAnterior =  pAtual;
  30.         pAtual = pAtual->pNext;      
  31.     }
  32.     free(pAtual);
  33.     pAnterior->pNext = NULL;
  34.     return;
  35. }
  36.  
  37. void Reset(SLista *pLista)
  38. {
  39.     while (pLista->pFirst->pNext != NULL)
  40.         Clear(pLista->pFirst);
  41.     free(pLista);
  42. }
  43.  
  44. bool PUSH(SLista *pLista, struct SNodo *pNodo, unsigned int nIndex)
  45. {
  46.     struct SNodo *pAtual, *pAnterior;
  47.     int nPos;
  48.  
  49.     if (pLista->pFirst == NULL && nIndex !=0)
  50.         return 0; /* erro: nIndex invalido */
  51.    
  52.     if (pLista->pFirst == NULL && nIndex == 0)
  53.     {
  54.         pLista->pFirst = pNodo;
  55.         pLista->pFirst->pNext = NULL;
  56.         return 1;
  57.     }
  58.    
  59.     if (nIndex == 0)
  60.     {
  61.         pNodo->pNext = pLista->pFirst;
  62.         pLista->pFirst = pNodo;
  63.         return 1;            
  64.     }
  65.  
  66.     pAtual = pLista->pFirst;
  67.     for (nPos=0; nPos < nIndex && pAtual != NULL; nPos++)
  68.     {
  69.         pAnterior = pAtual;
  70.         pAtual = pAtual->pNext;
  71.     }
  72.     if (!pAtual) return 0;
  73.    
  74.     pNodo->pNext = pAtual->pNext;
  75.     pAtual->pNext = pNodo;
  76.     return 1;
  77. }
  78.  
  79. bool POP(SLista *pLista, struct SNodo *pNodo, unsigned int nIndex)
  80. {
  81.     struct SNodo *pAnterior, *pAtual;
  82.     int nPos;
  83.    
  84.     if (pLista->pFirst == NULL) return 0; /* erro: lista vazia */
  85.  
  86.     if (nIndex == 0)
  87.     {
  88.         pNodo = pLista->pFirst;
  89.         pLista->pFirst = pLista->pFirst->pNext;
  90.         return 1;
  91.     }
  92.  
  93.     pAtual = pLista->pFirst;
  94.     for (nPos = 0; nPos < nIndex && pAtual != NULL; nPos++)
  95.     {
  96.         pAnterior =  pAtual;
  97.         pAtual = pAtual->pNext;
  98.     }
  99.     if (!pAtual) return 0; // erro nIndex invalido
  100.     pAnterior->pNext = pAtual->pNext;    
  101.     pNodo = pAtual;
  102.     return 1;
  103. }
  104.  
  105. void Imprime(SLista *pLista)
  106. {
  107.     struct SNodo *pAnterior, *pAtual=pLista->pFirst;
  108.     for (; pAtual->pNext != NULL; )
  109.     {
  110.         printf("Nome: %s - Cod.: %d\n", pAtual->info.nome, pAtual->info.cod);
  111.         pAnterior = pAtual;
  112.         pAtual = pAtual->pNext;
  113.     }
  114.  
  115. }
  116.  
  117. void main()
  118. {
  119.     SLista *Encadeada=malloc(sizeof(SLista));
  120.     Encadeada->pFirst=NULL;
  121.     struct SNodo *Nodo;
  122.     unsigned int i=0, j, a;
  123.     bool confere;
  124.     Inicio: printf("\nSelecione a sua escolha...\n0: Insere pessoa\n1: Deleta pessoa\n2: Limpa a lista\n3: Lista na tela as pessoas\n4: Sair do programa\nDigite: ");
  125.     scanf("%lu", &a);
  126.     switch(a)
  127.     {
  128.         case 0:
  129.             if (i > 0)
  130.             {
  131.                 printf("Digite o índice: ");
  132.                 scanf("%lu", &j);
  133.             }
  134.             Nodo=malloc(sizeof(struct SNodo));
  135.             printf("Digite o nome: ");
  136.             getchar();
  137.             fgets(Nodo->info.nome,40,stdin);
  138.             Nodo->info.cod = (int) *(Nodo->info.nome);
  139.             confere=PUSH(Encadeada,Nodo,j);
  140.             if (confere == true)
  141.             {
  142.                 printf("Adicionado com sucesso!\n");
  143.                 i++;
  144.             }
  145.             else
  146.                 printf("Erro.\n");
  147.             goto Inicio;
  148.         break;
  149.  
  150.         case 1:
  151.             if (i > 1)
  152.             {
  153.                 printf("Digite o índice: ");
  154.                 scanf("%lu", &j);
  155.             }
  156.             confere=POP(Encadeada,Nodo,j);
  157.             free(Nodo);
  158.             if (confere == true)
  159.             {
  160.                 printf("Removido com sucesso!\n");
  161.                 i--;
  162.             }
  163.             else
  164.                 printf("Erro.\n");
  165.             goto Inicio;
  166.         break;
  167.  
  168.         case 2:
  169.             Reset(Encadeada);
  170.             Encadeada=malloc(sizeof(SLista));
  171.             printf("Lista limpa!\n");
  172.             goto Inicio;
  173.         break;
  174.  
  175.         case 3:
  176.             Imprime(Encadeada);
  177.             goto Inicio;
  178.         break;
  179.  
  180.         case 4:
  181.             Reset(Encadeada);
  182.         break;
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement