关键词搜索

源码搜索 ×
×

漫话Redis源码之八十六

发布2022-02-20浏览557次

详情内容

这里主要是list相关的操作,相信熟悉数据结构的人肯定不陌生,这也是笔试面试常考的内容哦:

  1. /* Create a new list. The created list can be freed with
  2. * listRelease(), but private value of every node need to be freed
  3. * by the user before to call listRelease(), or by setting a free method using
  4. * listSetFreeMethod.
  5. *
  6. * On error, NULL is returned. Otherwise the pointer to the new list. */
  7. list *listCreate(void)
  8. {
  9. struct list *list;
  10. if ((list = zmalloc(sizeof(*list))) == NULL)
  11. return NULL;
  12. list->head = list->tail = NULL;
  13. list->len = 0;
  14. list->dup = NULL;
  15. list->free = NULL;
  16. list->match = NULL;
  17. return list;
  18. }
  19. /* Remove all the elements from the list without destroying the list itself. */
  20. void listEmpty(list *list)
  21. {
  22. unsigned long len;
  23. listNode *current, *next;
  24. current = list->head;
  25. len = list->len;
  26. while(len--) {
  27. next = current->next;
  28. if (list->free) list->free(current->value);
  29. zfree(current);
  30. current = next;
  31. }
  32. list->head = list->tail = NULL;
  33. list->len = 0;
  34. }
  35. /* Free the whole list.
  36. *
  37. * This function can't fail. */
  38. void listRelease(list *list)
  39. {
  40. listEmpty(list);
  41. zfree(list);
  42. }
  43. /* Add a new node to the list, to head, containing the specified 'value'
  44. * pointer as value.
  45. *
  46. * On error, NULL is returned and no operation is performed (i.e. the
  47. * list remains unaltered).
  48. * On success the 'list' pointer you pass to the function is returned. */
  49. list *listAddNodeHead(list *list, void *value)
  50. {
  51. listNode *node;
  52. if ((node = zmalloc(sizeof(*node))) == NULL)
  53. return NULL;
  54. node->value = value;
  55. if (list->len == 0) {
  56. list->head = list->tail = node;
  57. node->prev = node->next = NULL;
  58. } else {
  59. node->prev = NULL;
  60. node->next = list->head;
  61. list->head->prev = node;
  62. list->head = node;
  63. }
  64. list->len++;
  65. return list;
  66. }
  67. /* Add a new node to the list, to tail, containing the specified 'value'
  68. * pointer as value.
  69. *
  70. * On error, NULL is returned and no operation is performed (i.e. the
  71. * list remains unaltered).
  72. * On success the 'list' pointer you pass to the function is returned. */
  73. list *listAddNodeTail(list *list, void *value)
  74. {
  75. listNode *node;
  76. if ((node = zmalloc(sizeof(*node))) == NULL)
  77. return NULL;
  78. node->value = value;
  79. if (list->len == 0) {
  80. list->head = list->tail = node;
  81. node->prev = node->next = NULL;
  82. } else {
  83. node->prev = list->tail;
  84. node->next = NULL;
  85. list->tail->next = node;
  86. list->tail = node;
  87. }
  88. list->len++;
  89. return list;
  90. }
  91. list *listInsertNode(list *list, listNode *old_node, void *value, int after) {
  92. listNode *node;
  93. if ((node = zmalloc(sizeof(*node))) == NULL)
  94. return NULL;
  95. node->value = value;
  96. if (after) {
  97. node->prev = old_node;
  98. node->next = old_node->next;
  99. if (list->tail == old_node) {
  100. list->tail = node;
  101. }
  102. } else {
  103. node->next = old_node;
  104. node->prev = old_node->prev;
  105. if (list->head == old_node) {
  106. list->head = node;
  107. }
  108. }
  109. if (node->prev != NULL) {
  110. node->prev->next = node;
  111. }
  112. if (node->next != NULL) {
  113. node->next->prev = node;
  114. }
  115. list->len++;
  116. return list;
  117. }
  118. /* Remove the specified node from the specified list.
  119. * It's up to the caller to free the private value of the node.
  120. *
  121. * This function can't fail. */
  122. void listDelNode(list *list, listNode *node)
  123. {
  124. if (node->prev)
  125. node->prev->next = node->next;
  126. else
  127. list->head = node->next;
  128. if (node->next)
  129. node->next->prev = node->prev;
  130. else
  131. list->tail = node->prev;
  132. if (list->free) list->free(node->value);
  133. zfree(node);
  134. list->len--;
  135. }
  136. /* Returns a list iterator 'iter'. After the initialization every
  137. * call to listNext() will return the next element of the list.
  138. *
  139. * This function can't fail. */
  140. listIter *listGetIterator(list *list, int direction)
  141. {
  142. listIter *iter;
  143. if ((iter = zmalloc(sizeof(*iter))) == NULL) return NULL;
  144. if (direction == AL_START_HEAD)
  145. iter->next = list->head;
  146. else
  147. iter->next = list->tail;
  148. iter->direction = direction;
  149. return iter;
  150. }
  151. /* Release the iterator memory */
  152. void listReleaseIterator(listIter *iter) {
  153. zfree(iter);
  154. }
  155. /* Create an iterator in the list private iterator structure */
  156. void listRewind(list *list, listIter *li) {
  157. li->next = list->head;
  158. li->direction = AL_START_HEAD;
  159. }
  160. void listRewindTail(list *list, listIter *li) {
  161. li->next = list->tail;
  162. li->direction = AL_START_TAIL;
  163. }
  164. /* Return the next element of an iterator.
  165. * It's valid to remove the currently returned element using
  166. * listDelNode(), but not to remove other elements.
  167. *
  168. * The function returns a pointer to the next element of the list,
  169. * or NULL if there are no more elements, so the classical usage
  170. * pattern is:
  171. *
  172. * iter = listGetIterator(list,<direction>);
  173. * while ((node = listNext(iter)) != NULL) {
  174. * doSomethingWith(listNodeValue(node));
  175. * }
  176. *
  177. * */
  178. listNode *listNext(listIter *iter)
  179. {
  180. listNode *current = iter->next;
  181. if (current != NULL) {
  182. if (iter->direction == AL_START_HEAD)
  183. iter->next = current->next;
  184. else
  185. iter->next = current->prev;
  186. }
  187. return current;
  188. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载