-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorted-list.c
188 lines (165 loc) · 3.91 KB
/
sorted-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
#include "sorted-list.h"
SortedListPtr SLCreate(CompareFuncT cf){
SortedListPtr slp;
if((slp = (SortedListPtr)(malloc(sizeof(struct SortedList)))) == NULL){
return NULL;
}
slp->head = NULL;
slp->compare = cf;
slp->size = 0;
return slp;
}
void SLDestroy(SortedListPtr list){
free(list);
}
int SLInsert(SortedListPtr list, void *newObj){
node tempNode, currNode;
/*Create and allocate memory for new node*/
if((tempNode = (node)(malloc(sizeof(struct ListNode_)))) == NULL){
printf("Failed allocating memory for tempnode\n");
return 0;
}
tempNode->data = newObj;
tempNode->next = NULL;
/*Case where list is null*/
if(list == NULL){
printf("List is NULL\n");
return 0;
}
/*insert new node into appropriate spot using compare function*/
if(list->head == NULL){
list->head = tempNode;
list->size++;
/*printf("Added node to front of list\n");*/
return 1;
}
else{
/*case where new item is greater than the head of the list*/
if(list->compare(tempNode->data, list->head->data) < 0){
tempNode->next = list->head;
list->head = tempNode;
list->size++;
/*printf("node added to head of existing list\n");*/
return 1;
}
/*case where its somewhere in the list thats not the head*/
else{
currNode = list->head;
while(currNode->next != NULL){
if(list->compare(tempNode->data, currNode->data) > 0){
currNode = currNode->next;
}
else{
break;
}
}
if(list->compare(tempNode->data, currNode->data) != 0){
tempNode->next = currNode->next;
currNode->next = tempNode;
list->size++;
}
/*printf("node added somewhere in list\n");*/
return 1;
}
}
return 0;
}
int SLRemove(SortedListPtr list, void *newObj){
node ptr; /*pointer for traversing purpose*/
/*If the list is empty then there's nothing to remove*/
if(list == NULL){
return 0;
}
/*If the data in the head matches the argument then make the next node the new head and then remove the
original head*/
if(list->head->data == newObj)
{
ptr = list->head;
if(list->head->next == NULL){
list->head = NULL;
}
else{
list->head = list->head->next;
}
free(ptr);
list->size--;
}
/*For all other cases, traverse through the list to find the matching component to remove*/
ptr = list->head;
while(ptr != NULL)
{
if(ptr->next != NULL && *((int*)(ptr->next->data)) == newObj)
{
node tmp = ptr->next;
ptr->next = ptr->next->next;
free(tmp);
list->size--;
return 1;
}
ptr = ptr->next;
}
return 0;
}
SortedListIteratorPtr SLCreateIterator(SortedListPtr list){
SortedListIteratorPtr slip;
/*Case where list is null*/
if(list == NULL){
printf("List is NULL\n");
return NULL;
}
if((slip = (SortedListIteratorPtr)(malloc(sizeof(struct SortedListIterator)))) == NULL){
printf("Error allocating memory for SortedListIteratorPointer\n");
return NULL;
}
slip->list = list;
slip->currNode = list->head;
return slip;
}
void SLDestroyIterator(SortedListIteratorPtr iter){
free(iter);
}
void *SLNextItem(SortedListIteratorPtr iter){
/*check for memory allocation and management*/
node tempNode;
if(iter == NULL){
return NULL;
}
else if(iter->list == NULL){
printf("iterators list is null\n");
return NULL;
}
else if(iter->currNode == NULL){
/*printf("currNode is null\n");*/
return NULL;
}
else{
tempNode = iter->currNode;
iter->currNode = iter->currNode->next;
return tempNode;
}
}
/*
int main(int argc, char **argv)
{
int i, holder;
SortedListPtr slp;
SortedListIteratorPtr slip;
node tempNode;
int num[] = {5,4,3,2,1};
slp = SLCreate(compareInts);
for(i = 0; i < 5; i++){
if(SLInsert(slp, (void*)&num[i]) == 1){
printf("successful insertion of %d\n", i);
}
}
printf("blah\n");
SLRemove(slp, (void*)&num[2]);
printf("num nodes is %d\n", slp->size);
slip = SLCreateIterator(slp);
while((tempNode = SLNextItem(slip)) != NULL){
holder = *((int*)(tempNode->data));
printf("node is %d\n", holder);
}
return 0;
}
*/