utils_list.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (C) 2012-2019 UCloud. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #ifndef C_SDK_UTILS_LIST_H_
  16. #define C_SDK_UTILS_LIST_H_
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. #include <stdlib.h>
  21. /*
  22. * ListNode迭代器的方向.
  23. */
  24. typedef enum {
  25. LIST_HEAD,
  26. LIST_TAIL
  27. } ListDirection;
  28. /*
  29. * 链表节点定义.
  30. */
  31. typedef struct ListNode {
  32. struct ListNode *prev;
  33. struct ListNode *next;
  34. void *val;
  35. } ListNode;
  36. /*
  37. * 链表.
  38. */
  39. typedef struct {
  40. ListNode *head;
  41. ListNode *tail;
  42. unsigned int len;
  43. void (*free)(void *val);
  44. int (*match)(void *a, void *b);
  45. } List;
  46. /*
  47. * 迭代器.
  48. */
  49. typedef struct {
  50. ListNode *next;
  51. ListDirection direction;
  52. } ListIterator;
  53. /* 节点. */
  54. ListNode *list_node_new(void *val);
  55. /* 链表. */
  56. List *list_new(void);
  57. ListNode *list_rpush(List *self, ListNode *node);
  58. ListNode *list_lpush(List *self, ListNode *node);
  59. ListNode *list_find(List *self, void *val);
  60. ListNode *list_at(List *self, int index);
  61. ListNode *list_rpop(List *self);
  62. ListNode *list_lpop(List *self);
  63. void list_remove(List *self, ListNode *node);
  64. void list_destroy(List *self);
  65. /* 迭代器. */
  66. ListIterator *list_iterator_new(List *list, ListDirection direction);
  67. ListIterator *list_iterator_new_from_node(ListNode *node, ListDirection direction);
  68. ListNode *list_iterator_next(ListIterator *self);
  69. void list_iterator_destroy(ListIterator *self);
  70. #ifdef __cplusplus
  71. }
  72. #endif
  73. #endif //C_SDK_UTILS_LIST_H_