-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.h
427 lines (418 loc) · 17.3 KB
/
list.h
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
Suhas Kashyap 01FB14ECS255
Varun Bharadwaj 01FB14ECS278
Varun M 01FB14ECS280
*/
#ifndef list_h
#define list_h
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
long int_hash(int* t)
{
return *t;
}
void* reference(void* value)
{
return value;
}
#define List_declare(type) \
\
typedef struct list_node_##type /*stringification*/ \
{ \
type data; \
struct list_node_##type* next; \
struct list_node_##type* prev; \
}list_node_##type; \
\
typedef struct list_##type \
{ \
/*Members of a struct*/ \
list_node_##type* head; \
long size; \
\
/*Member functions declaration*/ \
long (*list_length)(struct list_##type*); \
void (*insert_head)(struct list_##type*, type); \
void (*insert_tail)(struct list_##type*, type); \
void (*list_insert)(struct list_##type*, type, long); \
void (*replace)(struct list_##type*, type, long); \
void (*drop_head)(struct list_##type*); \
void (*drop_tail)(struct list_##type*); \
void (*drop)(struct list_##type*, long position); \
void (*dslib_free_ds)(struct list_##type*); \
void (*list_disp)(struct list_##type*,void (*)(type*)); \
struct list_##type* (*chain)(struct list_##type*,struct list_##type*); \
type* (*list_to_array)(struct list_##type*); \
long (*list_search)(struct list_##type*,type,long (*)(type*)); \
void (*list_reverse)(struct list_##type*); \
type (*list_get)(struct list_##type*,long); \
}list_##type; \
\
list_node(type) new_node_##type(type value) \
{ \
list_node(type) newNode=(list_node(type)) malloc(sizeof(list_node_##type)); \
newNode->next=NULL; \
newNode->prev=NULL; \
newNode->data=value; \
return newNode; \
} \
\
long list_length_##type(List(type) l) \
{ \
return l->size; \
} \
\
void list_disp_##type(List(type) list,void (*nodePrinter)(type*)) \
{ \
if(list->head==NULL) \
{ \
printf("length: %ld\n",list_length(list)); \
return; \
} \
printf("\n"); \
list_node(type) p=list->head; \
if(nodePrinter==NULL) \
{ \
while(p) \
{ \
_Generic((p->data),int:printf("%d",p->data),long:printf("%ld",p->data),float:printf("%lf",p->data),double:printf("%lf",p->data),char*:printf("%s",p->data),default:printf("%p",&(p->data))); \
p=p->next; \
if(p) \
printf(" -> "); \
} \
} \
else \
{ \
while(p) \
{ \
_Generic((p->data),int:printf("%d",p->data),long:printf("%ld",p->data),float:printf("%lf",p->data),double:printf("%lf",p->data),char*:printf("%s",p->data),default:nodePrinter(&(p->data))); \
p=p->next; \
if(p) \
printf(" -> "); \
} \
} \
printf("\tlength: %ld\n",list_length(list)); \
} \
\
/*Insert node at beginning*/ \
void insert_head_##type(List(type) list,type value) \
{ \
list_node(type) newNode = new_node_##type(value); \
\
if(list->head==NULL) \
list->head=newNode; \
else \
{ \
newNode->next=list->head; \
(list->head)->prev=newNode; \
list->head=newNode; \
} \
++(list->size); \
/*return list;*/ \
} \
\
/*Insert node at end of link-list*/ \
void insert_tail_##type(List(type) list,type value) \
{ \
list_node(type) newNode=new_node_##type(value); \
\
if(list->head==NULL) \
list->head=newNode; \
else \
{ \
list_node(type) p=list->head; \
list_node(type) q; \
while(p) /*p!=NULL*/ \
{ \
q=p; \
p=p->next; \
} \
q->next=newNode; \
newNode->prev=q; \
} \
++(list->size); \
/*return list;*/ \
} \
\
/*Insert node at specified position*/ \
void list_insert_##type(List(type) list,type value,long position) \
{ \
long list_len=(list->list_length)(list)+1; \
assert(position>0 && position<=list_len); \
\
if(position==1) \
{ \
insert_head_##type(list,value); \
} \
else if(position==list_len) \
{ \
insert_tail_##type(list,value); \
} \
else \
{ \
list_node(type) newNode=new_node_##type(value); \
long pos=2; \
\
list_node(type) p=list->head->next; \
list_node(type) q=list->head; \
while(pos!=position && p) /*p!=NULL*/ \
{ \
q=p; \
p=p->next; \
pos+=1; \
} \
q->next=newNode; \
newNode->prev=q; \
newNode->next=p; \
p->prev=newNode; \
} \
++(list->size); \
/*return list;*/ \
} \
\
/*Replace data of a node at speified position*/ \
void replace_##type(List(type) list,type rdata,long position) \
{ \
assert(position>0 && position<=list_length(list)); \
\
long pos=1; \
list_node(type) p=list->head; \
while(pos!=position && p->next) /*p!=NULL*/ \
{ \
p=p->next; \
pos+=1; \
} \
p->data=rdata; \
} \
\
/*Drop node at beginning of link list*/ \
void drop_head_##type(List(type) list) \
{ \
if(list->head!=NULL) \
{ \
list_node(type) p=list->head; \
list->head=p->next; \
free(p); \
if(list->head) \
list->head->prev=NULL; \
--(list->size); \
} \
/*return list;*/ \
} \
\
/*Drop node at end of link list*/ \
void drop_tail_##type(List(type) list) \
{ \
if(list->head==NULL)return; \
else if(list->head->next==NULL) \
{ \
list_node(type) ref=list->head; \
free(ref); \
list->head=NULL; \
--(list->size); \
} \
else \
{ \
list_node(type) p=list->head; \
list_node(type) q; \
while(p) /*p!=NULL*/ \
{ \
q=p; \
p=p->next; \
} \
list_node(type) ref=q; \
q->prev->next=NULL; \
free(ref); \
--(list->size); \
} \
/*return list;*/ \
} \
\
/*Drop node at specified position*/ \
void drop_##type(List(type) list,long position) \
{ \
long list_size=(list->list_length)(list); \
assert(position>0 && position<=list_size); \
\
if(position==1) \
{ \
drop_head_##type(list); \
} \
else if(position==list_size) \
{ \
drop_tail_##type(list); \
} \
else \
{ \
long pos=2; \
list_node(type) p=list->head->next; \
list_node(type) q=list->head; \
while(pos!=position && p) /*p!=NULL*/ \
{ \
q=p; \
p=p->next; \
pos+=1; \
} \
q->next=p->next; \
p->next->prev=q; \
free(p); \
} \
--(list->size); \
/*return list;*/ \
} \
\
List(type) chain_##type(List(type) l1,List(type) l2) \
{ \
if(l1->head==NULL) return l2; \
else if(l2==NULL || l2->head==NULL) return l1; \
\
list_node(type) p=l1->head; \
list_node(type) q; \
while(p) \
{ \
q=p; \
p=p->next; \
} \
q->next=l2->head; \
l2->head->prev=q; \
return l1; \
} \
\
type* list_to_array_##type(List(type) l) \
{ \
type* array=malloc(list_length(l)*sizeof(type)); \
list_node(type) p=l->head; \
long i=0; \
while(p) \
{ \
array[i]=p->data; \
i+=1; \
p=p->next; \
} \
return array; \
} \
\
long list_search_##type(List(type) list, type key, long (*hash_code)(type*)) \
{ \
if(list->head==NULL)return -1; \
long pos=1; \
list_node(type) p; \
if(hash_code==NULL) \
{ \
p=list->head; \
while(p) \
{ \
if(_Generic((key),int:int_hash,default:reference)(&(p->data))==_Generic((key),int:int_hash,default:reference)(&key)) \
return pos; \
p=p->next; \
++pos; \
} \
} \
else \
{ \
list_node(type) p=list->head; \
while(p) \
{ \
if(_Generic((key),int:int_hash,default:(hash_code))(&(p->data))==_Generic((key),int:int_hash,default:hash_code)(&key)) \
return pos; \
p=p->next; \
++pos; \
} \
} \
return -1; \
} \
\
void list_reverse_##type(List(type) list) \
{ \
if(list->head==NULL)return; \
list_node(type) p=list->head; \
list_node(type) q=NULL; \
list_node(type) ref; \
while(p) \
{ \
q=p->prev; \
p->prev=p->next; \
p->next=q; \
\
ref=p; \
p=p->prev; \
} \
list->head=ref; \
} \
\
/*De-allocate memory*/ \
void list_destroy_##type(List(type) list) \
{ \
if(list==NULL) return; \
list_node(type) p=list->head; \
list_node(type) q; \
while(p!=NULL) \
{ \
q=p->next; \
free(p); \
p=q; \
} \
/*free(list);*/ \
list->head=NULL; \
} \
\
type list_get_##type(List(type) list, long position) \
{ \
assert(position>0 && position<=list_length(list)); \
long pos=1; \
list_node(type) p; \
p=list->head; \
while(p) \
{ \
if(pos==position) \
return p->data; \
p=p->next; \
++pos; \
} \
} \
\
List(type) new_List_##type() \
{ \
List(type) l=malloc(sizeof(list_##type)); \
\
l->head=NULL; \
l->size=0; \
\
l->list_length=list_length_##type; \
l->insert_head=insert_head_##type; \
l->insert_tail=insert_tail_##type; \
l->list_insert=list_insert_##type; \
l->replace=replace_##type; \
l->drop_head=drop_head_##type; \
l->drop_tail=drop_tail_##type; \
l->drop=drop_##type; \
l->dslib_free_ds=list_destroy_##type; \
l->list_disp=list_disp_##type; \
l->chain=chain_##type; \
l->list_to_array=list_to_array_##type; \
l->list_search=list_search_##type; \
l->list_reverse=list_reverse_##type; \
l->list_get=list_get_##type; \
\
return l; \
}
#define list_node(type) list_node_##type*
#define new_List(type) new_List_##type()
#define List(type) list_##type*
#define list_length(l) l->list_length(l)
#define list_disp(l,nodePrinter) l->list_disp(l,nodePrinter)
#define insert_head(l,type) l->insert_head(l,type)
#define insert_tail(l,type) l->insert_tail(l,type)
#define insert(l,type,position) l->list_insert(l,type,position)
#define replace(l,type,position) l->replace(l,type,position)
#define drop_head(l) l->drop_head(l)
#define drop_tail(l) l->drop_tail(l)
#define drop(l,position) l->drop(l,position)
#define chain(l1,l2) l1->chain(l1,l2)
#define list_to_array(l) l->list_to_array(l)
#define list_search(l,value,hc) l->list_search(l,value,hc)
#define list_reverse(l) l->list_reverse(l)
#define list_get(l,val) l->list_get(l,val)
/*#define list_destroy(l) l->list_destroy(l)*/
#endif