-
Notifications
You must be signed in to change notification settings - Fork 0
/
my402list.c
282 lines (282 loc) · 7.89 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//Author: Aditya Barawkar
//CSCI402: warmup1
#include<stdio.h>
#include<string.h>
#include "my402list.h"
#include<stdlib.h>
#define MAX_LINE 1024
#ifdef DEBUG
# define dbg(x) printf("DEBUG:%s",x)
#else
# define dbg(x) {}//No Debug
#endif
int errno;
char dbgMsg[1024];
extern int My402ListLength(My402List* list)
{
return(list->num_members);
}
extern int My402ListEmpty(My402List* list)
{
if (list->num_members == 0)
return(1); //TRUE
else
return(0); //FALSE
}
extern int My402ListAppend(My402List* list, void* data)
{
//Add obj after Last(). This function returns TRUE if the operation is performed successfully and returns FALSE otherwise.
My402ListElem *last;
My402ListElem *p = (My402ListElem *)malloc(sizeof(My402ListElem));
if ( p == NULL)
{
fprintf(stderr,"ERROR:In function: %s at line %d. %s \n",__func__,__LINE__,strerror(errno));
return(0); //FALSE
}
p->obj = data;
if ( My402ListEmpty(list) )
{
list->anchor.next = list->anchor.prev = p;
p->next = &(list->anchor);
p->prev = &(list->anchor);
list->num_members = 1;
return(1); //TRUE
}
else
{
last = My402ListLast(list);
last->next = p;
p->next = &(list->anchor);
p->prev = last;
list->anchor.prev = p;
list->num_members = (list->num_members) + 1;
return(1); //TRUE
}
//fprintf(stderr,"Some thing unusual in %s at line %d. No condition in if was satisifed.Returning FALSE \n",__func__,__LINE__);
//return(0); //FALSE
}
extern int My402ListPrepend(My402List* list, void* data)
{
//Add obj before First(). This function returns TRUE if the operation is performed successfully and returns FALSE otherwise.
My402ListElem *first;
My402ListElem *p = (My402ListElem *)malloc(sizeof(My402ListElem));
if (p == NULL)
{
fprintf(stderr,"ERROR: In function: %s at line %d %s \n",__func__,__LINE__,strerror(errno));
return(0); //FALSE
}
p->obj = data;
if ( My402ListEmpty(list) )
{
list->anchor.next = list->anchor.prev = p;
p->next = &(list->anchor);
p->prev = &(list->anchor);
list->num_members = 1;
return(1); //TRUE
}
else
{
first = My402ListFirst(list);
list->anchor.next = p;
p->prev = &(list->anchor);
p->next = first;
first->prev = p;
list->num_members = (list->num_members) + 1;
return(1); //TRUE
}
//sprintf(dbgMsg,"Some thing unusual in %s at line %d. No condition in if was satisifed.Returning FALSE \n",__func__,__LINE__);
//dbg(dbgMsg);
//return(0); //FALSE
}
extern void My402ListUnlink(My402List* list, My402ListElem* p)
{
//Unlink and delete elem from the list. Please do not delete the object pointed to by elem and do not check if elem is on the list.
//Assume the element is present in list. Still will check a trivila thing that if there is atleast one element in the list.
if (list->num_members == 0)
{
fprintf(stderr,"In function %s and line %d. \nThere are no elements in the list and still trying to delete.Exiting..\n",__func__,__LINE__);
exit(1);
}
(p->prev)->next = p->next;
(p->next)->prev = p->prev;
list->num_members = (list->num_members) - 1;
free(p);
}
extern void My402ListUnlinkAll(My402List* list)
{
//Unlink and delete all elements from the list and make the list empty. Please do not delete the objects pointed to be the list elements.
while ( list->num_members > 0)
{
My402ListUnlink(list, list->anchor.next);
}
}
extern int My402ListInsertAfter(My402List* list, void* data, My402ListElem* elem)
{
//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. Please do not check if elem is on the list.
My402ListElem *p;
if (elem == NULL)
{
if (My402ListAppend(list,data))
return(1); // TRUE
else
return(0); //FALSE
}
else
{
p = (My402ListElem *)malloc(sizeof(My402ListElem));
if (p == NULL)
{
fprintf(stderr,"ERROR: In function: %s at line %d %s \n",__func__,__LINE__,strerror(errno));
return(0); //FALSE
}
p->obj = data;
p->next = elem->next;
p->prev = elem;
elem->next= p;
(p->next)->prev = p;
list->num_members = (list->num_members) + 1;
return(1); //TRUE
}
}
extern int My402ListInsertBefore(My402List* list, void* data, My402ListElem* elem)
{
//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. Please do not check if elem is on the list.
My402ListElem *p;
if (elem == NULL)
{
if(My402ListPrepend(list,data))
return(1); //TRUE
else
return(0); //FALSE
}
else
{
p = (My402ListElem *)malloc(sizeof(My402ListElem));
if (p == NULL)
{
fprintf(stderr,"ERROR: In function: %s at line %d %s \n",__func__,__LINE__,strerror(errno));
return(0); //FALSE
}
p->obj = data;
p->prev = elem->prev;
p->next = elem;
elem->prev = p;
(p->prev)->next = p;
list->num_members = (list->num_members) + 1;
return(1); //TRUE
}
}
extern My402ListElem *My402ListFirst(My402List* list)
{
//Returns the first list element or NULL if the list is empty.
if (My402ListEmpty(list))
return(NULL);
else
return(list->anchor.next); //First element
}
extern My402ListElem *My402ListLast(My402List* list)
{
//Returns the last list element or NULL if the list is empty.
if (My402ListEmpty(list))
return(NULL);
else
return(list->anchor.prev); //Last element
}
extern My402ListElem *My402ListNext(My402List* list, My402ListElem* elem)
{
//Returns elem->next or NULL if elem is the last item on the list. Please do not check if elem is on the list.
My402ListElem *last;
last = My402ListLast(list);
if (last == elem)
return(NULL);
else
return(elem->next);
}
extern My402ListElem *My402ListPrev(My402List* list, My402ListElem* elem)
{
//Returns elem->prev or NULL if elem is the first item on the list. Please do not check if elem is on the list.
My402ListElem *first;
first = My402ListFirst(list);
if (first == elem)
return(NULL);
else
return(elem->prev);
}
extern My402ListElem *My402ListFind(My402List* list, void* data)
{
//Returns the list element elem such that elem->obj == obj. Returns NULL if no such element can be found.
My402ListElem *first, *last, *p;
first = My402ListFirst(list);
last = My402ListLast(list);
if (NULL == first)
{
sprintf(dbgMsg,"In Find: List is empty. Returning NULL\n");
dbg(dbgMsg);
return(NULL); //The list is empty
}
else
{
p = first;
while ( p != last)
{
if (p->obj == data)
{
sprintf(dbgMsg,"In Find: Found the element\n");
dbg(dbgMsg);
return(p); //Found Object
}
else
{
p = p->next;
}
}
if (p == last)
{
if (p->obj == data)
{
sprintf(dbgMsg,"In Find: Found the element as the last element\n");
dbg(dbgMsg);
return(p); //Found Object
}
else
{
sprintf(dbgMsg,"In Find: Element not found. Returning NULL\n");
dbg(dbgMsg);
return(NULL); //Element not found
}
}
else
{
sprintf(dbgMsg,"In function %s line #%d. pointer p traveresed whole list but did not reach last. Seems like a logical flaw in the the program. Returning NULL\n",__func__,__LINE__);
dbg(dbgMsg);
return(NULL);
}
}
}
extern int My402ListInit(My402List* list)
{
//Initialize the list into an empty list. Returns TRUE if all is well and returns FALSE if there is an error initializing the list.
if (list->num_members == 0)
{
list->anchor.next = &(list->anchor);
list->anchor.prev = &(list->anchor);
list->anchor.obj = NULL;
//return(1); //TRUE
}
else
{
My402ListUnlinkAll(list);
//return(1); //TRUE
}
if (list->num_members != 0)
{
sprintf(dbgMsg, "In function %s line #%d. Seems like unlink all dint work. Returning False as UnlinkAll did not leave the list with zero elements\n",__func__,__LINE__);
dbg(dbgMsg);
return(0); //FALSE
}
else
{
return(1); //TRUE
}
}