Advertisement
Semper_Idem

Linked List demo 1

Nov 7th, 2020
2,640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct slist_node
  6. {
  7.     void *data;
  8.     struct slist_node *next;
  9. };
  10.  
  11. typedef void (*datafree_t)(void *data);
  12. struct slist
  13. {
  14.     struct slist_node *head;
  15.     datafree_t datafree;
  16. };
  17.  
  18. typedef void (*node_visit_t)(struct slist_node *node);
  19. typedef int (*datacmp_t)(void *a, void *b);
  20.  
  21. void slist_walk(struct slist *list, node_visit_t visit)
  22. {
  23.     struct slist_node *node = NULL;
  24.     for (node = list->head; node; node = node->next)
  25.     {
  26.         visit(node);
  27.     }
  28. }
  29.  
  30. void slist_print_strings(struct slist *list)
  31. {
  32.     slist_walk(list, snode_print_string_data);
  33. }
  34.  
  35. struct slist_node *slist_node_new(void *data)
  36. {
  37.     struct slist_node *new = malloc(sizeof(*new));
  38.     // printf("slist_node_new() %p\n", new);
  39.     new->data = data;
  40.     new->next = NULL;
  41.     return new;
  42. }
  43. void slist_add_left(struct slist *list, struct slist_node *node)
  44. {
  45.     node->next = list->head;
  46.     list->head = node;
  47.     return;
  48. }
  49.  
  50. void slist_node_free(struct slist_node *node, datafree_t datafree)
  51. {
  52.     // printf("slist_node_free() %p\n", node);
  53.     if (datafree)
  54.     {
  55.         datafree(node->data);
  56.     }
  57.     free(node);
  58. }
  59.  
  60. void slist_delete_node(struct slist *list, void *key, datacmp_t compare)
  61. {
  62.     if (list->head)
  63.     {
  64.         if (compare(list->head->data, key) == 0)
  65.         {
  66.             struct slist_node *node = list->head;
  67.             list->head = list->head->next;
  68.             slist_node_free(node, list->datafree);
  69.         }
  70.         else
  71.         {
  72.             struct slist_node *prev = list->head;
  73.             struct slist_node *current = prev->next;
  74.             while (current)
  75.             {
  76.                 if (compare(current->data, key) == 0)
  77.                 {
  78.                     prev->next = current->next;
  79.                     slist_node_free(current, list->datafree);
  80.                     return;
  81.                 }
  82.                 prev = current;
  83.                 current = current->next;
  84.             }
  85.         }
  86.     }
  87. }
  88.  
  89. void slist_free(struct slist *list)
  90. {
  91.     struct slist_node *next;
  92.     struct slist_node *current = list->head;
  93.  
  94.     while (current)
  95.     {
  96.         next = current->next;
  97.         slist_node_free(current, list->datafree);
  98.         current = next;
  99.     }
  100. }
  101.  
  102. struct slist_node *slist_find_node(struct slist *list, void *key, datacmp_t compare)
  103. {
  104.     struct slist_node *node;
  105.     for (node = list->head; node; node = node->next)
  106.     {
  107.         if (compare(key, node->data) == 0)
  108.         {
  109.             return node;
  110.         }
  111.     }
  112.     return NULL;
  113. }
  114.  
  115. void snode_print_string_data(struct slist_node *node)
  116. {
  117.     printf("data = %s, \n", node->data);
  118. }
  119.  
  120. char *console_read_string(void)
  121. {
  122.     char *str = malloc(100);
  123.     printf("malloc() str = %p\n", str);
  124.     scanf("%100s", str);
  125.     fflush(stdin);
  126.     return str;
  127. }
  128.  
  129. void free_string(char *str)
  130. {
  131.     printf("free_string() %p\n", str);
  132.     free(str);
  133. }
  134.  
  135. int main(void)
  136. {
  137.     fflush(stdin);
  138.     struct slist list = {0};
  139.     list.datafree = free_string;
  140.  
  141.     for (int i = 0; i < 5; i++)
  142.     {
  143.         slist_add_left(&list, slist_node_new(console_read_string()));
  144.     }
  145.  
  146.     struct slist_node *three = slist_find_node(&list, "three", strcmp);
  147.     if (three)
  148.     {
  149.         printf("Found!\n");
  150.     }
  151.     else
  152.     {
  153.         printf("Not found!\n");
  154.     }
  155.  
  156.     printf("Deleting 'one'!\n");
  157.     slist_delete_node(&list, "one", strcmp);
  158.     slist_print_strings(&list);
  159.  
  160.     printf("Deleting 'three'!\n");
  161.     slist_delete_node(&list, "three", strcmp);
  162.     slist_print_strings(&list);
  163.  
  164.     slist_free(&list);
  165.     return 0;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement