Guest User

Untitled

a guest
Feb 18th, 2024
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.67 KB | None | 0 0
  1. // List implementation
  2. typedef struct ListNode {
  3.     void* data;
  4.     struct ListNode* next;
  5.     struct ListNode* prev;
  6. } ListNode;
  7.  
  8. typedef struct List {
  9.     int size;
  10.     ListNode* head;
  11.     ListNode* tail;
  12.     size_t type;
  13. } List;
  14. List* list_init() {
  15.     List* list = malloc(sizeof(List));
  16.     assert(list);
  17.  
  18.     list->size = 0;
  19.     list->head = NULL;
  20.     list->tail = NULL;
  21.  
  22.     return list;
  23. }
  24.  
  25. void list_cleanup(List* list) {
  26.     ListNode* current = list->head;
  27.     while (current != NULL) {
  28.         ListNode* next = current->next;
  29.         free(current);
  30.         current = next;
  31.     }
  32.     free(list);
  33. }
  34.  
  35. void list_push(List* list, void* data) {
  36.     ListNode* node = malloc(sizeof(ListNode));
  37.     assert(node);
  38.  
  39.     node->data = data;
  40.     node->next = list->head;
  41.     node->prev = NULL;
  42.  
  43.     if (list->head != NULL) {
  44.         list->head->prev = node;
  45.     } else {
  46.         list->tail = node;
  47.     }
  48.     list->head = node;
  49.  
  50.     list->size++;
  51. }
  52.  
  53. void list_remove_nth(List* list, int n) {
  54.     assert(n >= 0 && n < list->size);
  55.     ListNode* cur = list->head;
  56.     for (int i = 0; i < n && cur != NULL; i++)
  57.         cur = cur->next;
  58.  
  59.     if (cur == list->head) {
  60.         list->head = cur->next;
  61.         if (list->head != NULL)
  62.             list->head->prev = NULL;
  63.         else
  64.             list->tail = NULL;
  65.     } else if (cur == list->tail) {
  66.         list->tail = cur->prev;
  67.         list->tail->next = NULL;
  68.     } else {
  69.         cur->prev->next = cur->next;
  70.         cur->next->prev = cur->prev;
  71.     }
  72.  
  73.     free(cur);
  74.     list->size--;
  75. }
  76.  
  77. void* list_get_nth(List* list, int n) {
  78.     assert(n >= 0 && n < list->size);
  79.     ListNode* cur = list->head;
  80.     for (int i = 0; i < n; i++) {
  81.         cur = cur->next;
  82.     }
  83.  
  84.     return cur->data;
  85. }
  86.  
  87. // Main function
  88.     List* li = list_init();
  89.     int x = 4;
  90.     int y = 8;
  91.     list_push(li, &x);
  92.     list_push(li, &x);
  93.     list_push(li, &y);
  94.     list_push(li, &x);
  95.  
  96.     printf("List size is: %d\n", li->size);
  97.  
  98.     ListNode* cur = li->head;
  99.     printf("Getting list elements with while loop:\n");
  100.     while (cur != NULL) {
  101.         printf("%d\n", *(int*)cur->data);
  102.         cur = cur->next;
  103.     }
  104.  
  105.     printf("Getting list elements with for loop (list_get_nth):\n");
  106.     for (int i = 0; i < li->size - 1; i++) {
  107.         printf("%d: %d\n", i, *(int*)list_get_nth(li, i));
  108.     }
  109.  
  110.     list_remove_nth(li, 0);
  111.     list_remove_nth(li, 3);
  112.     printf("List size after removing elements: %d\n", li->size);
  113.  
  114.     printf("New List elements (using for loop):\n");
  115.     for (int i = 0; i < li->size - 1; i++) {
  116.         printf("%d\n", *(int*)list_get_nth(li, i));
  117.     }
  118.  
  119.     list_cleanup(li);
  120.  
Advertisement
Add Comment
Please, Sign In to add comment