-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.c
112 lines (95 loc) · 2.06 KB
/
list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <assert.h>
#include <stddef.h>
#include "list.h"
/**
* Set the next entry of a list.
* @param list The list to mutate
* @param next The new next entry
*/
void setnext(List * list, List * next) {
assert(list != NULL);
list->next = next;
}
/**
* Set the previous entry of a list.
* @param list The list to mutate
* @param prev The new prev entry
*/
void setprev(List * list, List * prev) {
assert(list != NULL);
list->prev = prev;
}
/**
* Find the head of a list
* @param list List to search
*/
List * gethead(List * list) {
List * head;
for(head = list; head->prev != NULL; head = head->prev);
return head;
}
/**
* Find the tail of a list
* @param list List to search
*/
List * gettail(List * list) {
List * tail;
for(tail = list; tail->next != NULL; tail = tail->next);
return tail;
}
/**
* Chop the given item out of the list, updating the prev and
* next. Returns the head of the list (may not have changed)
* @param list The list item to remove
*/
List * drop(List * list) {
assert(list != NULL);
List * head = list->next;
if(list->prev != NULL) {
setnext(list->prev, list->next);
head = gethead(list->prev);
}
if(list->next != NULL) {
setprev(list->next, list->prev);
}
return head;
}
/**
* Insert an item to a list. Returns the head.
* @param list The list to mutate
* @param insert The list item to insert
*/
List * insert(List * list, List * insert) {
assert(insert != NULL);
setnext(insert, list);
setprev(insert, NULL);
if(list != NULL) {
setprev(list, insert);
}
return insert;
}
/**
* Appends the source list to the target list. Returns the new head.
* @param target The list to append to
* @param source The list to append
*/
List * append(List * target, List * source) {
if(target == NULL) {
return source;
}
List * tail = gettail(target);
tail->next = source;
source->prev = tail;
return target;
}
/**
* Get the length of the list
* @param list The list
*/
unsigned int length(List * list) {
unsigned int out = 0;
for(List * item = list; item != NULL; item = item->next) {
out ++;
}
return out;
}