Advertisement
LtStaffel

doubly linked list

Dec 15th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.50 KB | None | 0 0
  1. #include "includes.h"
  2.  
  3. typedef struct Node {
  4.     struct Node* prev;
  5.     int data;
  6.     struct Node* next;
  7. } node;
  8.  
  9. #define NODESIZE sizeof(struct Node)
  10.  
  11. struct Node* node_init(int dat) {
  12.     struct Node* newNode = (struct Node*)malloc(NODESIZE);
  13.     newNode->data = dat;
  14.     newNode->prev = NULL;
  15.     newNode->next = NULL;
  16.     return newNode;
  17. }
  18.  
  19. typedef struct List {
  20.     struct Node* head;
  21.     struct Node* tail;
  22. } list;
  23.  
  24. #define LISTSIZE sizeof(struct List)
  25.  
  26. struct List* list_init(void) {
  27.     struct List* newList = (struct List*)malloc(LISTSIZE);
  28.     newList->head = NULL;
  29.     newList->tail = NULL;
  30.     return newList;
  31. }
  32.  
  33. bool list_is_empty(struct List* l) {
  34.     return l->head == NULL;
  35. }
  36.  
  37. int list_get_length(struct List* l) {
  38.     if (list_is_empty(l)) {
  39.         return 0;
  40.     }
  41.  
  42.     if (l->tail == NULL) {
  43.         return 1;
  44.     }
  45.  
  46.     int ecx = 0;
  47.     struct Node* i = l->head;
  48.     while (i->next != NULL) {
  49.         ecx++;
  50.         i = i->next;
  51.     }
  52.     return ecx;
  53. }
  54.  
  55. void list_insert_between(struct Node* before, struct Node* newNode, struct Node* after) {
  56.     newNode->next = after;
  57.     newNode->prev = before;
  58.  
  59.     before->next = newNode;
  60.     after->prev = newNode;
  61.     return;
  62. }
  63.  
  64. void list_append(struct List* l, int dat) {
  65.     struct Node* newNode = node_init(dat);
  66.  
  67.     if (list_is_empty(l)) {
  68.         l->head = newNode;
  69.         return;
  70.     }
  71.  
  72.     if (l->tail == NULL) {
  73.         newNode->prev = l->head;
  74.         l->head->next = newNode;
  75.         l->tail = newNode;
  76.         return;
  77.     }
  78.  
  79.     l->tail->next = newNode;
  80.     newNode->prev = l->tail;
  81.     l->tail = newNode;
  82.     return;
  83. }
  84.  
  85. void list_prepend(struct List* l, int dat) {
  86.     struct Node* newNode = node_init(dat);
  87.  
  88.     if (list_is_empty(l)) {
  89.         l->head = newNode;
  90.         return;
  91.     }
  92.  
  93.     l->head->prev = newNode;
  94.     newNode->next = l->head;
  95.     l->head = newNode;
  96.     return;
  97. }
  98.  
  99. void list_destroy(struct List* l) {
  100.     struct Node* h = l->head;
  101.     struct Node* curr = l->head;
  102.     struct Node* next;
  103.  
  104.     while (next != NULL) {
  105.         next = curr->next;
  106.         free(curr);
  107.         curr = next;
  108.     }
  109.  
  110.     free(l);
  111.     return;
  112. }
  113.  
  114. void list_print(struct List* l) {
  115.     if (list_is_empty(l)) {
  116.         printf("[lst prnt] [  ]\n");
  117.         return;
  118.     }
  119.  
  120.     struct Node* curr = l->head;
  121.  
  122.     printf("[lst prnt] [ ");
  123.  
  124.     while (curr != NULL) {
  125.         printf("%d ", curr->data);
  126.         curr = curr->next;
  127.     }
  128.  
  129.     printf("]\n");
  130.     return;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement