-
Notifications
You must be signed in to change notification settings - Fork 2
/
list.h
532 lines (468 loc) · 10.7 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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
#ifndef LIST_
#define LIST_
#include "stdafx.h"
#include <stdlib.h>
#include <conio.h>
#define FLUSH_ free();
#define FOOD_ int;
struct payload
{
char *str;
};
struct linked_list
{
void *payload;
linked_list *next,*prev,*my;
linked_list* attach_next(void *src)
{
if(my->next!=NULL)
{printf("restricted node creation..bridge already occupied!!");
exit(0);
}
else{
my->next=(linked_list*)malloc(sizeof(linked_list));
my->next->prev=my;
my->next->next=NULL;
my->next->my=my->next;
my->next->payload=src;
return my->next;
}
}
linked_list* get_root()
{
linked_list *tmp=my;while(tmp->prev!=NULL){tmp=tmp->prev;}return tmp;
}
linked_list *get_tip()
{
linked_list *tmp=my;while(tmp->next!=NULL){tmp=tmp->next;}return tmp;
}
void kill(linked_list *target)
{
if(target!=NULL)
{
linked_list *t=search(target);
if(t!=NULL) //means if target found in list..
{
//time to verify weather target is on root node or tip or b/w some where..
if(t->next==NULL &&t->prev!=NULL)//tip node...
{
t->prev->next=t->next;
linked_list* target_node=t->get_root();
free(t);
}
else if(t->next!=NULL &&t->prev!=NULL) //centernode
{
t->prev->next=t->next;
t->next->prev=t->prev;
free(t);
}
else if(t->prev==NULL) //root node....
{
return;
}
}
else
puts("!!TARGET NOT FOUND IN LIST TO KILL..");return;
}
else
{
puts("\n!!CANT KILL NULL POINTER\n");return;
}
}
linked_list* search(linked_list *target)
{
linked_list *dummyPtr=(linked_list*)(get_root());
while(dummyPtr!=NULL)
{
if(dummyPtr==target)
{
return dummyPtr; //if found..
}
else
dummyPtr=dummyPtr->next;
}
return NULL; //if not found...
}
};
linked_list* init_List()
{
linked_list *ref;
ref=(linked_list*)malloc(sizeof(linked_list));
ref->my=ref;
ref->payload=NULL;
ref->next=NULL;
ref->prev=NULL;
return ref;
}
class LIST_LINKED
{
int nodes;
void copyNode(linked_list *dst,linked_list *src)
{
dst->my=src->my;
dst->next=src->next;
dst->payload=src->payload;
dst->prev=src->prev;
printf("\nLIST_LINKED::cpynode()->self ptr:%p payload ptr:%p self-next ptr:%p",src->my,src->payload,src->next);
return;
}
public:
linked_list *list,*root;
LIST_LINKED()
{
list=init_List();
root=list;
nodes=1;
}
linked_list *get_Node( int index)
{
if(index<0)
{
printf("\nLIST_LINKED::OPERATOR::get_Node()->!!wrong argument (index < 0)");
abort();
}
else if(index>getsize())
return NULL;
else
{
linked_list *tmp=getFirstNode();
for(int iter=0;iter<index;iter++)
{
tmp=tmp->next;
}
return tmp;
}
}
void copyTo(LIST_LINKED &dest)
{
if(!dest.isEmpty())
{
dest.destroy();
}
linked_list *iter=getFirstNode();
printf("\n\nCPY::FUNC\n");
while(dest.getsize()!=getsize())
{
dest.insert(iter->payload);
payload *dat=(payload*)dest.list->payload;
printf("%s\n",dat->str);
iter=iter->next;
}
}
void insert(void *food)
{
if(food!=NULL){
list=list->attach_next(food);
nodes+=1;}
else
{puts("null pointer cant be listed..");
return;}
}
linked_list *getFirstNode()
{
return (list->get_root()->next);
}
int getsize()
{
return (nodes-1);
}
void destroy()
{
printf("\n\n FUNCTION DESTROY CALLED..");
if(list->next==NULL && list->prev==NULL)
{
puts("\nLIST_LINKED::OPERATION::destroy()->list empty ...nothing to destroy");
return;
}
while(getsize()!=0)
{
#pragma warning(suppress: 6273)
printf("\nLIST_LINKED::OPERATION::destroy()-> list node mem block:0x%x size remaining(%d)",list,getsize());
list=list->prev;
free(list->next);
list->next=NULL;
nodes-=1;
}
#pragma warning(suppress: 6273)
printf("\nseed node block:%x list r/w header node:%x",root,list);
/*copyNode(list,dummyPtr);
list=dummyPtr;
*/
// printf("\n\n data::lost%x %x %x",list,root,dummyPtr);
printf("\nremaining nodes:%d list seed block :0x%x",getsize(),root);
//printf("\n\nlist data::%x",list->next);
}
void viewAll(bool newline)
{
linked_list *dummyPtr=root;
if(newline==false)
while(dummyPtr!=NULL)
{
//printf("||tree NODE:0x%x # list NODE:0x%x|||==>",dummyPtr->payload,dummyPtr->my);
dummyPtr=dummyPtr->next;
}
else
while(dummyPtr!=NULL)
{
#pragma warning(suppress: 6273)
printf("|tree NODE:0x%x # list NODE:0x%x||",dummyPtr->payload,dummyPtr->my);
printf("\n || \n");printf(" \\/ \n");
dummyPtr=dummyPtr->next;
}
printf("XXXNULL");
}
bool isEmpty()
{
return (getsize()==0 ? true:false);
}
};
class STACK_CHILD
{
linked_list *top;
int stacks;
linked_list* get_end()
{
linked_list *dummyPtr=top;
while(dummyPtr->prev!=NULL)
{
dummyPtr=dummyPtr->prev;
}
return dummyPtr;
}
public:
STACK_CHILD()
{
top=init_List();
//printf("\n0x%x",top);
stacks=1;
}
void push(void *link)
{
linked_list* tmp=init_List();
tmp->payload=link; //link to payload..
tmp->prev=top; //backlink
top=tmp;
stacks+=1;
}
void* pop()
{
void *tmp;
tmp=top->payload;
/*payload *data=(payload*)tmp;
printf("\ndata :%s",data->str);
*/
linked_list *iter=top;
if(top->prev!=NULL)
{
top=top->prev; //mov to prev node
//printf("\n0x%x",iter);
free(iter);
//puts("payload deleted");
stacks-=1;
return tmp;
}
else
{puts("STACK::OPERATION::pop()->null exception in stack operation...");
if(top->payload!=NULL)
{free(top->payload);
top->payload=NULL;
}
top->next=top->prev=NULL;
}
}
void invert()
{
STACK_CHILD *stack;
linked_list *list_buffer=(linked_list*)malloc(sizeof(linked_list)*stacks),*dummyPtr=top;
for(int i=0;i<stacks;i++)
{
list_buffer[i].payload=pop();
}
for(int i=(stacks-1);i<=0;i--)
{
push(list_buffer[i].payload);
}
}
void viewStack()
{
if(top->prev==NULL && top->next==NULL) //first stack payload ...no data member further..
{
puts("STACK::OPERATION::viewstack()->!error----stack empty..");
return;
}
else
{
linked_list *dummyPtr=top;
do
{
#pragma warning(suppress: 6273)
printf("\nSTACK::OPERATION::viewstack()->stack element (payload)mem block.:0x%x stack node at:0x%3x",dummyPtr->payload,dummyPtr);
dummyPtr=dummyPtr->prev;
}while(dummyPtr->next!=NULL);
}
}
int get_stack()
{ return (stacks-1);}
void clearStack()
{
if(!isEmpty())
{
while(get_stack()!=0)
{
pop();
}
}
else
{
printf("\nSTACK::OPERATION::clearstack()->stack is empty");
return ;
}
}
bool isEmpty()
{
return (get_stack()==0 ? true:false);
}
};
/*
struct N_ARYTREE
{
long long int data; //int key
int number_of_friends; //total no of neighbour
N_ARYTREE *my,*parent;
linked_list *friends,*root;
N_ARYTREE* allocator()
{
N_ARYTREE * tmp=(N_ARYTREE*)malloc(sizeof(N_ARYTREE));
tmp->my=tmp;
tmp->friends=init_List();
tmp->root=tmp->friends; //setup root pointer...
tmp->parent=NULL;
tmp->data=0;
tmp->number_of_friends=0;
return tmp;
}
void attach_friend(int food)
{
N_ARYTREE *tmp=allocator(); //new friend allocated..
//time to attach with list...
my->friends=my->friends->attach_next(tmp);
my->friends->pointer->data=food;
my->friends->pointer->parent=my; // set parent pointer to friends...
my->number_of_friends+=1;
//friend attached to list..
}
void traverse_relations( N_ARYTREE *parent)
{
clock_t fire=clock(),end;
int traversals=0;
//visit root node an stack all its friends...
N_ARYTREE *dummyPtr=parent;
STACK_CHILD stack;
linked_list *list=dummyPtr->root->next;
printf("\nstacks:%d\n",stack.get_stack());
//check for friends..
hell:
while(list!=NULL)
{
printf("\npushing friends pointer: 0x%x to stack dummyRoot: 0x%x",list->pointer,dummyPtr->my);
stack.push(list->pointer);
list=list->next;
}
printf("\nstacks:%d\n",stack.get_stack());
while(stack.get_stack()!=0)
{dummyPtr=stack.pop();
printf(" dummy pointer location:0x%x location data:%d",dummyPtr->my,dummyPtr->data);
traversals+=1;
if(dummyPtr->root->next!=NULL)
{list=dummyPtr->root->next;
goto hell;}
else
goto hell;
}
end=clock();
printf("time taken in traversal:%f sec..",(double)(end-fire)/CLOCKS_PER_SEC);
printf("\n %d traversal(TOTAL) done",(traversals+1));
}
linked_list* searchFriend(N_ARYTREE *target)
{N_ARYTREE *base=my;
linked_list *list=base->root->next;
int pos=1;
while(list!=base->friends)
{
if(list->pointer==target)
{
return list;
}
else
{
list=list->next;
pos+=1;
}
}
return NULL;
}
void killFriend(N_ARYTREE *target)
{
//the problem is how to identify to root node.....b'cause root node must not be deallocated
//the way to recog.root ndoe is to check if parent node of given node ==NULL...because how i made the program...then root node will only be the node which lack parent node.
//to avoid program from runtime crashing I need to avoid deallocation of root node..in case root node was asked for kill>?! then all of its friends and pointers will be deallocated..
if(target->parent!=NULL) //if given node is not a parent node....
{
N_ARYTREE *dummyparent=target->parent;
dummyparent->friends->next=target->root->next;
dummyparent->friends=target->friends;
dummyparent->number_of_friends+=target->number_of_friends;
//time to search target node in friends list of parent node...
printf("%d",dummyparent->data);
linked_list *dummyPtr=searchFriend(target);
if(dummyPtr!=NULL)
{
dummyparent->root->kill(dummyPtr);
dummyparent->number_of_friends-=1;
free(target);
}
else
{
puts("!ERROR IN FINDING TARGET NODE..");
return ;
}
}
else
{
puts("!ROOT NODE DELETION::REQUEST DENIED!!");
return;
}
}
};
N_ARYTREE* init_tree()
{
N_ARYTREE* tmp=( N_ARYTREE* )malloc(sizeof( N_ARYTREE ));
tmp->my=tmp;
tmp->friends=init_List();
tmp->root=tmp->friends;
tmp->parent=NULL; ///since it is an independent node ...No parent..
tmp->data=0;
tmp->number_of_friends=0;
return tmp;
}
*/
/*
void main()
{
N_ARYTREE *node=init_tree();
node->data=10;
node->attach_friend(1000);
node->attach_friend(2000);
node->attach_friend(9000);
node->attach_friend(4738247);
node->traverse_relations(node);
STACK_CHILD stack;
stack.push(node);
stack.push(node->friends->pointer);
printf("%d",(stack.pop())->data);
while(1)
printf("\n\n\n%f",(float)(rand()*0.00001));
_getch();
}
*/
#endif