-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinked_list.cpp
More file actions
105 lines (87 loc) · 3.41 KB
/
linked_list.cpp
File metadata and controls
105 lines (87 loc) · 3.41 KB
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
#include "linked_list.h"
void PrintListToFile(FILE* filename, char* format, struct list_node *ptr) {
while ( ptr != NULL ) {
fprintf( filename, format, ptr->data );
ptr = ptr->next;
}
}
struct list_node* AddListElement(struct list_node *root, int new_data ) {
struct list_node *temp;
temp = (struct list_node*) malloc( sizeof(struct list_node) );
temp->data = new_data;
temp->next = root;
root = temp;
return root;
}
void SetDiffLeafListBfromA( struct tree *T, int A_index, int B_index ) {
struct list_node *temp, *previous, *ptrA, *ptrB;
previous = NULL;
ptrA = T->leaf_lists[ A_index ];
ptrB = T->leaf_lists[ B_index ];
while ( (ptrA != NULL) && (ptrB != NULL) ) {
if ( ptrA->data > ptrB->data ) {
/* If ptrA->data is greater than ptrB->data, advance ptrA (ptrA->data cannot be contained in B since all remaining elements
of B are less than ptrB->data) until ptrA->data <= ptrB->data */
previous = ptrA;
ptrA = ptrA->next;
} else if (ptrA->data == ptrB->data) {
/* If equal to, and there is no previous element in A (ptrA points to the head of the list), redefine the head of the list to the
next element since the head will be deleted. If not, set the previous element of A to point to the next element. */
if ( previous == NULL ) T->leaf_lists[A_index] = ptrA->next;
else previous->next = ptrA->next;
/* Delete the element of A */
temp = ptrA;
ptrA = ptrA->next;
ptrB = ptrB->next;
free(temp);
}
else {
/* If ptrA->data < ptrB->data, ptrB->data is no longer a possible element in A. Advance ptrB to its next element
until ptrB points to an element less than or equal to that of ptrA */
ptrB = ptrB->next;
}
}
}
void MergeLeafListBwithA( struct tree *T, int A_index, int B_index ) {
struct list_node *temp, *previous, *ptrA, *ptrB;
previous = NULL;
ptrA = T->leaf_lists[ A_index ];
ptrB = T->leaf_lists[ B_index ];
while ( ptrB != NULL ) {
if ( (ptrA == NULL) || (ptrA->data < ptrB->data) ) {
/* If ptrA is NULL, add the remaining elements of B to A. Otherwise, add all elements of B in front of ptrA
until ptrA->data is >= ptrB->data again. In both cases, be careful to preserve the reverse-sorted order of A. */
temp = (struct list_node*) malloc( sizeof(struct list_node) );
temp->data = ptrB->data;
temp->next = ptrA;
ptrB = ptrB->next;
/* If no element exists before ptrA, the new element added to A is the head of the list. Otherwise make the
previous element point to the newly added element */
if ( previous == NULL ) T->leaf_lists[ A_index ] = temp;
else previous->next = temp;
previous = temp;
}
else if (ptrA->data == ptrB->data) {
/* If the two elements are equal, skip over that element of A since it is already in A. Advance A and B to their next elements. */
previous = ptrA;
ptrA = ptrA->next;
ptrB = ptrB->next;
}
else {
/* If ptrA->data > ptrB->data, there is nothing to be added to A from B in front of ptrA (ptrB->data belongs somewhere behind ptrA) */
previous = ptrA;
ptrA = ptrA->next;
}
}
}
void FreeList(struct list_node *root) {
struct list_node *ptr = root;
struct list_node *temp;
/* While not at the end of the list, free elements starting at the root element */
while ( ptr != NULL ) {
temp = ptr;
ptr = ptr->next;
free(temp);
}
root = NULL;
}