-
Notifications
You must be signed in to change notification settings - Fork 0
/
my402list.c
269 lines (219 loc) · 6.96 KB
/
my402list.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//
// Created by surajhs04 on 9/17/16.
//
#ifndef _MY402LIST_C_
#define _MY402LIST_C_
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include "my402list.h"
/*
* Returns TRUE if the list is empty
*/
int My402ListEmpty(My402List *list) {
if (list->num_members == 0)
return TRUE;
return FALSE;
}
/*
* Returns the number of members in list
*/
int My402ListLength(My402List *list) {
return list->num_members;
}
/*
* Returns the first list element or NULL if the list is empty.
*/
My402ListElem *My402ListFirst(My402List *list) {
int isListEmpty = My402ListEmpty(list);
if (isListEmpty) {
return NULL;
}
return list->anchor.next;
}
/*
* Returns the last list element or NULL if the list is empty
*/
My402ListElem *My402ListLast(My402List *list) {
int isListEmpty = My402ListEmpty(list);
if (isListEmpty) {
return NULL;
}
return list->anchor.prev;
}
/*
* Returns elem->next or NULL if elem is the last item on the list. Do not check if elem is on the list.
*/
My402ListElem *My402ListNext(My402List *list, My402ListElem *elem) {
if (elem->next == &(list->anchor)) {
return NULL;
} else {
return elem->next;
}
}
/*
* Returns elem->prev or NULL if elem is the first item on the list. Do not check if elem is on the list.
*/
My402ListElem *My402ListPrev(My402List *list, My402ListElem *elem) {
if (elem->prev == &(list->anchor)) {
return NULL;
} else {
return elem->prev;
}
}
/*
* Returns the list element elem such that elem->obj == obj. Returns NULL if no such element can be found.
*/
My402ListElem *My402ListFind(My402List *list, void *obj) {
My402ListElem *my402ListElem = NULL;
for (my402ListElem = My402ListFirst(list); my402ListElem != NULL;
my402ListElem = My402ListNext(list, my402ListElem)) {
if (my402ListElem->obj == obj) {
return my402ListElem;
}
}
return NULL;
}
/*
* If list is empty, just add obj to the list. Otherwise, add obj after Last().
* This function returns TRUE if the operation is performed successfully and returns FALSE otherwise
*/
int My402ListAppend(My402List *list, void *obj) {
My402ListElem *listElem = NULL;
My402List *my402List = NULL;
my402List = list;
listElem = malloc(sizeof(My402ListElem));
if (listElem == NULL) {
fprintf(stderr, "ERROR : Couldn't allocate memory = %s", strerror(errno));
return FALSE;
}
if (My402ListEmpty(list)) {
my402List->num_members = 1;
listElem->obj = obj;
my402List->anchor.next = listElem;
listElem->next = &(my402List->anchor);
my402List->anchor.prev = listElem;
listElem->prev = &(my402List->anchor);
} else {
my402List->num_members++;
listElem->obj = obj;
listElem->next = &(my402List->anchor);
listElem->prev = my402List->anchor.prev;
my402List->anchor.prev->next = listElem;
my402List->anchor.prev = listElem;
}
return TRUE;
}
/*
* If list is empty, just add obj to the list. Otherwise, add obj before First().
* This function returns TRUE if the operation is performed successfully and returns FALSE otherwise.
*/
int My402ListPrepend(My402List *list, void *obj) {
My402ListElem *listElem = NULL;
My402List *my402List = NULL;
my402List = list;
listElem = malloc(sizeof(My402ListElem));
if (listElem == NULL) {
fprintf(stderr, "ERROR : Couldn't allocate memory = %s", strerror(errno));
return FALSE;
}
if (My402ListEmpty(list)) {
my402List->num_members = 1;
listElem->obj = obj;
my402List->anchor.next = listElem;
listElem->next = &(my402List->anchor);
my402List->anchor.prev = listElem;
listElem->prev = &(my402List->anchor);
} else {
my402List->num_members++;
listElem->obj = obj;
listElem->next = my402List->anchor.next;
listElem->prev = &(my402List->anchor);
my402List->anchor.next->prev = listElem;
my402List->anchor.next = listElem;
}
return TRUE;
}
/*
* Insert obj between elem and elem->next. If elem is NULL, then this is the same as Append().
* This function returns TRUE if the operation is performed successfully and returns FALSE otherwise.
* Do not check if elem is on the list.
*/
int My402ListInsertAfter(My402List *list, void *obj, My402ListElem *my402ListElem) {
if (my402ListElem == NULL) {
My402ListAppend(list, obj);
} else {
My402ListElem *newElem = NULL;
newElem = malloc(sizeof(My402ListElem));
if (newElem == NULL) {
fprintf(stderr, "ERROR : Couldn't allocate memory = %s", strerror(errno));
return FALSE;
}
newElem->obj = obj;
newElem->next = my402ListElem->next;
newElem->prev = my402ListElem;
my402ListElem->next->prev = newElem;
my402ListElem->next = newElem;
list->num_members++;
}
return TRUE;
}
/*
* Insert obj between elem and elem->prev. If elem is NULL, then this is the same as Prepend().
* This function returns TRUE if the operation is performed successfully and returns FALSE otherwise.
* Do not check if elem is on the list.
*/
int My402ListInsertBefore(My402List *list, void *obj, My402ListElem *my402ListElem) {
if (my402ListElem == NULL) {
My402ListPrepend(list, obj);
} else {
My402ListElem *newElem = NULL;
newElem = malloc(sizeof(My402ListElem));
if (newElem == NULL) {
fprintf(stderr, "ERROR : Couldn't allocate memory = %s", strerror(errno));
return FALSE;
}
newElem->obj = obj;
newElem->next = my402ListElem;
newElem->prev = my402ListElem->prev;
my402ListElem->prev->next = newElem;
my402ListElem->prev = newElem;
list->num_members++;
}
return TRUE;
}
/*
* Unlink and delete elem from the list.
* Do not delete the object pointed to by elem and do not check if elem is on the list.
*/
void My402ListUnlink(My402List *list, My402ListElem *elem) {
My402ListElem *previousElem = elem->prev;
My402ListElem *nextElem = elem->next;
previousElem->next = nextElem;
nextElem->prev = previousElem;
free(elem);
list->num_members--;
}
/*
* Unlink and delete all elements from the list and make the list empty.
* Do not delete the objects pointed to be the list elements.
*/
void My402ListUnlinkAll(My402List *list) {
My402ListElem *my402ListElem = NULL;
for (my402ListElem = My402ListFirst(list); my402ListElem != NULL;
my402ListElem = My402ListNext(list, my402ListElem)) {
My402ListUnlink(list, my402ListElem);
}
}
/*
* Initialize the list into an empty list.
* Returns TRUE if all is well and returns FALSE if there is an error initializing the list
*/
int My402ListInit(My402List *list) {
list->anchor.obj = NULL;
list->anchor.next = list->anchor.prev = NULL;
list->num_members = 0;
return TRUE;
}
#endif