学习/linux/list.h_双链表实现

Posted Jason_linux

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了学习/linux/list.h_双链表实现相关的知识,希望对你有一定的参考价值。

 linux-3.5/include/linux/list.h

使用只含指针域的双向循环链表进行链表的操作。

下面是我选取部分list.h中代码:

  1 #ifndef _LINUX_LIST_H
  2 #define _LINUX_LIST_H
  3 
  4 struct list_head {
  5     struct list_head *next, *prev;
  6 };
  7 /*
  8  * Simple doubly linked list implementation.
  9  *
 10  * Some of the internal functions ("__xxx") are useful when
 11  * manipulating whole lists rather than single entries, as
 12  * sometimes we already know the next/prev entries and we can
 13  * generate better code by using them directly rather than
 14  * using the generic single-entry routines.
 15  */
 16 
 17 #define LIST_HEAD_INIT(name) { &(name), &(name) }
 18 
 19 #define LIST_HEAD(name) \\
 20     struct list_head name = LIST_HEAD_INIT(name)
 21 
 22 static inline void INIT_LIST_HEAD(struct list_head *list)
 23 {
 24     list->next = list;
 25     list->prev = list;
 26 }
 27 
 28 /*
 29  * Insert a new entry between two known consecutive entries.
 30  *
 31  * This is only for internal list manipulation where we know
 32  * the prev/next entries already!
 33  */
 34 #ifndef CONFIG_DEBUG_LIST
 35 static inline void __list_add(struct list_head *new,
 36                   struct list_head *prev,
 37                   struct list_head *next)
 38 {
 39     next->prev = new;
 40     new->next = next;
 41     new->prev = prev;
 42     prev->next = new;
 43 }
 44 #else
 45 extern void __list_add(struct list_head *new,
 46                   struct list_head *prev,
 47                   struct list_head *next);
 48 #endif
 49 
 50 /**
 51  * list_add - add a new entry
 52  * @new: new entry to be added
 53  * @head: list head to add it after
 54  *
 55  * Insert a new entry after the specified head.
 56  * This is good for implementing stacks.
 57  */
 58 static inline void list_add(struct list_head *new, struct list_head *head)
 59 {
 60     __list_add(new, head, head->next);
 61 }
 62 
 63 
 64 /**
 65  * list_add_tail - add a new entry
 66  * @new: new entry to be added
 67  * @head: list head to add it before
 68  *
 69  * Insert a new entry before the specified head.
 70  * This is useful for implementing queues.
 71  */
 72 static inline void list_add_tail(struct list_head *new, struct list_head *head)
 73 {
 74     __list_add(new, head->prev, head);
 75 }
 76 
 77 /*
 78  * Delete a list entry by making the prev/next entries
 79  * point to each other.
 80  *
 81  * This is only for internal list manipulation where we know
 82  * the prev/next entries already!
 83  */
 84 static inline void __list_del(struct list_head * prev, struct list_head * next)
 85 {
 86     next->prev = prev;
 87     prev->next = next;
 88 }
 89 
 90 /**
 91  * list_del - deletes entry from list.
 92  * @entry: the element to delete from the list.
 93  * Note: list_empty() on entry does not return true after this, the entry is
 94  * in an undefined state.
 95  */
 96 #ifndef CONFIG_DEBUG_LIST
 97 static inline void __list_del_entry(struct list_head *entry)
 98 {
 99     __list_del(entry->prev, entry->next);
100 }
101 #endif
102 
103 /**
104  * list_replace - replace old entry by new one
105  * @old : the element to be replaced
106  * @new : the new element to insert
107  *
108  * If @old was empty, it will be overwritten.
109  */
110 static inline void list_replace(struct list_head *old,
111                 struct list_head *new)
112 {
113     new->next = old->next;
114     new->next->prev = new;
115     new->prev = old->prev;
116     new->prev->next = new;
117 }
118 
119 static inline void list_replace_init(struct list_head *old,
120                     struct list_head *new)
121 {
122     list_replace(old, new);
123     INIT_LIST_HEAD(old);
124 }
125 
126 /**
127  * list_del_init - deletes entry from list and reinitialize it.
128  * @entry: the element to delete from the list.
129  */
130 static inline void list_del_init(struct list_head *entry)
131 {
132     __list_del_entry(entry);
133     INIT_LIST_HEAD(entry);
134 }
135 
136 /**
137  * list_move - delete from one list and add as another\'s head
138  * @list: the entry to move
139  * @head: the head that will precede our entry
140  */
141 static inline void list_move(struct list_head *list, struct list_head *head)
142 {
143     __list_del_entry(list);
144     list_add(list, head);
145 }
146 
147 /**
148  * list_move_tail - delete from one list and add as another\'s tail
149  * @list: the entry to move
150  * @head: the head that will follow our entry
151  */
152 static inline void list_move_tail(struct list_head *list,
153                   struct list_head *head)
154 {
155     __list_del_entry(list);
156     list_add_tail(list, head);
157 }
158 
159 /**
160  * list_is_last - tests whether @list is the last entry in list @head
161  * @list: the entry to test
162  * @head: the head of the list
163  */
164 static inline int list_is_last(const struct list_head *list,
165                 const struct list_head *head)
166 {
167     return list->next == head;
168 }
169 
170 /**
171  * list_empty - tests whether a list is empty
172  * @head: the list to test.
173  */
174 static inline int list_empty(const struct list_head *head)
175 {
176     return head->next == head;
177 }
178 
179 /**
180  * list_empty_careful - tests whether a list is empty and not being modified
181  * @head: the list to test
182  *
183  * Description:
184  * tests whether a list is empty _and_ checks that no other CPU might be
185  * in the process of modifying either member (next or prev)
186  *
187  * NOTE: using list_empty_careful() without synchronization
188  * can only be safe if the only activity that can happen
189  * to the list entry is list_del_init(). Eg. it cannot be used
190  * if another CPU could re-list_add() it.
191  */
192 static inline int list_empty_careful(const struct list_head *head)
193 {
194     struct list_head *next = head->next;
195     return (next == head) && (next == head->prev);
196 }
197 
198 /**
199  * list_rotate_left - rotate the list to the left
200  * @head: the head of the list
201  */
202 static inline void list_rotate_left(struct list_head *head)
203 {
204     struct list_head *first;
205 
206     if (!list_empty(head)) {
207         first = head->next;
208         list_move_tail(first, head);
209     }
210 }
211 
212 /**
213  * list_is_singular - tests whether a list has just one entry.
214  * @head: the list to test.
215  */
216 static inline int list_is_singular(const struct list_head *head)
217 {
218     return !list_empty(head) && (head->next == head->prev);
219 }
220 
221 static inline void __list_cut_position(struct list_head *list,
222         struct list_head *head, struct list_head *entry)
223 {
224     struct list_head *new_first = entry->next;
225     list->next = head->next;
226     list->next->prev = list;
227     list->prev = entry;
228     entry->next = list;
229     head->next = new_first;
230     new_first->prev = head;
231 }
232 
233 /**
234  * list_cut_position - cut a list into two
235  * @list: a new list to add all removed entries
236  * @head: a list with entries
237  * @entry: an entry within head, could be the head itself
238  *    and if so we won\'t cut the list
239  *
240  * This helper moves the initial part of @head, up to and
241  * including @entry, from @head to @list. You should
242  * pass on @entry an element you know is on @head. @list
243  * should be an empty list or a list you do not care about
244  * losing its data.
245  *
246  */
247 static inline void list_cut_position(struct list_head *list,
248         struct list_head *head, struct list_head *entry)
249 {
250     if (list_empty(head))
251         return;
252     if (list_is_singular(head) &&
253         (head->next != entry && head != entry))
254         return;
255     if (entry == head)
256         INIT_LIST_HEAD(list);
257     else
258         __list_cut_position(list, head, entry);
259 }
260 
261 static inline void __list_splice(const struct list_head *list,
262                  struct list_head *prev,
263                  struct list_head *next)
264 {
265     struct list_head *first = list->next;
266     struct list_head *last = list->prev;
267 
268     first->prev = prev;
269     prev->next = first;
270 
271     last->next = next;
272     next->prev = last;
273 }
274 
275 /**
276  * list_splice - join two lists, this is designed for stacks
277  * @list: the new list to add.
278  * @head: the place to add it in the first list.
279  */
280 static inline void list_splice(const struct list_head *list,
281                 struct list_head *head)
282 {
283     if (!list_empty(list))
284         __list_splice(list, head, head->next);
285 }
286 
287 /**
288  * list_splice_tail - join two lists, each list being a queue
289  * @list: the new list to add.
290  * @head: the place to add it in the first list.
291  */
292 static inline void list_splice_tail(struct list_head *list,
293                 struct list_head *head)
294 {
295     if (!list_empty(list))
296         __list_splice(list, head->prev, head);
297 }
298 
299 /**
300  * list_splice_init - join two lists and reinitialise the emptied list.
301  * @list: the new list to add.
302  * @head: the place to add it in the first list.
303  *
304  * The list at @list is reinitialised
305  */
306 static inline void list_splice_init(struct list_head *list,
307                     struct list_head *head)
308 {
309     if (!list_empty(list)) {
310         __list_splice(list, head, head->next);
311         INIT_LIST_HEAD(list);
312     }
313 }
314 
315 /**
316  * list_splice_tail_init - join two lists and reinitialise the emptied list
317  * @list: the new list to add.
318  * @head: the place to add it in the first list.
319  *
320  * Each of the lists is a queue.
321  * The list at @list is reinitialised
322  */
323 static inline void list_splice_tail_init(struct list_head *list,
324                      struct list_head *head)
325 {
326     if (!list_empty(list)) {
327         __list_splice(list, head->prev, head);
328         INIT_LIST_HEAD(list);
329     }
330 }
331 
332 /**
333  * list_entry - get the struct for this entry
334  * @ptr:    the &struct list_head pointer.
335  * @type:    the type of the struct this is embedded in.
336  * @member:    the name of the list_struct within the struct.
337  */
338 #define list_entry(ptr, type, member) \\
339     container_of(ptr, type, member)
340 
341 /**
342  * list_first_entry - get the first element from a list
343  * @ptr:    the list head to take the element from.
344  * @type:    the type of the struct this is embedded in.
345  * @member:    the name of the list_struct within the struct.
346  *
347  * Note, that list is expected to be not empty.
348  */
349 #define list_first_entry(ptr, type, member) \\
350     list_entry((ptr)->next, type, member)
351 
352 /**
353  * list_for_each    -    iterate over a list
354  * @pos:    the &struct list_head to use as a loop cursor.
355  * @head:    the head for your list.
356  */
357 #define list_for_each(pos, head) \\
358     for (pos = (head)->next; pos != (head); pos = pos->next)
359 
360 /**
361  * __list_for_each    -    iterate over a list
362  * @pos:    the &struct list_head to use as a loop cursor.
363  * @head:    the head for your list.
364  *
365  * This variant doesn\'t differ from list_for_each() any more.
366  * We don\'t do prefetching in either case.
367  */
368 #define __list_for_each(pos, head) \\
369     for (pos = (head)->next; pos != (head); pos = pos->next)
370 
371 /**
372  * list_for_each_prev    -    iterate over a list backwards
373  * @pos:    the &struct list_head to use as a loop cursor.
374  * @head:    the head for your list.
375  */
376 #define list_for_each_prev(pos, head) \\
377     for (pos = (head)->prev; pos != (head); pos = pos->prev)
378 
379 /**
380  * list_for_each_safe - iterate over a list safe against removal of list entry
381  * @pos:    the &struct list_head to use as a loop cursor.
382  * @n:        another &struct list_head to use as temporary storage
383  * @head:    the head for your list.
384  */
385 #define list_for_each_safe(pos, n, head) \\
386     for (pos = (head)->next, n = pos->next; pos != (head); \\
387         pos = n, n = pos->next)
388 
389 /**
390  * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
391  * @pos:    the &struct list_head to use as a loop cursor.
392  * @n:        another &struct list_head to use as temporary storage
393  * @head:    the head for your list.
394  */
395 #define list_for_each_prev_safe(pos, n, head) \\
396     for (pos = (head)->prev, n = pos->prev; \\
397          pos != (head); \\
398          pos = n, n = pos->prev)
399 
400 /**
401  * list_for_each_entry    -    iterate over list of given type
402  * @pos:    the type * to use as a loop cursor.
403  * @head:    the head for your list.
404  * @member:    the name of the list_struct within the struct.
405  */
406 #define list_for_each_entry(pos, head, member)                \\
407     for (pos = list_entry((head)->next, typeof(*pos), member);    \\
408          &pos->member != (head);     \\
409          pos = list_entry(pos->member.next, typeof(*pos), member))
410 
411 /**
412  * list_for_each_entry_reverse - iterate backwards over list of given type.
413  * @pos:    the type * to use as a loop cursor.
414  * @head:    the head for your list.
415  * @member:    the name of the list_struct within the struct.
416  */
417 #define list_for_each_entry_reverse(pos, head, member)            \\
418     for (pos = list_entry((head)->prev, typeof(*pos), member);    \\
419          &pos->member != (head);     \\
420          pos = list_entry(pos->member.prev, typeof(*pos), member))
421 
422 /**
423  * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
424  * @pos:    the type * to use as a start point
425  * @head:    the head of the list
426  * @member:    the name of the list_struct within the struct.
427  *
428  * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
429  */
430 #define list_prepare_entry(pos, head, member) \\
431     ((pos) ? : list_entry(head, typeof(*pos), member))
432 
433 /**
434  * list_for_each_entry_continue - continue iteration over list of given type
435  * @pos:    the type * to use as a loop cursor.
436  * @head:    the head for your list.
437  * @member:    the name of the list_struct within the struct.
438  *
439  * Continue to iterate over list of given type, continuing after
440  * the current position.
441  */
442 #define list_for_each_entry_continue(pos, head, member)         \\
443     for (pos = list_entry(pos->member.next, typeof(*pos), member);    \\
444          &pos->member != (head);    \\
445          pos = list_entry(pos->member.next, typeof(*pos), member))
446 
447 /**
448  * list_for_each_entry_continue_reverse - iterate backwards from the given point
449  * @pos:    the type * to use as a loop cursor.
450  * @head:    the head for your list.
Linux内核之数据双链表

c++学习笔记_c++实现双链表

日常学习随笔-自定义了一个双链表(注释蛮详细的)

list链表

C语言实现非循环双链表节点的删除(带头结点尾结点)

基础数据结构---双链表go语言的代码实现