Advertisement
riquems

Lista Dinâmica Encadeada

Mar 28th, 2020
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define TRUE 1
  5. #define FALSE 0
  6.  
  7. typedef struct ListaDE ListaDE;
  8. typedef struct Item Item;
  9.  
  10. struct Item {
  11.     int value;
  12.     Item* next;
  13. };
  14.  
  15. struct ListaDE {
  16.     Item* item;
  17. };
  18.  
  19. ListaDE* newListaDE();
  20. int isEmpty(ListaDE* list);
  21. int has(int value, ListaDE* list);
  22. Item* get(int value, ListaDE* list);
  23.  
  24. void add(int value, ListaDE* list);
  25. void delete(int value, ListaDE* list);
  26. void print(ListaDE* list);
  27. void freeList(ListaDE* list);
  28.  
  29. ListaDE* newListaDE()
  30. {
  31.     ListaDE* newList = malloc(sizeof(ListaDE));
  32.     newList->item = NULL;
  33.  
  34.     return newList;
  35. }
  36.  
  37. int isEmpty(ListaDE* list) {
  38.     return list->item == NULL;
  39. }
  40.  
  41. int has(int value, ListaDE* list)
  42. {
  43.     Item* item = list->item;
  44.  
  45.     while (item != NULL) {
  46.         if (item->value == value)
  47.             return TRUE;
  48.  
  49.         item = item->next;
  50.     }
  51.  
  52.     return FALSE;
  53. }
  54.  
  55. Item* get(int value, ListaDE* list)
  56. {
  57.     Item* item = list->item;
  58.  
  59.     while (item != NULL && item->value != value)
  60.         item = item->next;
  61.  
  62.     return item;
  63. }
  64.  
  65. void add(int value, ListaDE* list)
  66. {
  67.     if (has(value, list))
  68.         return;
  69.  
  70.     Item* newItem = malloc(sizeof(Item));
  71.     newItem->value = value;
  72.     newItem->next = NULL;
  73.    
  74.     if (isEmpty(list)) {
  75.         list->item = newItem;
  76.         return;
  77.     }
  78.  
  79.     Item *item = list->item;
  80.  
  81.     if (item->value > value)
  82.     {
  83.         newItem->next = item;
  84.         list->item = newItem;
  85.         return;
  86.     }
  87.  
  88.     while (item->next != NULL && value > item->next->value)
  89.         item = item->next;
  90.  
  91.     newItem->next = item->next;
  92.     item->next = newItem;
  93. }
  94.  
  95. void delete(int value, ListaDE* list)
  96. {
  97.     if (isEmpty(list) || !has(value, list))
  98.         return;
  99.  
  100.     Item* item = list->item;
  101.  
  102.     if (item->value == value)
  103.     {
  104.         if (item->next == NULL)
  105.         {
  106.             list->item = NULL;
  107.         }
  108.         else
  109.         {
  110.             list->item = item->next;
  111.         }
  112.  
  113.         free(item);
  114.         return;
  115.     }
  116.  
  117.     while(item->next != NULL && item->next->value != value)
  118.         item = item->next;
  119.  
  120.     Item* itemToBeDeleted = item->next;
  121.  
  122.     item->next = itemToBeDeleted->next;
  123.  
  124.     free(itemToBeDeleted);
  125. }
  126.  
  127. void print(ListaDE* list)
  128. {
  129.     Item* item = list->item;
  130.  
  131.     while (item != NULL) {
  132.         printf("%d%c", item->value, (item->next == NULL) ? '\n' : ' ');
  133.         item = item->next;
  134.     }
  135. }
  136.  
  137. void freeList(ListaDE* list)
  138. {
  139.     Item* item = list->item;
  140.  
  141.     while (item != NULL) {
  142.         Item* next = item->next;
  143.  
  144.         free(item);
  145.         item = next;
  146.     }
  147.  
  148.     free(list);
  149. }
  150.  
  151. int main()
  152. {
  153.     ListaDE* list = newListaDE();
  154.  
  155.     int value;
  156.  
  157.     char c;
  158.     while (scanf("%c", &c) != EOF)
  159.     {
  160.         switch (c)
  161.         {
  162.             case 'I':
  163.                 scanf("%d", &value);
  164.                
  165.                 add(value, list);
  166.             break;
  167.  
  168.             case 'R':
  169.                 scanf("%d", &value);
  170.  
  171.                 delete(value, list);
  172.             break;
  173.  
  174.             case 'B':
  175.                 scanf("%d", &value);
  176.  
  177.                 if (has(value, list))
  178.                     printf("SIM\n");
  179.                 else
  180.                     printf("NAO\n");
  181.             break;
  182.  
  183.             case 'L':
  184.                 print(list);
  185.             break;
  186.  
  187.             default:
  188.             break;
  189.         }
  190.     }
  191.  
  192.     freeList(list);
  193.     return 0;
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement