RyanList.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include "RyanList.h"
  2. /**
  3. * @brief Inserts a node between two existing nodes in a doubly linked list.
  4. *
  5. * This internal static function updates the pointers of the given nodes to insert `node`
  6. * between `prev` and `next`. It does not perform any validation on the input pointers.
  7. *
  8. * @param node The node to insert.
  9. * @param prev The node that will precede the inserted node.
  10. * @param next The node that will follow the inserted node.
  11. */
  12. static void _RyanListAdd(RyanList_t *node, RyanList_t *prev, RyanList_t *next)
  13. {
  14. next->prev = node; // 后继节点的前驱指向新节点
  15. node->next = next; // 新节点的后继指向原后继节点
  16. node->prev = prev; // 新节点的前驱指向原前驱节点
  17. prev->next = node; // 前驱节点的后继指向新节点
  18. }
  19. /**
  20. * @brief Unlinks all nodes between two given nodes in a doubly linked list.
  21. *
  22. * Adjusts the pointers of the specified predecessor and successor nodes so that they point to each other, effectively removing any nodes between them from the list.
  23. */
  24. static void _RyanListDel(RyanList_t *prev, RyanList_t *next)
  25. {
  26. prev->next = next; // 前驱节点直接指向后继节点
  27. next->prev = prev; // 后继节点直接指向前驱节点
  28. }
  29. /**
  30. * @brief Removes a specific node from the doubly linked list.
  31. *
  32. * This internal function unlinks the given node from its neighboring nodes, effectively deleting it from the list.
  33. *
  34. * @param entry Pointer to the node to be removed.
  35. */
  36. static void _RyanListDel_entry(RyanList_t *entry)
  37. {
  38. _RyanListDel(entry->prev, entry->next); // 调用区间删除函数
  39. }
  40. /**
  41. * @brief Initializes a list head node to represent an empty doubly linked list.
  42. *
  43. * Sets the head node's next and previous pointers to point to itself, marking the list as empty.
  44. */
  45. void RyanListInit(RyanList_t *list)
  46. {
  47. list->next = list; // 后继指向自己
  48. list->prev = list; // 前驱指向自己
  49. }
  50. /**
  51. * @brief Inserts a node at the beginning of the list, immediately after the head.
  52. *
  53. * The new node becomes the first valid element in the doubly linked list.
  54. */
  55. void RyanListAdd(RyanList_t *node, RyanList_t *list)
  56. {
  57. _RyanListAdd(node, list, list->next); // 在头节点和第一个节点间插入
  58. }
  59. /**
  60. * @brief Inserts a node at the end of the doubly linked list.
  61. *
  62. * The new node is added immediately before the list head, making it the last valid node in the list.
  63. */
  64. void RyanListAddTail(RyanList_t *node, RyanList_t *list)
  65. {
  66. _RyanListAdd(node, list->prev, list); // 在尾节点和头节点间插入
  67. }
  68. /**
  69. * @brief Removes a specified node from the doubly linked list.
  70. *
  71. * The node is unlinked from its current list but is not reinitialized; its pointers remain unchanged after removal.
  72. */
  73. void RyanListDel(RyanList_t *entry)
  74. {
  75. _RyanListDel_entry(entry); // 调用内部删除函数
  76. }
  77. /**
  78. * @brief Removes a node from the list and reinitializes it as a standalone node.
  79. *
  80. * After removal, the node's `next` and `prev` pointers are set to point to itself, making it an isolated node.
  81. */
  82. void RyanListDelInit(RyanList_t *entry)
  83. {
  84. _RyanListDel_entry(entry); // 先删除节点
  85. RyanListInit(entry); // 再重新初始化该节点
  86. }
  87. /**
  88. * @brief Moves a node to the front of the list.
  89. *
  90. * Removes the specified node from its current position and inserts it immediately after the list head, making it the first element in the list.
  91. */
  92. void RyanListMove(RyanList_t *node, RyanList_t *list)
  93. {
  94. _RyanListDel_entry(node); // 先从原位置删除
  95. RyanListAdd(node, list); // 再插入到头部
  96. }
  97. /**
  98. * @brief Moves a node to the end of the doubly linked list.
  99. *
  100. * Removes the specified node from its current position and inserts it immediately before the list head, making it the last node in the list.
  101. *
  102. * @param node The node to move.
  103. * @param list The list head node.
  104. */
  105. void RyanListMoveTail(RyanList_t *node, RyanList_t *list)
  106. {
  107. _RyanListDel_entry(node); // 先从原位置删除
  108. RyanListAddTail(node, list); // 再插入到尾部
  109. }
  110. /**
  111. * @brief Checks whether the list is empty.
  112. *
  113. * Determines if the given list head node represents an empty list by checking if its `next` pointer points to itself.
  114. *
  115. * @param list Pointer to the list head node.
  116. * @return int Returns 1 if the list is empty, 0 otherwise.
  117. */
  118. int RyanListIsEmpty(RyanList_t *list)
  119. {
  120. return list->next == list; // 头节点的next指向自己说明为空
  121. }