Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.09 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdbool.h>
  5.  
  6.  
  7. typedef struct node {
  8.     void* data;
  9.     void* next;
  10.     struct node* previous;
  11. } node;
  12.  
  13. typedef struct double_linked_list {
  14.     node* start;
  15.     node* end;
  16.     size_t size;
  17.     size_t objsize;
  18. } double_linked_list;
  19.  
  20. void create_list(double_linked_list* list, size_t objsize) {
  21.     list->start = NULL;
  22.     list->objsize = objsize;
  23. }
  24.  
  25. bool get_next(node** node) {
  26.     if(!(*node)->next) {
  27.         *node = NULL;
  28.         return false;
  29.     }
  30.     *node = (*node)->next;
  31.     return true;
  32. }
  33.  
  34. bool get_previous(node** node) {
  35.     if(!(*node)->previous) {
  36.         *node = NULL;
  37.         return false;
  38.     }
  39.     *node = (*node)->previous;
  40.     return true;
  41. }
  42.  
  43. node* push_front(double_linked_list* list, void* obj) {
  44.     node* prev = list->start;
  45.     list->start = malloc(sizeof(node));
  46.     list->start->data = malloc(list->objsize);
  47.     memcpy(list->start->data, obj, list->objsize);
  48.     list->start->next = prev;
  49.     list->start->previous = NULL;
  50.     if(prev != NULL) {
  51.         prev->previous = list->start;
  52.     } else {
  53.         list->end = list->start;
  54.     }
  55.     return list->start;
  56. }
  57.  
  58. node* push_back(double_linked_list* list, void* obj) {
  59.     node* prev = list->end;
  60.     list->end = malloc(sizeof(node));
  61.     list->end->data = malloc(list->objsize);
  62.     memcpy(list->end->data, obj, list->objsize);
  63.     list->end->next = NULL;
  64.     list->end->previous = prev;
  65.     if(prev != NULL) {
  66.         prev->next = list->end;
  67.     } else {
  68.         list->start = list->end;
  69.     }
  70.     return list->end;
  71. }
  72.  
  73. void free_list(double_linked_list* list) {
  74.     node* iterator = list->start;
  75.     while(iterator) {
  76.         node* cur = iterator;
  77.         free(cur->data);
  78.         get_next(&iterator);
  79.         free(cur);
  80.     }
  81. }
  82.  
  83.  
  84. int main() {
  85.     double_linked_list list;
  86.     create_list(&list, sizeof(int));
  87.     int vals[] = { 1, 2, 3, 4, 5 };
  88.     for(int i = sizeof(vals) / sizeof(int) - 1; i >= 0; i--) {
  89.         push_back(&list, &vals[i]);
  90.     }
  91.  
  92.     node* iterator = list.start;
  93.     while(iterator) {
  94.         printf("%d\n", *(int*)iterator->data);
  95.         get_next(&iterator);
  96.     }
  97.  
  98.     iterator = list.end;
  99.     while(iterator) {
  100.         printf("%d\n", *(int*)iterator->data);
  101.         get_previous(&iterator);
  102.     }
  103.     free_list(&list);
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement