4 /* Initializes 'list' as an empty list. */
6 list_init(struct list *list)
8 list->next = list->prev = list;
11 /* Inserts 'elem' just before 'before'. */
13 list_insert(struct list *before, struct list *elem)
15 elem->prev = before->prev;
17 before->prev->next = elem;
21 /* Removes elements 'first' though 'last' (exclusive) from their current list,
22 then inserts them just before 'before'. */
24 list_splice(struct list *before, struct list *first, struct list *last)
30 /* Cleanly remove 'first'...'last' from its current list. */
31 first->prev->next = last->next;
32 last->next->prev = first->prev;
34 /* Splice 'first'...'last' into new list. */
35 first->prev = before->prev;
37 before->prev->next = first;
41 /* Inserts 'elem' at the beginning of 'list', so that it becomes the front in
44 list_push_front(struct list *list, struct list *elem)
46 list_insert(list->next, elem);
49 /* Inserts 'elem' at the end of 'list', so that it becomes the back in
52 list_push_back(struct list *list, struct list *elem)
54 list_insert(list, elem);
57 /* Removes 'elem' from its list and returns the element that followed it.
58 Undefined behavior if 'elem' is not in a list. */
60 list_remove(struct list *elem)
62 elem->prev->next = elem->next;
63 elem->next->prev = elem->prev;
67 /* Removes the front element from 'list' and returns it. Undefined behavior if
68 'list' is empty before removal. */
70 list_pop_front(struct list *list)
72 struct list *front = list->next;
77 /* Removes the back element from 'list' and returns it.
78 Undefined behavior if 'list' is empty before removal. */
80 list_pop_back(struct list *list)
82 struct list *back = list->prev;
87 /* Returns the front element in 'list'.
88 Undefined behavior if 'list' is empty. */
90 list_front(struct list *list)
92 assert(!list_is_empty(list));
96 /* Returns the back element in 'list'.
97 Undefined behavior if 'list' is empty. */
99 list_back(struct list *list)
101 assert(!list_is_empty(list));
105 /* Returns the number of elements in 'list'.
106 Runs in O(n) in the number of elements. */
108 list_size(const struct list *list)
110 const struct list *e;
113 for (e = list->next; e != list; e = e->next)
118 /* Returns true if 'list' is empty, false otherwise. */
120 list_is_empty(const struct list *list)
122 return list->next == list;