-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdlist.c
158 lines (128 loc) · 3.55 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
146
147
148
149
150
151
152
153
154
155
156
157
158
/* dlist.c */
#include <stdlib.h>
#include <string.h>
#include "dlist.h"
/* dlist_init */
void dlist_init(DList *list, void (*destroy)(void *data)){
/* Initialize the list */
list->size = 0;
list->destroy = destroy;
list->head = NULL;
list->tail = NULL;
return;
}
/* dlist_destroy */
void dlist_destroy(DList *list){
void *data;
/* Remove each element. */
while (dlist_size(list) > 0) {
if (dlist_remove(list, dlist_tail(list), (void **)&data) == 0
&& list->destroy != NULL) {
list->destroy(data);
}
}
/* No operation are allowed now, but clear the structure
* as a precaution. */
memset(list, 0, sizeof(DList));
return;
}
/* dlist_ins_next */
int dlist_ins_next(DList *list, DListElmt *element, const void *data){
DListElmt *new_elelment;
/* Don't allow a NULL element unless the list is empty. */
if (element == NULL && dlist_size(list) != 0) {
return -1;
}
/* Allocate storage for the element. */
if ((new_elelment = (DListElmt *)malloc(sizeof(DListElmt))) == NULL) {
return -1;
}
/* Insert the new element into the list. */
new_elelment->data = (void *) data;
if (dlist_size(list) == 0) {
/* Handle insertion when the list is empty. */
list->head = new_elelment;
list->head->prev = NULL;
list->head->next = NULL;
list->tail = new_elelment;
}
else {
/* Handle insertion when the list is not empty. */
new_elelment->next = element->next;
new_elelment->prev = element;
if (element->next == NULL) {
list->tail = new_elelment;
} else {
element->next->prev = new_elelment;
}
element->next = new_elelment;
}
/* Adjust the size of the list to the account for the inserted element. */
list->size++;
return 0;
}
/* dlist_ins_prev */
int dlist_ins_prev(DList *list, DListElmt *element, const void *data){
DListElmt *new_elelment;
/* Don't allow a NULL element unless the list is empty. */
if (element == NULL && dlist_size(list) != 0) {
return -1;
}
/* Allocate storage to be managed by the abstract datetype. */
if ((new_elelment = (DListElmt *)malloc(sizeof(DListElmt))) == NULL) {
return -1;
}
/* Insert the new element into the list. */
new_elelment->data = (void *)data;
if (dlist_size(list) == 0) {
/* Handle insertion when the list is empty */
list->head = new_elelment;
list->head->prev = NULL;
list->head->next = NULL;
list->tail = new_elelment;
} else {
/* Handle insertion when the list is not empty. */
new_elelment->next = element;
new_elelment->prev = element->prev;
if (element->prev == NULL) {
list->head = new_elelment;
} else {
element->prev->next = new_elelment;
}
element->prev = new_elelment;
}
/* Adjust the size of list to account for the new element. */
list->size++;
return 0;
}
/* dlist_remove */
int dlist_remove(DList *list, DListElmt *element, void **data){
/* Don't allow a NULL element or remoral from an empty list. */
if (element == NULL && dlist_size(list) == 0) {
return -1;
}
/* Remove the element from the list. */
*data = element->data;
if (element == list->head) {
/* Handle remoral from the head of list. */
list->head = element->next;
if (list->head == NULL) {
list->tail = NULL;
} else {
element->next->prev = NULL;
}
} else {
/* Handle remoral from other than the head of the list. */
element->prev->next = element->next;
if (element->next == NULL) {
list->tail = element->prev;
} else {
element->next->prev = element->prev;
}
}
/* Free the storage allocated by the abstract datetype. */
free(element);
/* Adjust the size of list to account for the removed element. */
list->size--;
return 0;
}