59 lines
1.5 KiB
C
59 lines
1.5 KiB
C
|
#include <list.h>
|
||
|
|
||
|
void init_list(struct dlist_node *n) {
|
||
|
n->prev = n;
|
||
|
n->next = n;
|
||
|
}
|
||
|
|
||
|
int list_empty(struct dlist_node *n) {
|
||
|
return n->next == n;
|
||
|
}
|
||
|
|
||
|
void insert_after(struct dlist_node *n, struct dlist_node *to_add) {
|
||
|
to_add->next = n->next;
|
||
|
to_add->next->prev = to_add;
|
||
|
n->next = to_add;
|
||
|
to_add->prev = n;
|
||
|
}
|
||
|
|
||
|
void insert_before(struct dlist_node *n, struct dlist_node *to_add) {
|
||
|
to_add->prev = n->prev;
|
||
|
to_add->prev->next = to_add;
|
||
|
n->prev = to_add;
|
||
|
to_add->next = n;
|
||
|
}
|
||
|
|
||
|
void remove(struct dlist_node *to_remove) {
|
||
|
if (!list_empty(to_remove)) {
|
||
|
to_remove->next->prev = to_remove->prev;
|
||
|
to_remove->prev->next = to_remove->next;
|
||
|
init_list(to_remove);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* Remove a series of nodes from a list. Note that this assumes the list is not
|
||
|
* empty, and that 'from' comes before 'to' in the list. 'from' and 'to' will
|
||
|
* both be removed from the list, along with any nodes which come after 'from'
|
||
|
* but before 'to'.
|
||
|
*/
|
||
|
void remove_splice(struct dlist_node *from, struct dlist_node *to) {
|
||
|
to->next->prev = from->prev;
|
||
|
from->prev->next = to->next;
|
||
|
|
||
|
//make the removed splice become its own wrap-around list, for easy access to the last member when re-splicing
|
||
|
to->next = from;
|
||
|
from->prev = to;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* Add a splice of nodes to a list (most likely a series previously removed via
|
||
|
* remove_splice()).
|
||
|
*/
|
||
|
void insert_splice_before(struct dlist_node *n, struct dlist_node *first, struct dlist_node *last) {
|
||
|
first->prev = n->prev;
|
||
|
first->prev->next = first;
|
||
|
n->prev = last;
|
||
|
last->next = n;
|
||
|
}
|