-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
41 lines (37 loc) · 750 Bytes
/
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
#include "list.h"
void list_add_tail(struct list_head *head, struct list_node *node) {
head->len++;
if (head->tail) {
node->next = NULL;
node->prev = head->tail;
head->tail->next = node;
head->tail = node;
} else {
/* empty list*/
node->next = NULL;
node->prev = NULL;
head->head = node;
head->tail = node;
}
}
struct list_node *list_del_head(struct list_head *head) {
struct list_node *orphan;
orphan = head->head;
if (!orphan) {
return NULL;
}
head->len--;
if (orphan->next) {
orphan->next->prev = NULL;
head->head = orphan->next;
orphan->next = NULL;
orphan->prev = NULL;
} else {
head->head = NULL;
head->tail = NULL;
}
return orphan;
}
int list_len(struct list_head *head) {
return head->len;
}