doublylinkedlist.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*******************************************************************************
  2. * Copyright (c) 2017, Rockwell Automation, Inc.
  3. * All rights reserved.
  4. *
  5. ******************************************************************************/
  6. #include "doublylinkedlist.h"
  7. #include "opener_user_conf.h"
  8. #include <stdio.h> // Needed to define NULL
  9. #include <assert.h>
  10. void DoublyLinkedListInitialize(DoublyLinkedList *list,
  11. NodeMemoryAllocator allocator,
  12. NodeMemoryDeallocator deallocator) {
  13. list->first = NULL;
  14. list->last = NULL;
  15. list->allocator = allocator;
  16. list->deallocator = deallocator;
  17. }
  18. void DoublyLinkedListDestroy(DoublyLinkedList *list) {
  19. DoublyLinkedListNode *iterator = list->first;
  20. while(NULL != iterator) {
  21. DoublyLinkedListNode *to_delete = iterator;
  22. iterator = iterator->next;
  23. DoublyLinkedListNodeDestroy(list, &to_delete);
  24. }
  25. list->first = NULL;
  26. list->last = NULL;
  27. list->allocator = NULL;
  28. list->deallocator = NULL;
  29. }
  30. DoublyLinkedListNode *DoublyLinkedListNodeCreate(
  31. const void *const data,
  32. NodeMemoryAllocator const
  33. allocator) {
  34. DoublyLinkedListNode *new_node = (DoublyLinkedListNode *)allocator();
  35. new_node->data = (void *)data;
  36. return new_node;
  37. }
  38. void DoublyLinkedListNodeDestroy(const DoublyLinkedList *const list,
  39. DoublyLinkedListNode **node) {
  40. OPENER_ASSERT(list->deallocator != NULL);
  41. list->deallocator(node);
  42. }
  43. void DoublyLinkedListInsertAtHead(DoublyLinkedList *const list,
  44. const void *const data) {
  45. OPENER_ASSERT(list->allocator != NULL);
  46. DoublyLinkedListNode *new_node = DoublyLinkedListNodeCreate(data,
  47. list->allocator);
  48. if(NULL == list->first) {
  49. list->first = new_node;
  50. list->last = new_node;
  51. } else {
  52. new_node->next = list->first;
  53. list->first->previous = new_node;
  54. list->first = new_node;
  55. }
  56. }
  57. void DoublyLinkedListInsertAtTail(DoublyLinkedList *const list,
  58. const void *const data) {
  59. OPENER_ASSERT(list->allocator != NULL);
  60. DoublyLinkedListNode *new_node = DoublyLinkedListNodeCreate(data,
  61. list->allocator);
  62. if(NULL == list->last) {
  63. list->first = new_node;
  64. list->last = new_node;
  65. } else {
  66. new_node->previous = list->last;
  67. list->last->next = new_node;
  68. list->last = new_node;
  69. }
  70. }
  71. void DoublyLinkedListInsertBeforeNode(DoublyLinkedList *const list,
  72. DoublyLinkedListNode *node,
  73. void *data) {
  74. OPENER_ASSERT(list->allocator != NULL);
  75. if(list->first == node) {
  76. DoublyLinkedListInsertAtHead(list, data);
  77. } else {
  78. DoublyLinkedListNode *new_node = DoublyLinkedListNodeCreate(data,
  79. list->allocator);
  80. new_node->previous = node->previous;
  81. new_node->next = node;
  82. node->previous = new_node;
  83. new_node->previous->next = new_node;
  84. }
  85. }
  86. void DoublyLinkedListInsertAfterNode(DoublyLinkedList *const list,
  87. DoublyLinkedListNode *node,
  88. void *data) {
  89. OPENER_ASSERT(list->allocator != NULL);
  90. if(list->last == node) {
  91. DoublyLinkedListInsertAtTail(list, data);
  92. } else {
  93. DoublyLinkedListNode *new_node = DoublyLinkedListNodeCreate(data,
  94. list->allocator);
  95. new_node->previous = node;
  96. new_node->next = node->next;
  97. node->next->previous = new_node;
  98. node->next = new_node;
  99. }
  100. }
  101. void DoublyLinkedListRemoveNode(DoublyLinkedList *const list,
  102. DoublyLinkedListNode **pointer_to_node_pointer)
  103. {
  104. DoublyLinkedListNode *node = *pointer_to_node_pointer;
  105. DoublyLinkedListNode *previous = node->previous;
  106. DoublyLinkedListNode *next = node->next;
  107. if (node == list->first && node == list->last) {
  108. list->first = NULL;
  109. list->last = NULL;
  110. } else {
  111. if(node == list->first) {
  112. list->first = next;
  113. }
  114. if(node == list->last) {
  115. list->last = previous;
  116. }
  117. if(NULL != previous) {
  118. previous->next = next;
  119. }
  120. if(NULL != next) {
  121. next->previous = previous;
  122. }
  123. }
  124. DoublyLinkedListNodeDestroy(list, pointer_to_node_pointer);
  125. }