-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathll.c
94 lines (72 loc) · 1.29 KB
/
ll.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
#include "ll.h"
#include <stdlib.h>
list_t *
list_new(void)
{
list_t *list;
if ((list = malloc(sizeof(list_t))) == NULL)
return NULL;
list->length = 0;
list->head = NULL;
list->tail = NULL;
return list;
}
list_t *
list_push_head(list_t *self, void *value)
{
list_node_t *node;
if (self == NULL)
return NULL;
if ((node = malloc(sizeof(*node))) == NULL)
return NULL;
node->next = NULL;
node->prev = NULL;
node->value = value;
if (self->length == 0) {
self->head = node;
self->tail = node;
} else {
node->next = self->head;
self->head->prev = node;
self->head = node;
}
self->length++;
return self;
}
list_t *
list_push_tail(list_t *self, void *value)
{
list_node_t *node;
if (self == NULL)
return NULL;
if ((node = malloc(sizeof(*node))) == NULL)
return NULL;
node->next = NULL;
node->prev = NULL;
node->value = value;
if (self->length == 0) {
self->head = node;
self->tail = node;
} else {
node->prev = self->tail;
self->tail->next = node;
self->tail = node;
}
self->length++;
return self;
}
void
list_free(list_t *self)
{
uint64_t length = self->length;
list_node_t *current = self->head;
list_node_t *next;
while (length--) {
next = current->next;
free(current->value);
free(current);
current = next;
}
free(self);
self = NULL;
}