-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlist.c
145 lines (132 loc) · 2.84 KB
/
dlist.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "dlist.h"
#include <stdio.h>
#include <stdlib.h>
void dlist_init(struct DList *list) {
list->size = 0;
list->head = NULL;
list->tail = NULL;
}
void dlist_destroy(struct DList *list) {
while (dlist_size(list) > 0) {
dlist_remove(list, list->tail);
}
dlist_init(list);
}
// TODO:
enum Status dlist_insert_after(struct DList *list, struct DListNode *node,
DataType data) {
struct DListNode *new_node = malloc(sizeof(struct DListNode));
if (new_node == NULL) {
return STATUS_ERR_ALLOC;
}
new_node->data = data;
if (node == NULL) {
if (dlist_size(list) == 0) {
list->tail = new_node;
}
new_node->prev = NULL;
new_node->next = list->head;
list->head = new_node;
}
else {
if (node->next == NULL) {
list->tail = new_node;
}
else {
node->next->prev = new_node;
}
new_node->next = node->next;
node->next = new_node;
new_node->prev = node;
}
list->size++;
return STATUS_OK;
}
// TODO:
enum Status dlist_insert_before(struct DList *list, struct DListNode *node,
DataType data) {
struct DListNode *new_node = malloc(sizeof(struct DListNode));
if (new_node == NULL) {
return STATUS_ERR_ALLOC;
}
new_node->data = data;
if (node == NULL) {
if (dlist_size(list) != 0) {
list->head->prev = new_node;
}
new_node->prev = NULL;
new_node->next = list->head;
list->head = new_node;
}
else {
if (node->prev == NULL) {
list->head = new_node;
new_node->prev = NULL;
}
else {
node->prev->next = new_node;
new_node->prev = node->prev;
}
node->prev = new_node;
new_node->next = node;
}
list->size++;
return STATUS_OK;
}
enum Status dlist_push_front(struct DList *list, DataType data) {
return dlist_insert_before(list, list->head, data);
}
enum Status dlist_push_back(struct DList *list, DataType data) {
return dlist_insert_after(list, list->tail, data);
}
// TODO:
enum Status dlist_remove(struct DList *list, struct DListNode *node) {
if (dlist_size(list) == 0) {
return STATUS_ERR_INVALID_RANGE;
}
if (node == NULL) {
return STATUS_OK;
}
else {
if (dlist_size(list) == 1) {
dlist_init(list);
}
else if (node == list->head) {
list->head = node->next;
list->head->prev = NULL;
list->size--;
}
else if (node == list->tail) {
list->tail = node->prev;
list->tail->next = NULL;
list->size--;
}
else {
node->prev->next = node->next;
node->next->prev = node->prev;
list->size--;
}
free(node);
}
return STATUS_OK;
}
enum Status dlist_pop_front(struct DList *list) {
return dlist_remove(list, list->head);
}
DataType dlist_pop_back(struct DList *list) {
DataType tmp = list->tail->data;
list->size--;
if (list->tail->prev) {
list->tail = list->tail->prev;
list->tail->next = NULL;
free(list->tail->next);
}
else {
free(list->tail);
list->tail = list->head = NULL;
}
return tmp;
}
int dlist_size(struct DList *list) {
return list->size;
}