-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist.c
107 lines (89 loc) · 2.01 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
#include <stdio.h>
#include "list.h"
list *list_new()
{
list *this;
if (!(this = malloc(sizeof(list))))
return NULL;
this->head = NULL;
this->tail = NULL;
this->length = 0;
return this;
}
list_node *list_head_push(list *list, list_node *list_node)
{
if (!list_node) return NULL;
if (list->length)
{
list_node->next = list->head;
list_node->previous = NULL;
list->head->previous = list_node;
list->head = list_node;
}
else {
list->head = list->tail = list_node;
list_node->previous = list_node->next = NULL;
}
++list->length;
return list_node;
}
list_node *list_tail_push(list *list, list_node *list_node)
{
if (!list_node) return NULL;
if (list->length)
{
list_node->previous = list->tail;
list_node->next = NULL;
list->tail->next = list_node;
list->tail = list_node;
}
else {
list->head = list->tail = list_node;
list_node->previous = list_node->next = NULL;
}
++list->length;
return list_node;
}
list_node *list_head_pop(list *list)
{
if(!list->length) return NULL;
list_node *list_node = list->head;
if (--list->length)
{
(list->head = list_node->next)->previous = NULL;
}
else {
list->head = list->tail = NULL;
}
list_node->next = list_node->previous = NULL;
return list_node;
}
list_node *list_tail_pop(list *list)
{
if(!list->length) return NULL;
list_node *list_node = list->tail;
if(--list->length)
{
(list->tail = list_node->previous)->next = NULL;
}
else
{
list->head = list->tail = NULL;
}
list_node->next = list_node->previous = NULL;
return list_node;
}
void list_destroy(list *list)
{
unsigned int length = list->length;
list_node *next;
list_node *current = list->head;
while (length--)
{
next = current->next;
free(current);
current = next;
}
free(list);
list = NULL;
}