Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. //
  2. // Created by Mi on 24.11.2019.
  3. //
  4.  
  5. #ifndef LINKEDLIST_LINKEDLIST_H
  6. #define LINKEDLIST_LINKEDLIST_H
  7.  
  8. #include <stdlib.h>
  9.  
  10. typedef struct Node node;
  11. typedef struct LinkedList List;
  12.  
  13. typedef node* iterator;
  14.  
  15. struct Node
  16. {
  17. node* next;
  18. node* prev;
  19.  
  20. int _data;
  21. };
  22.  
  23. struct LinkedList
  24. {
  25. node* head;
  26. node* tail;
  27.  
  28. iterator iter;
  29. int length;
  30. };
  31.  
  32. List* CreateList(int t_data)
  33. {
  34. List *list = malloc(sizeof(List));
  35. list->head = malloc(sizeof(node));
  36. list->tail = list->iter = list->head;
  37.  
  38. list->iter->_data = t_data;
  39. list->iter->next = list->iter->prev = NULL;
  40.  
  41. list->length = 1;
  42. return list;
  43. }
  44.  
  45. node* CreateNode(int t_data)
  46. {
  47. iterator new_node = malloc(sizeof(node));
  48. new_node->prev = new_node->next = NULL;
  49. new_node->_data = t_data;
  50.  
  51. return new_node;
  52. }
  53.  
  54. iterator begin(List *list)
  55. {
  56. return list->head;
  57. }
  58.  
  59. iterator end(List *list)
  60. {
  61. return list->tail;
  62. }
  63.  
  64. iterator get_iterator(List *list, int t_index)
  65. {
  66. list->iter = list->head;
  67. for(int i = 0; i < t_index; ++i, list->iter = list->iter->next)
  68. ;
  69. return list->iter;
  70. }
  71.  
  72. void push_back(List *list, int t_data)
  73. {
  74. if (!list)
  75. {
  76. puts("List nullptr");
  77. return;
  78. }
  79. if (!list->head->next)
  80. {
  81. list->tail = CreateNode(t_data);
  82. list->length++;
  83.  
  84. list->head->next = list->tail;
  85. list->tail->prev = list->head;
  86. return;
  87. }
  88. list->iter = end(list);
  89. list->tail = CreateNode(t_data);
  90. list->iter->next = end(list);
  91. list->tail->prev = list->iter;
  92.  
  93. list->length++;
  94. }
  95.  
  96. void push_front(List *list, int t_data)
  97. {
  98. if (!list)
  99. {
  100. puts("List nullptr");
  101. return;
  102. }
  103. node* new_node = CreateNode(t_data);
  104. new_node->prev = NULL;
  105. new_node->next = begin(list);
  106. list->head->prev = new_node;
  107. list->head = new_node;
  108.  
  109. list->length++;
  110. }
  111.  
  112. void insert(List *list, int t_data, int t_index)
  113. {
  114. if (!list)
  115. {
  116. puts("List nullptr");
  117. return;
  118. }
  119. if (t_index > list->length || t_index < 0)
  120. {
  121. puts("Out of range!");
  122. return;
  123. }
  124.  
  125. if (!t_index)
  126. {
  127. push_front(list, t_data);
  128. return;
  129. }
  130. if (t_index == list->length)
  131. {
  132. push_back(list, t_data);
  133. return;
  134. }
  135.  
  136. list->iter = begin(list);
  137. for (int i = 0; i < t_index - 1; ++i, list->iter = list->iter->next)
  138. ;
  139. node* new_node = CreateNode(t_data);
  140. new_node->next = list->iter->next;
  141. new_node->prev = list->iter;
  142. list->iter->next = new_node;
  143.  
  144. list->length++;
  145. }
  146.  
  147. #endif //LINKEDLIST_LINKEDLIST_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement