-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
73 lines (60 loc) · 1.03 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
#include <assert.h>
#include "list.h"
void list_push_front(list_t *list, node_t *node)
{
assert(list);
assert(node);
node->next = list->head;
if (list->head) {
list->head->prev = node;
} else {
list->tail = node;
}
list->head = node;
list->size++;
}
void list_push_back(list_t *list, node_t *node)
{
assert(list);
assert(node);
node->prev = list->tail;
if (list->tail) {
list->tail->next = node;
} else {
list->head = node;
}
list->tail = node;
list->size++;
}
node_t* list_pop_front(list_t *list)
{
assert(list);
node_t *result = list->head;
if (result) {
list->size--;
list->head = list->head->next;
if (list->head) {
list->head->prev = NULL;
} else {
list->tail = NULL;
}
result->next = NULL;
}
return result;
}
node_t* list_pop_back(list_t *list)
{
assert(list);
node_t *result = list->tail;
if (result) {
list->size--;
list->tail = list->tail->prev;
if (list->tail) {
list->tail->next = NULL;
} else {
list->head = NULL;
}
result->prev = NULL;
}
return result;
}