-
Notifications
You must be signed in to change notification settings - Fork 1
/
doubly_linked_list_adt.c
1412 lines (1349 loc) · 44.5 KB
/
doubly_linked_list_adt.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
/*
* Project: Doubly Linked List C-API
* File: DoublyLinkedList_ADT.c
* Author: Chris Aslanoglou
* Github: https://github.com/chris-asl/doubly-linked-list-API
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include "doubly_linked_list_adt.h"
// Node type definition
typedef struct DoublyLinkedListNode *dllnodeptr;
struct DoublyLinkedListNode
{
dllnodeptr previous, next;
void* data;
};
// Iterator type definition
typedef struct DoublyLinkedListIterator dlliterator;
struct DoublyLinkedListIterator
{
dllnodeptr node;
IteratorID id;
};
// DLL ADT definition
struct DoublyLinkedList_ADT
{
dllnodeptr head, tail;
int size;
dlliterator *iteratorsArray;
int iteratorsCount;
IteratorID id_counter;
};
// Forward declaration of non API functions
/*
* Function responsible for taking two dllnodeptr acting as old and new
* finding all the iterators that point to old and updating them to point
* to the new node
* If the list is empty, it will delete all iterators
* Note: Will not used by the user
*/
void dll_iteratorUpdate(list_t, dllnodeptr, dllnodeptr);
/*
* Given an IteratorID this function returns the index of the array
* in which the Iterator is located (using binary search)
* Return values:
* [*] On success, the index is returned
* [*] On element not found, -1 is returned
*/
int dll_iteratorGetIdxWithID(list_t, IteratorID);
/*
* Given a dllnodeptr this function returns the index of the array
* in which the Iterator is located (using linear search)
* Return values:
* [*] On success, the index is returned
* [*] On element not found, -1 is returned
*/
int dll_iteratorGetIdxWithPtr(list_t, dllnodeptr);
int dll_iteratorBinarySearch(list_t, IteratorID, int, int);
/*
* Function responsible for initializing the Doubly Linked List ADT
* Return values:
* [*] On success, 0 is returned
* [*] On failure, -1 is returned
*/
int dll_init(list_t *listptr_addr)
{
(*listptr_addr) = malloc(sizeof(struct DoublyLinkedList_ADT));
if ((*listptr_addr) == NULL) {
perror("dll_init - Error initializing DoublyLinkedList ADT");
return -1;
}
(*listptr_addr)->head = NULL;
(*listptr_addr)->tail = NULL;
(*listptr_addr)->size = 0;
(*listptr_addr)->iteratorsArray = NULL;
(*listptr_addr)->iteratorsCount = 0;
(*listptr_addr)->id_counter = 0;
return 0;
}
/*
* Function returning the size of the list
*/
int dll_size(list_t list)
{
return list->size;
}
/*
* Function responsible for checking if the list is empty
* Return values:
* [*] on empty list, 1 is returned
* [*] on non empty list, 0 is returned
*/
int dll_isempty(list_t list)
{
if (list == NULL) {
fprintf(stderr, "dll_isempty - Error: DLList has not been initialized\n");
return -1;
}
if (dll_size(list) == 0)
return 1;
else
return 0;
}
/*
* Function responsible for printing the list in this fashion:
* "element -> element -> element" (delimiter "->")
* Return values:
* [*] On success, 0 is returned
* [*] On failure, -1 is returned
*/
void dll_print(const list_t list, void (*print_data)(void* data),
int print_inline)
{
if (list == NULL) {
fprintf(stderr, "dll_print - Error: DLList has not been initialized\n");
return;
}
if (dll_isempty(list)) {
printf("List is empty\n");
return;
}
else {
printf("=======Printing list======\n");
printf("List size: %d\n", list->size);
dllnodeptr current = list->head;
while (current != list->tail) {
if (print_inline) {
(*print_data)(current->data);
printf(" -> ");
}
else {
printf(" -> ");
(*print_data)(current->data);
putchar('\n');
}
current = current->next;
}
//print tail element
if (print_inline)
(*print_data)(current->data);
else {
printf(" -> ");
(*print_data)(current->data);
}
printf("\n====Done printing list====\n");
return;
}
}
/*
* Function responsible for inserting an element at the end of the list
* The 2nd and 3rd arguments are to be used in conjuction, as the data parameter
* acts as a dummy object which is going to be duplicated as a new object of the
* data member of the list node struct.
* The duplicate function is responsible for allocating a new object and
* assigning the values of source to destination.
* Return values:
* [*] On success, 0 is returned
* [*] On failure, -1 is returned
*/
int dll_insert_at_back(list_t list, void* data, void* (*duplicate)(void*))
{
// Safety checks firstly
// 1. Dllist must be initialized
if (list == NULL) {
fprintf(stderr, "dll_insert_at_back - Error: DLList has not been initialized\n");
return -1;
}
// 2. Data must not be NULL
if (data == NULL) {
fprintf(stderr, "dll_insert_at_back - Error: Data given is NULL\n");
return -1;
}
else if (dll_isempty(list)) {
dllnodeptr elem = malloc(sizeof(struct DoublyLinkedListNode));
if (elem == NULL) {
perror("dll_insert_at_back - Empty list case, error: ");
return -1;
}
list->head = elem;
list->tail = elem;
list->size++;
elem->next = NULL;
elem->previous = NULL;
elem->data = (*duplicate)(data);
return 0;
}
else {
dllnodeptr elem = malloc(sizeof(struct DoublyLinkedListNode));
if (elem == NULL) {
perror("dll_insert_at_back - Error: ");
return -1;
}
(list->tail)->next = elem;
elem->previous = list->tail;
list->tail = elem;
list->size++;
elem->next = NULL;
elem->data = (*duplicate)(data);
return 0;
}
}
/*
* Function responsible for inserting an element at the start of the list
* The 2nd and 3rd arguments are to be used in conjuction, as the data parameter
* acts as a dummy object which is going to be duplicated as a new object of the
* data member of the list node struct.
* The duplicate function is responsible for allocating a new object and
* assigning the values of source to destination.
* Return values:
* [*] On success, 0 is returned
* [*] On failure, -1 is returned
*/
int dll_insert_at_front(list_t list, void* data, void* (*duplicate)(void*))
{
// Safety checks firstly
// 1. Dllist must be initialized
if (list == NULL) {
fprintf(stderr, "dll_insert_at_front - Error: DLList has not been initialized\n");
return -1;
}
// 2. Data must not be NULL
if (data == NULL) {
fprintf(stderr, "dll_insert_at_front - Error: Data given is NULL\n");
return -1;
}
else if (dll_isempty(list)) {
dllnodeptr elem = malloc(sizeof(struct DoublyLinkedListNode));
if (elem == NULL) {
perror("dll_insert_at_front - Empty list case, error: ");
return -1;
}
list->head = elem;
list->tail = elem;
list->size++;
elem->next = NULL;
elem->previous = NULL;
elem->data = (*duplicate)(data);
return 0;
}
else {
dllnodeptr elem = malloc(sizeof(struct DoublyLinkedListNode));
if (elem == NULL) {
perror("dll_insert_at_front - Error: ");
return -1;
}
(list->head)->previous = elem;
elem->next = list->head;
list->head = elem;
elem->previous = NULL;
list->size++;
elem->data = (*duplicate)(data);
return 0;
}
}
/*
* Function responsible for inserting an element into the list while keeping it
* sorted by a comparison defined by the user with a function called
* issmaller(*). Elements are going to be inserted in ascending order starting
* from the head.
* The 4th argument (duplication function) is explained at the dll_insert_at_end
* function.
* Return values:
* [*] On success, 0 is returned
* [*] On failure, -1 is returned
*
* (*): issmaller must be a function that:
* [*] Returns 1, if (1st parameter < 2nd parameter)
* [*] Returns 0, if (1st parameter >= 2nd parameter)
*/
int dll_insert_sorted(list_t list, void* data,
int (*issmaller)(void*, void*), void* (*duplicate)(void*))
{
/*
* Scenarios:
* 1. list is empty:
* a. head and tail pointers point to the new node
* b. next and previous pointers point to the node itself
* 2. list isn't empty:
* find where in the list the element belongs (cases: )
* i. element is to be inserted at the end (bigger than
*/
// Safety checks firstly
// 1. Dllist must be initialized
if (list == NULL) {
fprintf(stderr, "dll_insert_sorted - Error: DLList has not been initialized\n");
return -1;
}
// 2. Data must not be NULL
if (data == NULL) {
fprintf(stderr, "dll_insert_sorted - Error: Data given is NULL\n");
return -1;
}
if (dll_isempty(list)) {
dllnodeptr elem = malloc(sizeof(struct DoublyLinkedListNode));
if (elem == NULL) {
perror("dll_insert_sorted: Empty Dllist case, error: ");
return -1;
}
list->head = elem;
list->tail = elem;
list->size++;
elem->next = NULL;
elem->previous = NULL;
elem->data = (*duplicate)(data);
return 0;
}
else {
//find where the new element is bigger than the tail
//so as to avoid the search method
if ( (*issmaller)((list->tail)->data, data) ) {
//case in which the element is to be added into the end of the list
dllnodeptr elem = malloc(sizeof(struct DoublyLinkedListNode));
if (elem == NULL) {
perror("dll_insert_sorted: Tail insertion case, error: ");
return -1;
}
(list->tail)->next = elem;
elem->previous = list->tail;
elem->next = NULL;
list->tail = elem;
list->size++;
elem->data = (*duplicate)(data);
return 0;
}
//search method
dllnodeptr current = list->head;
do {
if ( (*issmaller)(data, current->data) )
break;
else {
if (current == list->tail)
break;
else
current = current->next;
}
}while(1);
//here we must identify which break occurred
if (current == list->head) {
//case in which the element is to be added into the start of the list
dllnodeptr elem = malloc(sizeof(struct DoublyLinkedListNode));
if (elem == NULL) {
perror("dll_insert_sorted: Tail insertion case, error: ");
return -1;
}
elem->next = list->head;
elem->previous = NULL;
(elem->next)->previous = elem;
list->head = elem;
list->size++;
elem->data = (*duplicate)(data);
return 0;
}
else {
//add the element before the current node
dllnodeptr elem = malloc(sizeof(struct DoublyLinkedListNode));
if (elem == NULL) {
perror("dll_insert_sorted - Error: ");
return -1;
}
(current->previous)->next = elem;
elem->previous = current->previous;
current->previous = elem;
elem->next = current;
list->size++;
elem->data = (*duplicate)(data);
return 0;
}
}
}
/*
* Inserts the element `data` before the `key` element provided. If the key is
* not found -1 is returned.
* Return values:
* [*]: On success, 0 is returned
* [*]: On error or `on key not found`, -1 is returned
*/
int dll_insert_before(list_t list, void* data, void* (*duplicate)(void*),
void* key, int (*is_equal)(void*, void*))
{
// Safety checks firstly
// 1. Dllist must be initialized
if (list == NULL) {
fprintf(stderr, "dll_insert_before - Error: DLList has not been initialized\n");
return -1;
}
// 2. Data must not be NULL
if (data == NULL) {
fprintf(stderr, "dll_insert_before - Error: Data given is NULL\n");
return -1;
}
// 3. Key must not be NULL
if (key == NULL) {
fprintf(stderr, "dll_insert_before - Error: Key given is NULL\n");
return -1;
}
//find the element (if it exits)
dllnodeptr current = list->head;
do {
if ( (*is_equal)(key, current->data) ) {
//found correct place
//add the element before the current node
dllnodeptr elem = malloc(sizeof(struct DoublyLinkedListNode));
if (elem == NULL) {
perror("dll_insert_sorted - Error: ");
return -1;
}
elem->previous = current->previous;
elem->next = current;
if (current != list->head)
(current->previous)->next = elem;
else {
list->head = elem;
elem->previous = NULL;
}
current->previous = elem;
elem->data = (*duplicate)(data);
list->size++;
return 0;
}
else {
if (current == list->tail)
return -1;
else
current = current->next;
}
}while(1);
}
/*
* Inserts the element `data` after the `key` element provided. If the key is
* not found -1 is returned.
* Return values:
* [*]: On success, 0 is returned
* [*]: On error or `on key not found`, -1 is returned
*/
int dll_insert_after(list_t list, void* data, void* (*duplicate)(void*),
void* key, int (*is_equal)(void*, void*)) {
// Safety checks firstly
// 1. Dllist must be initialized
if (list == NULL) {
fprintf(stderr, "dll_insert_before - Error: DLList has not been initialized\n");
return -1;
}
// 2. Data must not be NULL
if (data == NULL) {
fprintf(stderr, "dll_insert_before - Error: Data given is NULL\n");
return -1;
}
// 3. Key must not be NULL
if (key == NULL) {
fprintf(stderr, "dll_insert_before - Error: Key given is NULL\n");
return -1;
}
//find the element (if it exits)
dllnodeptr current = list->head;
do {
if ( (*is_equal)(key, current->data) ) {
//found correct place
//add the element after the current node
dllnodeptr elem = malloc(sizeof(struct DoublyLinkedListNode));
if (elem == NULL) {
perror("dll_insert_sorted - Error: ");
return -1;
}
elem->next = current->next;
elem->previous = current;
if (current != list->tail)
(current->next)->previous = elem;
else {
list->tail = elem;
elem->next = NULL;
}
current->next = elem;
elem->data = (*duplicate)(data);
list->size++;
return 0;
}
else {
if (current == list->tail)
return -1;
else
current = current->next;
}
}while(1);
}
/*
* Function that enables accessing data at the list, identified by the 2nd
* argument, with an is_equal type function as 3rd argument
* Return values:
* [*] On success, the object is returned
* [*] On element not found or on error, NULL is returned
*/
void* dll_edit_data(list_t list, void* data, int (*is_equal)(void*, void*))
{
if (list == NULL) {
fprintf(stderr, "dll_edit_data - Error: DLList has not been initialized\n");
return NULL;
}
if(dll_isempty(list)) {
return NULL;
}
else {
//find the element (if it exits)
//search method
dllnodeptr current = list->head;
do {
if ( (*is_equal)(data, current->data) )
return current->data;
else {
if (current == list->tail)
return NULL;
else
current = current->next;
}
}while(1);
}
}
/*
* Function that returns a copy of the data located at the front (Head) of
* the list, if getCopy option is true, or the actual data if the option is
* set to 0 (false)
*/
const void* dll_get_front(list_t list, void* (*duplicate)(void*),
int getCopy) {
// check if list is null
if (list == NULL) {
fprintf(stderr, "dll_get_front - Error: DLList has not been initialized\n");
return NULL;
}
if(dll_isempty(list)) {
fprintf(stderr, "dll_get_front - Error: DLList is empty\n"
"\tDeleting all iterators now...\n");
// invalidate - delete all iterators
dll_iteratorDeleteAll(list);
return NULL;
}
if (getCopy)
return (*duplicate)(list->head->data);
else
return list->head->data;
}
/*
* Function that returns a copy of the data located at the back (Tail) of
* the list, if getCopy option is true, or the actual data if the option is
* set to 0 (false)
*/
const void* dll_get_back(list_t list, void* (*duplicate)(void*),
int getCopy) {
// check if list is null
if (list == NULL) {
fprintf(stderr, "dll_get_back - Error: DLList has not been initialized\n");
return NULL;
}
if(dll_isempty(list)) {
fprintf(stderr, "dll_get_back - Error: DLList is empty\n"
"\tDeleting all iterators now...\n");
// invalidate - delete all iterators
dll_iteratorDeleteAll(list);
return NULL;
}
if (getCopy)
return (*duplicate)(list->tail->data);
else
return list->tail->data;
}
/*
* Function that is responsible for copying src list to dest list
* Dest list must be initialized and empty
* Return values:
* [*] On success, 0 is returned
* [*] On failure, -1 is returned
* [*] On destination list not empty, 2 is returned
* [*] On empty source list, 1 is returned
*/
int dll_copy(list_t src, list_t dest, void* (*duplicate)(void*),
void (*free_data)(void*))
{
// check if list is null
if (src == NULL) {
fprintf(stderr, "dll_copy - Error: Source list has not been initialized\n");
return -1;
}
// check if list is null
if (dest == NULL) {
fprintf(stderr, "dll_copy - Error: Destination list has not been "
"initialized\n");
return -1;
}
if(!dll_isempty(dest)) {
fprintf(stderr, "dll_copy - Error: Destination list is not empty\n");
return 2;
}
if(dll_isempty(src)) {
fprintf(stderr, "dll_copy - Notify: Source list is empty\n");
// nothing to do here
return 1;
}
// iterate through source list
IteratorID srcIter = dll_iteratorRequest(src);
if (srcIter == -1) {
fprintf(stderr, "dll_copy - Error: Requesting iterator for the source "
"list failed\n");
return -1;
}
while (1) {
// insert data pointed
void* data = dll_iteratorGetObj(src, srcIter);
if (data == NULL) {
fprintf(stderr, "dll_copy - Error: Cannot access data from source"
" list\nDestroying destination list completely\n");
dll_destroy(&dest, free_data);
return -1;
}
if (dll_insert_at_back(dest, data, duplicate) == -1) {
fprintf(stderr, "dll_copy - Error: Cannot insert data to "
" destination list\n"
"Destroying destination list completely\n");
dll_destroy(&dest, free_data);
return -1;
}
int retval = dll_iteratorNext(src, srcIter);
if (retval == 2)
// reached end of the source list, end loop
break;
else if (retval == -1) {
fprintf(stderr, "dll_copy - Error: Cannot iterate through source "
"list\nDestroying destination list completely\n");
dll_destroy(&dest, free_data);
return -1;
}
}
return 0;
}
/*
* Function responsible for appending `list b` to `list a`
* Upon return, the second list is going to be freed
* and nullified, so that it cannot longer be used
*/
void dll_append(list_t alist, list_t* listptrb)
{
//make the tail of `list a` to point to the `list b` head
(alist->tail)->next = (*listptrb)->head;
//symmetrically
((*listptrb)->head)->previous = alist->tail;
//update `list a` tail
alist->tail = (*listptrb)->tail;
//update sizes
alist->size += (*listptrb)->size;
//free `list b`
(*listptrb)->head = NULL;
(*listptrb)->tail = NULL;
(*listptrb)->size = 0;
free(*listptrb);
listptrb = NULL;
}
/*
* Function responsible for deleting the element that contains the `key`
* given as a 2nd parameter
* Also, two pointers to functions needed, the 1st for identifying the
* correct element and the second one for freeing it
* Return values:
* [*] On success, 0 is returned
* [*] On element not found, 1 is returned
* [*] On error, -1 is returned
*/
int dll_delete(list_t list, void* key, int (*is_equal)(void*, void*),
void (*free_data)(void*))
{
if (list == NULL) {
fprintf(stderr, "dll_delete - Error: DLList has not been initialized\n");
return -1;
}
if (key == NULL) {
fprintf(stderr, "dll_delete - Error: Key parameter is NULL\n");
return -1;
}
if(dll_isempty(list)) {
fprintf(stderr, "dll_delete - Error: DLList is empty\n");
return 1;
}
else {
//find the element (if it exits and delete it)
//search method
dllnodeptr current = list->head;
do {
if ( (*is_equal)(key, current->data) )
break;
else {
if (current == list->tail)
{
current = NULL;
break;
}
else
current = current->next;
}
}while(1);
if (current == NULL) {
//element wasn't found
return 1;
}
else if (current == list->head) {
if (current != list->tail) {
//first node doesn't have previous
list->size--;
dll_iteratorUpdate(list, current, current->next);
(current->next)->previous = NULL;
list->head = current->next;
(*free_data)((void*) current->data);
free(current);
current = NULL;
}
else {
//case in which we are deleting the one and only
//element of the list
list->head = NULL;
list->tail = NULL;
list->size--;
(*free_data)((void*) current->data);
free(current);
current = NULL;
dll_iteratorUpdate(list, NULL, NULL);
}
}
else if (current == list->tail) {
list->size--;
dll_iteratorUpdate(list, current, current->previous);
list->tail = current->previous;
//last node doesn't have next
(current->previous)->next = NULL;
(*free_data)((void*)current->data);
free(current);
current = NULL;
}
else {
list->size--;
dll_iteratorUpdate(list, current, current->next);
(current->previous)->next = current->next;
(current->next)->previous = current->previous;
(*free_data)((void*)current->data);
free(current);
current = NULL;
}
return 0;
}
}
/*
* Deletes the tail element of the list
*/
void dll_delete_back(list_t list, void (*free_data)(void* data))
{
list->size--;
dllnodeptr deletion = list->tail;
if (list->size > 0) {
//take care of iterators that point to tail, so as to point to the new tail
//element of the list
dll_iteratorUpdate(list, deletion, deletion->previous);
list->tail = (list->tail)->previous;
(list->tail)->next = NULL;
(*free_data)(deletion->data);
deletion->data = NULL;
deletion->previous = NULL;
free(deletion);
deletion = NULL;
}
else {
// list is empty
// invalidate - delete all iterators
dll_iteratorDeleteAll(list);
list->head = NULL;
list->tail = NULL;
(*free_data)(deletion->data);
deletion->data = NULL;
deletion->next = NULL;
free(deletion);
deletion = NULL;
}
}
/*
* Deletes the head element of the list
*/
void dll_delete_front(list_t list, void (*free_data)(void* data))
{
list->size--;
dllnodeptr deletion = list->head;
if (list->size > 0) {
//take care of iterators that point to head, so as they point to the next
//element (new head) of the list
dll_iteratorUpdate(list, deletion, deletion->next);
list->head = (list->head)->next;
(list->head)->previous = NULL;
(*free_data)(deletion->data);
deletion->data = NULL;
deletion->next = NULL;
free(deletion);
deletion = NULL;
}
else {
// list is empty
// invalidate - delete all iterators
dll_iteratorDeleteAll(list);
list->head = NULL;
list->tail = NULL;
(*free_data)(deletion->data);
deletion->data = NULL;
deletion->next = NULL;
free(deletion);
deletion = NULL;
}
}
/*
* Function responsible for freeing all the allocated memory
*/
void dll_destroy(list_t *dllptr_addr, void (*free_data)(void* data))
{
if (*dllptr_addr == NULL) {
fprintf(stderr, "dll_destroy - Error: DLList has not been initialized\n");
return;
}
if(dll_isempty(*dllptr_addr)) {
//free iterators, if there are any
dll_iteratorDeleteAll(*dllptr_addr);
//free doubly linked list structure
free(*dllptr_addr);
*dllptr_addr = NULL;
return;
}
else {
dllnodeptr current = (*dllptr_addr)->head;
dllnodeptr to_be_deleted = NULL;
while(1) {
if (current == NULL)
break;
to_be_deleted = current;
//avoid dangling pointers
if (to_be_deleted == (*dllptr_addr)->head)
(*dllptr_addr)->head = NULL;
else if(to_be_deleted == (*dllptr_addr)->tail)
(*dllptr_addr)->tail = NULL;
current = current->next;
(*free_data)(to_be_deleted->data);
free(to_be_deleted);
to_be_deleted = NULL;
}
(*dllptr_addr)->size = 0;
//free iterators, if there are any
dll_iteratorDeleteAll(*dllptr_addr);
//free the doubly linked list structure
free(*dllptr_addr);
*dllptr_addr = NULL;
return;
}
}
/*
* Allocates a new Iterator object
* assigning it to the head of the list and then returning its id
* Return values:
* [*] On success, the ID of the Iterator is returned
* [*] On failure, -1 is returned
*/
IteratorID dll_iteratorRequest(list_t list)
{
// check if list is null
if (list == NULL) {
fprintf(stderr, "dll_iteratorRequest - Error: DLList has not been initialized\n");
return -1;
}
// provide iterator only if list isn't empty
if(dll_isempty(list)) {
fprintf(stderr, "dll_iteratorRequest - Error: DLList is empty\n"
"\tDeleting all iterators now...\n");
// invalidate - delete all iterators
dll_iteratorDeleteAll(list);
return 1;
}
void* tmp = realloc(list->iteratorsArray, (list->iteratorsCount + 1) * sizeof(dlliterator));
if (tmp == NULL) {
perror("dll_requestIterator - Error: Cannot allocate iterator");
return -1;
}
else
list->iteratorsArray = tmp;
(list->iteratorsArray[list->iteratorsCount]).id = list->id_counter;
list->iteratorsCount++;
// set Iterator to point to the head
if (dll_iteratorBegin(list, list->id_counter) < 0) {
fprintf(stderr, "dll_requestIterator - Error: Cannot set iterator to list head\n");
return -1;
}
return list->id_counter++;
}
int dll_iteratorGetIdxWithID(list_t list, IteratorID iterID)
{
if (list->iteratorsCount > 0)
return dll_iteratorBinarySearch(list, iterID, 0, list->iteratorsCount - 1);
else
return -1;
}
int dll_iteratorBinarySearch(list_t list, IteratorID key, int start, int end) {
int middle_idx = (end - start) / 2 + start;
int current_id = (list->iteratorsArray[middle_idx]).id;
if (current_id == key)
return middle_idx;
else if (start == end)
return -1;
else if (key < current_id && middle_idx > 0)
return dll_iteratorBinarySearch(list, key, 0, middle_idx - 1);
else if (key > current_id && middle_idx < list->iteratorsCount)
return dll_iteratorBinarySearch(list, key, middle_idx + 1, list->iteratorsCount - 1);
return -1;
}
int dll_iteratorGetIdxWithPtr(list_t list, dllnodeptr node)
{
int idx;
for (idx = 0; idx < list->iteratorsCount; idx++) {
if ((list->iteratorsArray[idx]).node == node)
return idx;
}
return -1;
}
/*
* Takes two nodes (dllnodeptr) acting as old and new
* finding all the iterators that point to old and updating them to point
* to the new node
* If the list is empty, it will delete all iterators
* Note: Will not used by the user
*/
void dll_iteratorUpdate(list_t list, dllnodeptr old, dllnodeptr new)
{
if (dll_isempty(list))
dll_iteratorDeleteAll(list);
else {
int idx;
for (idx = 0; idx < list->iteratorsCount; idx++) {
if ((list->iteratorsArray[idx]).node == old)
(list->iteratorsArray[idx]).node = new;
}
}
}
/*
* Sets the iterator with ID iterID
* to point to the head of the list
* Return values:
* [*] On success, 0 is returned
* [*] On failure, -1 is returned