Advertisement
mrWeiss

linked list

Dec 20th, 2023 (edited)
881
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.34 KB | Source Code | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. struct node {
  5.     int value;
  6.     struct node *next;
  7. };
  8.  
  9. struct node *list_from_array(int *array, int length);
  10. void list_print(struct node *list);
  11. struct node *list_find(struct node **list, int value);
  12. void list_clear(struct node **list);
  13.  
  14. void test_init(struct node **list);
  15. void test(struct node **list, int value);
  16.  
  17. int main(void) {
  18.     struct node *list_head;
  19.     test_init(&list_head);
  20.  
  21.     test(&list_head, 11);
  22.     test(&list_head, 1);
  23.     test(&list_head, 12);
  24.     test(&list_head, 10);
  25.     test(&list_head, 9);
  26.     test(&list_head, 13);
  27.     test(&list_head, 9);
  28.     test(&list_head, 7);
  29.     test(&list_head, 14);
  30.     test(&list_head, 1);
  31.     test(&list_head, 4);
  32.     test(&list_head, 15);
  33.  
  34.     list_clear(&list_head);
  35.     return 0;
  36. }
  37.  
  38. void test_init(struct node **list) {
  39.     int init_array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  40.     *list = list_from_array(init_array, sizeof init_array / sizeof *init_array);
  41.  
  42.     list_print(*list);
  43.     printf("\n");  
  44. }
  45.  
  46. void test(struct node **list, int value) {
  47.     struct node *found = list_find(list, value);
  48.     if(!found)
  49.         printf("%d NO\n", value);
  50.     else
  51.         printf("%d OK\n", found->value);
  52.  
  53.     list_print(*list);
  54.     printf("\n");
  55. }
  56.  
  57. struct node *list_from_array(int *array, int length) {
  58.     struct node *list = NULL;
  59.     struct node **next_ptr = &list;
  60.     for(int i = 0; i < length; i++) {
  61.         struct node *new = malloc(sizeof *new);
  62.         if(!new) {
  63.             list_clear(&list);
  64.             break;
  65.         }
  66.         new->value = array[i];
  67.         /* *new = (struct node) {
  68.             .value = array[i],
  69.             .next  = NULL,
  70.         }; */
  71.         *next_ptr = new;
  72.         next_ptr = &new->next;
  73.     }
  74.     *next_ptr = NULL;
  75.     return list;
  76. }
  77.  
  78. void list_print(struct node *list) {
  79.     if (!list) {
  80.         printf("[]");
  81.         return;
  82.     }
  83.  
  84.     printf("[%d", list->value);
  85.     list = list->next;
  86.  
  87.     while (list) {
  88.         printf(", %d", list->value);
  89.         list = list->next;
  90.     }
  91.  
  92.     printf("]");
  93. }
  94.  
  95. struct node *list_find(struct node **list, int value) {
  96.     struct node **next_ptr = list;
  97.     while(*next_ptr) {
  98.         struct node *cur = *next_ptr;
  99.         if (cur->value == value) {
  100.             *next_ptr = cur->next;
  101.             cur->next = *list;
  102.             return *list = cur;
  103.         }
  104.         next_ptr = &cur->next;
  105.     }
  106.     return NULL;
  107. }
  108.  
  109. void list_clear(struct node **list) {
  110.     struct node *cur = *list;
  111.     while(cur) {
  112.         struct node *next = cur->next;
  113.         free(cur);
  114.         cur = next;
  115.     }
  116.     *list = NULL;
  117. }
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement