找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1183|回复: 0
打印 上一主题 下一主题
收起左侧

可移植的链表算法源码

[复制链接]
跳转到指定楼层
楼主
ID:315185 发表于 2018-4-24 11:34 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
可移植的链表算法。

单片机源程序如下:
  1. #ifndef _LINUX_LIST_H
  2. #define _LINUX_LIST_H

  3. #include <stdio.h>

  4. #ifndef offsetof
  5. /**
  6. * Get offset of a member
  7. */
  8. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  9. #endif

  10. #ifndef container_of
  11. /**
  12. * Casts a member of a structure out to the containing structure
  13. * @param ptr        the pointer to the member.
  14. * @param type       the type of the container struct this is embedded in.
  15. * @param member     the name of the member within the struct.
  16. *
  17. */
  18. #define container_of(ptr, type, member) ({                      \
  19.         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
  20.                 (type *)( (char *)__mptr - offsetof(type,member) );})
  21. #endif

  22. /*
  23. * These are non-NULL pointers that will result in page faults
  24. * under normal circumstances, used to verify that nobody uses
  25. * non-initialized list entries.
  26. */
  27. #define LIST_POISON1  ((void *) 0x00100100)
  28. #define LIST_POISON2  ((void *) 0x00200200)

  29. struct list_head {
  30.         struct list_head *next, *prev;
  31. };

  32. #define LIST_HEAD_INIT(name) { &(name), &(name) }

  33. #define LIST_HEAD(name) \
  34.         struct list_head name = LIST_HEAD_INIT(name)

  35. static inline void INIT_LIST_HEAD(struct list_head *list)
  36. {
  37.         list->next = list;
  38.         list->prev = list;
  39. }

  40. /*
  41. * Insert a new entry between two known consecutive entries.
  42. *
  43. * This is only for internal list manipulation where we know
  44. * the prev/next entries already!
  45. */
  46. static inline void __list_add(struct list_head *new,
  47.                               struct list_head *prev,
  48.                               struct list_head *next)
  49. {
  50.         next->prev = new;
  51.         new->next = next;
  52.         new->prev = prev;
  53.         prev->next = new;
  54. }

  55. /**
  56. * list_add - add a new entry
  57. * @new: new entry to be added
  58. * @head: list head to add it after
  59. *
  60. * Insert a new entry after the specified head.
  61. * This is good for implementing stacks.
  62. */
  63. static inline void list_add(struct list_head *new, struct list_head *head)
  64. {
  65.         __list_add(new, head, head->next);
  66. }

  67. /**
  68. * list_add_tail - add a new entry
  69. * @new: new entry to be added
  70. * @head: list head to add it before
  71. *
  72. * Insert a new entry before the specified head.
  73. * This is useful for implementing queues.
  74. */
  75. static inline void list_add_tail(struct list_head *new, struct list_head *head)
  76. {
  77.         __list_add(new, head->prev, head);
  78. }

  79. /*
  80. * Delete a list entry by making the prev/next entries
  81. * point to each other.
  82. *
  83. * This is only for internal list manipulation where we know
  84. * the prev/next entries already!
  85. */
  86. static inline void __list_del(struct list_head * prev, struct list_head * next)
  87. {
  88.         next->prev = prev;
  89.         prev->next = next;
  90. }

  91. /**
  92. * list_del - deletes entry from list.
  93. * @entry: the element to delete from the list.
  94. * Note: list_empty on entry does not return true after this, the entry is
  95. * in an undefined state.
  96. */
  97. static inline void list_del(struct list_head *entry)
  98. {
  99.         __list_del(entry->prev, entry->next);
  100.         entry->next = LIST_POISON1;
  101.         entry->prev = LIST_POISON2;
  102. }

  103. /**
  104. * __list_for_each        -        iterate over a list
  105. * @pos:        the &struct list_head to use as a loop counter.
  106. * @head:        the head for your list.
  107. *
  108. * This variant differs from list_for_each() in that it's the
  109. * simplest possible list iteration code, no prefetching is done.
  110. * Use this for code that knows the list to be very short (empty
  111. * or 1 entry) most of the time.
  112. */
  113. #define __list_for_each(pos, head) \
  114.         for (pos = (head)->next; pos != (head); pos = pos->next)

  115. /**
  116. * list_for_each_safe        -        iterate over a list safe against removal of list entry
  117. * @pos:        the &struct list_head to use as a loop counter.
  118. * @n:                another &struct list_head to use as temporary storage
  119. * @head:        the head for your list.
  120. */
  121. #define list_for_each_safe(pos, n, head) \
  122.         for (pos = (head)->next, n = pos->next; pos != (head); \
  123.                 pos = n, n = pos->next)

  124. /**
  125. * list_entry - get the struct for this entry
  126. * @ptr:        the &struct list_head pointer.
  127. * @type:       the type of the struct this is embedded in.
  128. * @member:     the name of the list_struct within the struct.
  129. */
  130. #define list_entry(ptr, type, member) \
  131.         container_of(ptr, type, member)

  132. static inline int list_len(struct list_head *head_p)
  133. {
  134.         struct list_head *p;
  135.         int n = 0;

  136.         __list_for_each(p, head_p) {
  137.                 n++;
  138.         }

  139.         return n;
  140. }

  141. /**
  142. * list_empty - tests whether a list is empty
  143. * @head: the list to test.
  144. */
  145. static inline int list_empty(const struct list_head *head)
  146. {
  147.         return head->next == head;
  148. }

  149. /**
  150. * list_first - Returns first entry on list, or NULL if empty
  151. * @head: the list
  152. */
  153. static inline struct list_head *list_first(const struct list_head *head)
  154. {
  155.         return list_empty(head) ? NULL : head->next;
  156. }

  157. /**
  158. * list_move_tail - delete from one list and add as another's tail
  159. * @list: the entry to move
  160. * @head: the head that will follow our entry
  161. */
  162. static inline void list_move_tail(struct list_head *list,
  163.                                   struct list_head *head)
  164. {
  165.         __list_del(list->prev, list->next);
  166.         list_add_tail(list, head);
  167. }

  168. static inline void __list_splice(struct list_head *list,
  169.                                  struct list_head *head)
  170. {
  171.         struct list_head *first = list->next;
  172.         struct list_head *last = list->prev;
  173.         struct list_head *at = head->next;

  174.         first->prev = head;
  175.         head->next = first;

  176.         last->next = at;
  177.         at->prev = last;
  178. }

  179. /**
  180. *  * list_splice - join two lists
  181. *   * @list: the new list to add.
  182. *    * @head: the place to add it in the first list.
  183. *     */
  184. static inline void list_splice(struct list_head *list, struct list_head *head)
  185. {
  186.         if (!list_empty(list))
  187.                 __list_splice(list, head);
  188. }

  189. /**
  190. * list_replace - replace old entry by new one
  191. * @old : the element to be replaced
  192. * @new : the new element to insert
  193. *
  194. * If @old was empty, it will be overwritten.
  195. */
  196. static inline void list_replace(struct list_head *old,
  197.                                 struct list_head *new)
  198. {
  199.         new->next = old->next;
  200.         new->next->prev = new;
  201.         new->prev = old->prev;
  202.         new->prev->next = new;
  203. }

  204. static inline void list_replace_init(struct list_head *old,
  205.                                         struct list_head *new)
  206. {
  207.         list_replace(old, new);
  208.         INIT_LIST_HEAD(old);
  209. }

  210. #endif
复制代码

所有资料51hei提供下载:
list.7z (1.94 KB, 下载次数: 10)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表