-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink_list.c
1407 lines (1201 loc) · 30.8 KB
/
link_list.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
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*! @file link_list.c
@brief
Contains definitions of routines supported by link list
*/
#include "link_list.h"
void add_begin_sll(t_gen d,t_gen data);
void add_begin_dll(t_gen d,t_gen data);
void add_begin_scll(t_gen d,t_gen data);
void add_begin_dcll(t_gen d,t_gen data);
void add_begin_xor_dll(t_gen d,t_gen data);
void add_end_sll(t_gen d,t_gen data);
void add_end_dll(t_gen d,t_gen data);
void add_end_scll(t_gen d,t_gen data);
void add_end_dcll(t_gen d,t_gen data);
void add_end_xor_dll(t_gen d,t_gen data);
t_gen del_node_sll(t_gen d, t_gen data);
t_gen del_node_dll(t_gen d, t_gen data);
t_gen del_node_scll(t_gen d, t_gen data);
t_gen del_node_dcll(t_gen d, t_gen data);
t_gen del_node_xor_dll(t_gen d, t_gen data);
t_gen del_node_sll_idx(t_gen d, int idx);
t_gen del_node_dll_idx(t_gen d, int idx);
t_gen del_node_scll_idx(t_gen d, int idx);
t_gen del_node_dcll_idx(t_gen d, int idx);
t_gen del_node_xor_idx(t_gen d, int idx);
int linklist_length(t_gen d);
t_gen linklist_find(t_gen d, t_gen data);
t_gen linklist_getnode(t_gen d, int idx);
void linklist_print (t_gen d);
void linklist_print_info (t_gen d);
void destroy_link_list (t_gen d);
t_gen linklist_get_head(t_gen);
t_gen linklist_get_tail(t_gen);
t_gen linklist_get_end(t_gen);
t_gen linklist_get_next(t_gen, t_gen);
t_gen linklist_get_prev(t_gen, t_gen);
t_gen linklist_get_node_data(t_gen);
t_gen xor(t_gen x, t_gen y);
/// Look Up function ptrs for add based on type of list
f_ins add[] = {add_begin_sll,add_begin_dll,add_begin_scll,add_begin_dcll, add_begin_xor_dll};
/// Look Up function ptrs for apend (add end) based on type of list
f_ins append[] = {add_end_sll,add_end_dll,add_end_scll,add_end_dcll, add_end_xor_dll};
/// Look Up function ptrs for delete node based on type of list
f_del del[] = {del_node_sll, del_node_dll, del_node_scll, del_node_dcll, del_node_xor_dll};
/// Look Up function ptrs for delete ith node based on type of list
f_del_idx del_idx[] = {del_node_sll_idx, del_node_dll_idx, del_node_scll_idx, del_node_dcll_idx, del_node_xor_idx};
/*! @brief
* Create an instance of link list
* @param name - Name of link list instance
* @param type - Type of link list to be created
* @param prm - Data type specific parameters
* @return - Pointer to instance of link list
* */
t_gen create_link_list (char *name, e_lltype type, t_dparams *prm)
{
t_linklist *l = (t_linklist*)get_mem(1, sizeof(t_linklist));
// Initailze LL Params
l->name = name;
l->type = type;
l->count = 0;
l->tail = l->head = NULL;
// Select Functions based on type of list
l->append = append[type];
l->add = add[type];
l->del = del[type];
l->del_idx = del_idx[type];
l->get_idx = linklist_getnode;
l->len = linklist_length;
l->find = linklist_find;
l->head_node = linklist_get_head;
l->tail_node = linklist_get_tail;
l->end_node = linklist_get_end;
l->next_node = linklist_get_next;
l->prev_node = linklist_get_prev;
l->destroy = destroy_link_list;
l->print = linklist_print;
l->print_info = linklist_print_info;
l->get_node_data = linklist_get_node_data;
l->cmpr = prm->cmpr;
l->swap = prm->swap;
l->print_data = prm->print_data;
l->free = prm->free;
return (t_gen)l;
}
/*! @brief
* Fetch node data;
* @param data - Pointer to the node whose data to fetch
* @return - Node data
*/
t_gen linklist_get_node_data(t_gen n)
{
t_llnode *node = (t_llnode*)n;
return node->data;
}
/*! @brief
* Add Beggining for singly LL
* @param d - Pointer to instance of link list
* @param data - Pointer to the data to be added
* @return - NA
* */
void add_begin_sll(t_gen d, t_gen data)
{
t_linklist *l = (t_linklist*)d;
t_llnode *node = (t_llnode*)get_mem(1, sizeof(t_llnode));
// Create a node and assign data
node->data = data;
// Link cur head to new node
node->nxt = l->head;
// update tail
if (l->head == NULL) {
l->tail = node;
}
// Update head to new node
l->head = node;
l->count++;
}
/*! @brief
* Add Beggining for doubly LL
* @param d - Pointer to instance of link list
* @param data - Pointer to the data to be added
* @return - NA
* */
void add_begin_dll(t_gen d, t_gen data)
{
t_linklist *l = (t_linklist*)d;
t_llnode *node = (t_llnode*)get_mem(1, sizeof(t_llnode));
// Create a node and assign data
node->data = data;
// Link cur head to new node
node->nxt = l->head;
// ground head for head node
node->prv = NULL;
// Update Prv node link if non empty list
if (l->head != NULL) {
l->head->prv = node;
} else {
l->tail = node;
}
// Update head to new node
l->head = node;
l->count++;
}
/*! @brief
* Add Beggining for Singly Circular LL
* @param d - Pointer to instance of link list
* @param data - Pointer to the data to be added
* @return - NA
* */
void add_begin_scll(t_gen d,t_gen data)
{
t_linklist *l = (t_linklist*)d;
t_llnode *node = (t_llnode*)get_mem(1, sizeof(t_llnode));
// Create a node and assign data
node->data = data;
// Link cur head to new node
node->nxt = l->head;
// Update head to new node
l->head = node;
if (l->tail != NULL) {
// Circ Link tail to head
l->tail->nxt = l->head;
} else {
// List Empty update head & tail
l->tail = l->head;
}
l->count++;
}
/*! @brief
* Add Beggining for Doubly Circular LL
* @param d - Pointer to instance of link list
* @param data - Pointer to the data to be added
* @return - NA
**/
void add_begin_dcll(t_gen d,t_gen data)
{
t_linklist *l = (t_linklist*)d;
t_llnode *node = (t_llnode*)get_mem(1, sizeof(t_llnode));
// Create a node and assign data
node->data = data;
// Circ Link new node prv & nxt with tail and head respect
node->nxt = l->head;
node->prv = l->tail;
if (l->tail != NULL) {
// Rvrs link cur head to new node(head)
l->head->prv = node;
l->head = node;
// Circ Link tail to head
l->tail->nxt = l->head;
} else {
// List Empty update head & tail
l->tail = l->head = node ;
}
l->count++;
}
/*! @brief
* Util fuction to complete xor operation between two pointers
* returns the xor'ed result as a t_gen type
* */
t_gen xor(t_gen x, t_gen y)
{
return (t_gen)((uintptr_t)(x) ^ (uintptr_t)(y));
}
/*! @brief
* Add begin in xor link list
* @param d - Pointer to instance of link list
* @param data - Pointer to the data to be added
* @return - NA
* */
void add_begin_xor_dll(t_gen d, t_gen data)
{
t_linklist *l = (t_linklist*)d;
t_llnode *node;
// create node and store data
node = (t_llnode*)get_mem(1, sizeof(t_llnode));
node->data = data;
//node->nxt = l->head ^ NULL
node->nxt = xor(l->head ,NULL);
if (l->head != NULL)
{
// In order to get the address of the next node, XOR it with `NULL`
//l->head->nxt = (l->head->nxt ^ NULL) ^ node;
l->head->nxt = xor(node, xor(l->head->nxt, NULL));
}
// update tail
if (l->head == NULL) {
l->tail = node;
}
// Update head to new node
l->head = node;
l->count++;
}
/*! @brief
* Add End for Singly LL
* @param d - Pointer to instance of link list
* @param data - Pointer to the data to be added
* @return - NA
* */
void add_end_sll(t_gen d,t_gen data)
{
t_linklist *l = (t_linklist*)d;
t_llnode *node = (t_llnode*)get_mem(1, sizeof(t_llnode));
// Create a node and assign data
node->data = data;
node->nxt = NULL;
if (l->head == NULL) {
// List Empty upadte head
l->head = node;
} else {
// Add new node as nxt of cur tail
l->tail->nxt = node;
}
// Add new node as tail
l->tail = node;
l->count++;
}
/*! @brief
* Add End for Doublly LL
* @param d - Pointer to instance of link list
* @param data - Pointer to the data to be added
* @return - NA
* */
void add_end_dll(t_gen d,t_gen data)
{
t_linklist *l = (t_linklist*)d;
t_llnode *node = (t_llnode*)get_mem(1, sizeof(t_llnode));
// Create a node and assign data
node->data = data;
node->prv = node->nxt = NULL;
if (l->head == NULL) {
// List Empty upadte head
l->head = node;
} else {
// Rev link to cur tail
node->prv = l->tail;
// Add new node as nxt of cur tail
l->tail->nxt = node;
}
// Add new node as tail
l->tail = node;
l->count++;
}
/*! @brief
* Add End for Singly Circular LL
* @param d - Pointer to instance of link list
* @param data - Pointer to the data to be added
* @return - NA
* */
void add_end_scll(t_gen d,t_gen data)
{
t_linklist *l = (t_linklist*)d;
t_llnode *node = (t_llnode*)get_mem(1, sizeof(t_llnode));
// Create a node and assign data
node->data = data;
node->nxt = NULL;
if (l->head == NULL) {
// List Empty upadte head
l->head = node;
} else {
// Add new node as nxt of cur tail
l->tail->nxt = node;
}
// Add new node as tail
l->tail = node;
// Circular link tail to head
l->tail->nxt = l->head;
l->count++;
}
/*! @brief
* Add End for Doubly Circular LL
* @param d - Pointer to instance of link list
* @param data - Pointer to the data to be added
* @return - NA
* */
void add_end_dcll(t_gen d,t_gen data)
{
t_linklist *l = (t_linklist*)d;
t_llnode *node = (t_llnode*)get_mem(1, sizeof(t_llnode));
// Create a node and assign data
node->data = data;
node->prv = node->nxt = NULL;
if (l->head == NULL) {
// List Empty upadte head
l->head = node;
} else {
l->tail->nxt = node;
node->prv = l->tail;
}
// Add new node as tail
// Rev link head to new tail
l->head->prv = l->tail = node;
// Circular link tail to head
l->tail->nxt = l->head;
l->count++;
}
/*! @brief
* add end in xor link list
* @param d - Pointer to instance of link list
* @param data - Pointer to the data to be added
* @return - NA
* */
void add_end_xor_dll(t_gen d, t_gen data)
{
t_linklist *l = (t_linklist*)d;
t_llnode *node;
// Create a node and assign data
node = (t_llnode*)get_mem(1, sizeof(t_llnode));
node->data = data;
// node->nxt = l->tail ^ NULL;
node->nxt = xor(l->tail, NULL);
if (l->head == NULL) {
// List Empty upadte head
//l->head = (t_llnode *)XOR(node, l->head) ;
l->head = l->tail = node;
} else {
// Add new node as nxt of cur tail
//l->tail->nxt = (l->tail->nxt ^ NULL) ^ node;
l->tail->nxt = xor(node, xor(l->tail->nxt, NULL));
}
// Add new node as tail
l->tail = node;
l->count++;
}
/*! @brief
* Delete node with matching data in singly LL
* and return the instance
* @param d - Pointer to instance of link list
* @param data - Pointer to the data which has to be deleted
* @return - Pointer to the data
* */
t_gen del_node_sll(t_gen d, t_gen data)
{
t_linklist *l = (t_linklist *)d;
t_llnode *cur = l->head, *prv;
t_gen tmp = NULL;
// empty list
if (l->head == NULL) {
LOG_WARN("LINK_LIST", "%s: No nodes exist\n",l->name);
return tmp;
}
// Head node is to be deleted and new head is updated
if (l->cmpr(cur->data, data) == eEQUAL) {
l->head = cur->nxt;
cur->nxt = NULL;
tmp = cur->data;
free_mem(cur);
l->count--;
// Reset Tail to NULL if list empty
l->tail = l->head? l->tail : NULL;
return tmp;
}
// Find the node to be deleted
while (l->cmpr(cur->data, data) != eEQUAL) {
prv = cur;
cur = cur->nxt;
// Node to be deleted not found
if (cur == NULL) {
LOG_INFO("LINK_LIST", "%s: No node found within the link list\n",l->name);
return tmp;
}
}
// Unlink cur node and update link
prv->nxt = cur->nxt;
// If last node deleted update tail ref
if (prv->nxt == NULL) {
l->tail = prv;
}
l->count--;
// Free node
cur->nxt = NULL;
tmp = cur->data;
free_mem(cur);
return tmp;
}
/*! @brief
* Delete node with matching data in Doublly LL
* and return the instance
* @param d - Pointer to instance of link list
* @param data - Pointer to the data which has to be deleted
* @return - Pointer to the data
* */
t_gen del_node_dll(t_gen d, t_gen data)
{
t_linklist *l = (t_linklist *)d;
t_llnode *cur = l->head;
t_gen tmp = NULL;
// empty list
if (l->head == NULL) {
LOG_WARN("LINK_LIST", "%s: No nodes exist\n",l->name);
return tmp;
}
// Head node is to be deleted and new head is updated
if (l->cmpr(cur->data, data) == eEQUAL) {
l->head = cur->nxt;
if (l->head != NULL) {
l->head->prv = NULL;
}
l->count--;
// Reset Tail to NULL if list empty
l->tail = l->head? l->tail : NULL;
cur->nxt = cur->prv = NULL;
tmp = cur->data;
free_mem(cur);
return tmp;
}
// Find the node to be deleted
while (l->cmpr(cur->data, data) != eEQUAL) {
cur = cur->nxt;
// Node to be deleted not found
if(cur == NULL) {
LOG_INFO("LINK_LIST", "%s: No node found within the link list\n",l->name);
return tmp;
}
}
// Unlink cur node and update fwd link
cur->prv->nxt = cur->nxt;
if (cur->nxt == NULL) {
// If last node deleted update tail ref
l->tail = cur->prv;
} else {
// Preserve rev link
cur->nxt->prv = cur->prv;
}
// Free node
l->count--;
cur->nxt = cur->prv = NULL;
tmp = cur->data;
free_mem(cur);
return tmp;
}
/*! @brief
* Delete node with matching data in Singly Circular LL
* and return the instance
* @param d - Pointer to instance of link list
* @param data - Pointer to the data which has to be deleted
* @return - Pointer to the data
* */
t_gen del_node_scll(t_gen d, t_gen data)
{
t_linklist *l = (t_linklist *)d;
t_llnode *cur = l->head, *prv;
t_gen tmp = NULL;
// empty list
if (l->head == NULL) {
LOG_WARN("LINK_LIST", "%s: No nodes exist\n",l->name);
return tmp;
}
// Head node is to be deleted and new head is updated
if (l->cmpr(cur->data, data) == eEQUAL) {
// Unlink cur node and update fwd link for tail
l->tail->nxt = l->head = cur->nxt;
l->count--;
// Reset Tail to NULL if list empty
if (l->count == 0) {
l->tail = l->head = NULL;
}
cur->nxt = NULL;
tmp = cur->data;
free_mem(cur);
return tmp;
}
// Find the node to be deleted
while (l->cmpr(cur->data, data) != eEQUAL) {
prv = cur;
cur = cur->nxt;
// Node to be deleted not found
if (cur == l->head) {
LOG_INFO("LINK_LIST", "%s: No node found within the link list\n",l->name);
return tmp;
}
}
l->count--;
// Unlink cur node and update fwd link
prv->nxt = cur->nxt;
// If last node deleted update tail ref
if (prv->nxt == l->head) {
l->tail = prv;
}
// Free node
cur->nxt = NULL;
tmp = cur->data;
free_mem(cur);
return tmp;
}
/*! @brief
* Delete node with matching data in Doubly Circular LL
* and return the instance
* @param d - Pointer to instance of link list
* @param data - Pointer to the data which has to be deleted
* @return - Pointer to the data
* */
t_gen del_node_dcll(t_gen d, t_gen data)
{
t_linklist *l = (t_linklist *)d;
t_llnode *cur = l->head;
t_gen tmp;
// empty list
if (l->head == NULL) {
LOG_WARN("LINK_LIST", "%s: No nodes exist\n",l->name);
return tmp;
}
// Head node is to be deleted and new head is updated
if (l->cmpr(cur->data, data) == eEQUAL) {
l->head = cur->nxt;
// Unlink cur node and update fwd and rev link
l->head->prv = l->tail;
l->tail->nxt = l->head;
l->count--;
// Reset Tail to NULL if list empty
if (l->count == 0) {
l->tail = l->head = NULL;
}
cur->nxt = cur->prv = NULL;
tmp = cur->data;
free_mem(cur);
return tmp;
}
// Find the node to be deleted
while (l->cmpr(cur->data, data) != eEQUAL) {
cur = cur->nxt;
if(cur == l->head) {
LOG_INFO("LINK_LIST", "%s: No node found within the link list\n",l->name);
return tmp;
}
}
// Unlink cur node and update fwd
cur->prv->nxt = cur->nxt;
if (cur->nxt == l->head) {
// If last node deleted update tail ref
l->head->prv = l->tail = cur->prv;
} else {
cur->nxt->prv = cur->prv;
}
l->count--;
// Free node
cur->nxt = cur->prv = NULL;
tmp = cur->data;
free_mem(cur);
return tmp;
}
/*! @brief
* Delete node with matching data in xor link list
* @param d - Pointer to instance of link list
* @param data - Pointer of data for the node to be deleted
* @return - Pointer to the data of the deleted node
* */
t_gen del_node_xor_dll(t_gen d, t_gen data)
{
t_linklist *l = (t_linklist *)d;
t_llnode *cur = l->head;
t_llnode *prev = NULL , *next = NULL;
t_gen tmp = NULL;
// empty list
if (l->head == NULL) {
LOG_WARN("LINK_LIST", "%s: No nodes exist\n",l->name);
return tmp;
}
// Head node is to be deleted and new head is updated
if (l->cmpr(cur->data, data) == eEQUAL) {
// l->head = cur->nxt ^ prev;
l->head = xor(cur->nxt, prev);
// update reverse link
// Reset Tail to NULL if list empty
if (l->head != NULL) {
l->head->nxt = xor(prev, xor(l->head->nxt, cur));
}
else {
l->tail = NULL;
}
l->count--;
// Free node
tmp = cur->data;
cur->nxt = NULL;
free_mem(cur);
return tmp;
}
// Find the node to be deleted
while (l->cmpr(cur->data, data) != eEQUAL) {
//next = cur->nxt ^ prev;
next = xor(cur->nxt, prev);
// Node to be deleted not found
if(next == NULL) {
LOG_INFO("LINK_LIST", "%s: No node found within the link list\n",l->name);
return tmp;
}
prev = cur;
cur = next;
}
// next = cur->nxt ^ prev;
next = xor(cur->nxt, prev);
// prev->nxt = (prev->nxt ^ cur) ^ (next) ;
prev->nxt = xor(next, xor(prev->nxt, cur)) ;
// Update tail when the last node is deleted
if (next == NULL) {
l->tail = prev;
}
else {
// next->nxt = (next->nxt ^ cur) ^ prev;
next->nxt = xor(prev, xor(next->nxt, cur));
}
// free node
l->count --;
tmp = cur->data;
cur->nxt = NULL;
free_mem(cur);
return tmp;
}
/*! @brief
* Delete node with matching index in singly LL
* and return the instance
* @param d - Pointer to instance of link list
* @param idx - index of the node to be deleted
* @return - Pointer to the data of the deleted node
* */
t_gen del_node_sll_idx(t_gen d, int idx)
{
t_linklist *l = (t_linklist *)d;
t_llnode *cur = l->head, *prv = NULL;
t_gen tmp = NULL;
// empty list
if (l->head == NULL) {
LOG_WARN("LINK_LIST", "%s: No nodes exist\n",l->name);
return tmp;
}
// idx >count
if (idx >= l->count && idx < 0) {
LOG_WARN("LINK_LIST", "%d: Index out of bounds, number of nodes that exist = %d\n",idx, l->count);
return tmp;
}
for(int i = 0; i < idx; i++) {
prv = cur;
cur = cur->nxt;
}
// Head node is to be deleted and new head is updated
if (idx == 0) {
l->head = cur->nxt;
// Reset Tail to NULL if list empty
l->tail = l->head? l->tail : NULL;
}
else {
// Unlink cur node and update link
prv->nxt = cur->nxt;
// If last node deleted update tail ref
if (prv->nxt == NULL) {
l->tail = prv;
}
}
l->count--;
// Free node
cur->nxt = NULL;
tmp = cur->data;
free_mem(cur);
return tmp;
}
/*! @brief
* Delete node with matching index in Doublly LL
* and return the instance
* @param d - Pointer to instance of link list
* @param idx - index of the node to be deleted
* @return - Pointer to the data of the deleted node
* */
t_gen del_node_dll_idx(t_gen d, int idx)
{
t_linklist *l = (t_linklist *)d;
t_llnode *cur = l->head;
t_gen tmp = NULL;
// empty list
if (l->head == NULL) {
LOG_WARN("LINK_LIST", "%s: No nodes exist\n",l->name);
return tmp;
}
// idx >list count
if (idx >= l->count && idx < 0) {
LOG_WARN("LINK_LIST", "%d: Index out of bounds, number of nodes that exist = %d\n",idx, l->count);
return tmp;
}
for(int i = 0; i < idx ; i ++) {
cur = cur->nxt;
}
// Head node is to be deleted and new head is updated
if (idx == 0) {
l->head = cur->nxt;
if (l->head != NULL) {
l->head->prv = NULL;
}
// Reset Tail to NULL if list empty
l->tail = l->head? l->tail : NULL;
}
// Unlink cur node and update fwd link
else {
cur->prv->nxt = cur->nxt;
if (cur->nxt == NULL) {
// If last node deleted update tail ref
l->tail = cur->prv;
} else {
// Preserve rev link
cur->nxt->prv = cur->prv;
}
}
// Free node
l->count--;
cur->nxt = cur->prv = NULL;
tmp = cur->data;
free_mem(cur);
return tmp;
}
/*! @brief
* Delete node with matching index in Singly Circular LL
* and return the instance
* @param d - Pointer to instance of link list
* @param idx - index of the node to be deleted
* @return - Pointer to the data of the deleted node
* */
t_gen del_node_scll_idx(t_gen d, int idx)
{
t_linklist *l = (t_linklist *)d;
t_llnode *cur = l->head, *prv;
t_gen tmp = NULL;
// empty list
if (l->head == NULL) {
LOG_WARN("LINK_LIST", "%s: No nodes exist\n",l->name);
return tmp;
}
// idx >list count
if (idx >= l->count && idx < 0) {
LOG_WARN("LINK_LIST", "%d: Index out of bounds, number of nodes that exist = %d\n",idx, l->count);
return tmp;
}
for(int i = 0; i < idx; i ++) {
prv = cur;
cur = cur->nxt;
// Node to be deleted not found
if (cur == l->head) {
LOG_INFO("LINK_LIST", "%s: No node of index %d found within the link list\n",l->name, idx);
return tmp;
}
}
// Head node is to be deleted and new head is updated
if (idx == 0) {
// Unlink cur node and update fwd link for tail
l->tail->nxt = l->head = cur->nxt;
l->count--;
// Reset Tail to NULL if list empty
if (l->count == 0) {
l->tail = l->head = NULL;
}
cur->nxt = NULL;
tmp = cur->data;
free_mem(cur);
return tmp;
}
else {
// Unlink cur node and update fwd link
prv->nxt = cur->nxt;
// If last node deleted update tail ref
if (prv->nxt == l->head) {
l->tail = prv;
}
}
// Free node
l->count--;
cur->nxt = NULL;
tmp = cur->data;
free_mem(cur);
return tmp;
}
/*! @brief
* Delete node with matching index in Doubly Circular LL
* and return the instance
* @param d - Pointer to instance of link list
* @param idx - index of the node to be deleted
* @return - Pointer to the data of the deleted node
* */
t_gen del_node_dcll_idx(t_gen d, int idx)
{
t_linklist *l = (t_linklist *)d;
t_llnode *cur = l->head;
t_gen tmp;
// empty list
if (l->head == NULL) {
LOG_WARN("LINK_LIST", "%s: No nodes exist\n",l->name);
return tmp;
}
// idx > list count
if (idx >= l->count && idx < 0) {
LOG_WARN("LINK_LIST", "%d: Index out of bounds, number of nodes that exist = %d\n",idx, l->count);
return tmp;
}
for(int i = 0; i < idx; i ++) {
cur = cur->nxt;
if(cur == l->head) {
LOG_INFO("LINK_LIST", "%s: No node of index %d found within the link list\n",l->name, idx);
return tmp;
}
}
// Head node is to be deleted and new head is updated
if (idx == 0) {
l->head = cur->nxt;
// Unlink cur node and update fwd and rev link
l->head->prv = l->tail;
l->tail->nxt = l->head;
l->count--;
// Reset Tail to NULL if list empty
if (l->count == 0) {
l->tail = l->head = NULL;
}
cur->nxt = cur->prv = NULL;
tmp = cur->data;
free_mem(cur);
return tmp;
}
else {
// Unlink cur node and update fwd
cur->prv->nxt = cur->nxt;
if (cur->nxt == l->head) {
// If last node deleted update tail ref
l->head->prv = l->tail = cur->prv;
} else {
cur->nxt->prv = cur->prv;
}
}