Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include "list.h"
  3.  
  4. struct ListItem
  5. {
  6. Data item;
  7. ListItem* next, * prev;
  8. };
  9.  
  10. struct List
  11. {
  12. ListItem* start;
  13. };
  14.  
  15. List* list_create()
  16. {
  17. List* temp = new List;
  18. temp->start = NULL;
  19. return temp;
  20. }
  21.  
  22. void list_delete(List* list)
  23. {
  24. list->start->prev->next = NULL;
  25. ListItem* temp = list->start;
  26. while (temp != NULL)
  27. {
  28. temp = list_item_next(list->start);
  29. delete list->start;
  30. list->start = temp;
  31. }
  32. delete list;
  33. }
  34.  
  35. ListItem* list_first(List* list)
  36. {
  37. return list->start;
  38. }
  39.  
  40. Data list_item_data(const ListItem* item)
  41. {
  42. return item->item;
  43. }
  44.  
  45. ListItem* list_item_next(ListItem* item)
  46. {
  47. return item->next;
  48. }
  49.  
  50. ListItem* list_item_prev(ListItem* item)
  51. {
  52. return item->prev;
  53. }
  54.  
  55. ListItem* list_insert(List* list, Data data)
  56. {
  57. if (list->start == NULL) {
  58. list->start->next = list->start->prev = list->start = new ListItem;;
  59. }
  60. else {
  61. ListItem* head = list->start;
  62. list->start = new ListItem;
  63.  
  64. list->start->prev = head->prev;
  65. list->start->next = head;
  66. head->prev->next = list->start;
  67. head->prev = list->start;
  68. }
  69. list->start->item = data;
  70. return list->start;
  71. }
  72.  
  73. ListItem* list_insert_after(List* list, ListItem* item, Data data)
  74. {
  75. ListItem* head = item->next;
  76. item->next = new ListItem;
  77. item->next->item = data;
  78.  
  79. head->prev = item->next;
  80. item->next->next = head;
  81. item->next->prev = item;
  82. return item->next;
  83. }
  84.  
  85. ListItem* list_erase(List* list, ListItem* item)
  86. {
  87. ListItem* temp_next = item->next;
  88. ListItem* temp_prev = item->prev;
  89. if (list->start == list->start->prev) {
  90. delete item;
  91. list->start = NULL;
  92. return list->start;
  93. }
  94. else {
  95. delete item;
  96. if (item == list->start) {
  97. list->start = temp_next;
  98. }
  99. temp_next->prev = temp_prev;
  100. temp_prev->next = temp_next;
  101. }
  102. return temp_next;
  103. }
  104.  
  105. ListItem* list_erase_next(List* list, ListItem* item)
  106. {
  107. return list_erase(list, item->next);
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement