-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRLEList.c
More file actions
310 lines (295 loc) · 7.7 KB
/
RLEList.c
File metadata and controls
310 lines (295 loc) · 7.7 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
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
#include "RLEList.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#define ZERO_ASSCI '0'
typedef struct RLEList_t{
char letter;
int occur;
RLEList next;
} RLEList_t;
RLEList RLEListCreate()
{
RLEList node = malloc(sizeof(RLEList_t));
if (node == NULL)
{
return NULL;
}
node->letter = 0;
node->occur = 0;
node->next = NULL;
return node;
}
void RLEListDestroy(RLEList list)
{
while (list) //list != NULL
{
RLEList toDelete = list;
list = list->next;
free(toDelete);
}
return;
}
RLEListResult RLEListAppend(RLEList list, char value)
{
if ((list == NULL) || (value == 0))
{
return RLE_LIST_NULL_ARGUMENT;
}
// we check to see if the the element we're adding is the same as the last one
while (list->next) //list->next != NULL
{
list = list->next;
}
// if its the same we increment the occur by one
if ((list->letter == value) || ((list->letter == 0) && (list->occur == 0)))
{
list->letter = value;
list->occur += 1;
}
// if not we create a new node (after the dummy node if the list is "empty (empty here means it only has the dummy node")
else
{
RLEList newNode = malloc(sizeof(RLEList_t));
if (newNode == NULL)
{
return RLE_LIST_OUT_OF_MEMORY;
}
// we are sure to have a new node now
list->next = newNode;
newNode->letter = value;
newNode->occur = 1;
newNode->next = NULL;
}
return RLE_LIST_SUCCESS;
}
int RLEListSize(RLEList list)
{
if (list == NULL)
{
return -1;
}
int counter = 0;
while (list) // list != NULL
{
counter += list->occur;
list = list->next;
}
return counter;
}
RLEListResult RLEListRemove(RLEList list, int index)
{
if(list == NULL)
{
return RLE_LIST_NULL_ARGUMENT;
}
int listLength = RLEListSize(list);
if ((index < 0) || (index >= listLength))
{
return RLE_LIST_INDEX_OUT_OF_BOUNDS;
}
// after these checks we are guarnteed to have a non-empty list
/* prevnode will point to the previous node in the given list
in case that <list> is pointing the first node, the prevNode will point to the first node as well
otherwise it will point to the previous node that the list points at
*/
RLEList currNode = list;
RLEList prevNode = list;
int counter = 0;
// after the while loop, currNode will be pointing to the node we want to remove a letter from
while (currNode->next) { // do this while we arent at the last node
counter += currNode->occur;
if (index < counter)
{
break;
}
if (currNode != prevNode)
{
prevNode = prevNode->next;
}
currNode = currNode->next;
}
assert(counter < listLength);
assert(listLength > 0);
assert(currNode != NULL);
assert(currNode->occur != 0);
assert(currNode->letter != 0);
// after the while loop we have currNode pointing at node we want to remove from
// there are 2 basic casses
// case 1 when we remove on letter when there is more than 1
if (currNode->occur > 1)
{
currNode->occur -= 1;
}
else // currNode->occur == 1
{
assert(currNode->occur == 1);
if (list->next == NULL)
{
list->occur = 0;
list->letter = 0;
}
else // list->next != NULL , this means we at least have 2 nodes
{
if (prevNode == currNode) // should "delete" the "first" node
{
// move the contents of the second node to the first one and then delete the second node
RLEList toDelete = list->next;
list->letter = list->next->letter;
list->occur = list->next->occur;
list->next = list->next->next;
free(toDelete);
}
else
{
assert(prevNode->next == currNode);
RLEList toDelete = currNode;
currNode = currNode->next;
prevNode->next = currNode;
free(toDelete);
if ((currNode != NULL) && (prevNode->letter == currNode->letter))
{
prevNode->occur += currNode->occur;
RLEList toDelete = currNode;
currNode = currNode->next;
prevNode->next = currNode;
free(toDelete);
}
}
}
}
//printf("\nits 7\n");
// we have everything removed now
return RLE_LIST_SUCCESS;
}
char RLEListGet(RLEList list, int index, RLEListResult *result)
{
if (list == NULL)
{
if (result)
{
*result = RLE_LIST_NULL_ARGUMENT;
}
return 0;
}
// check if in bounds
int listLength = RLEListSize(list);
if ((index < 0) || (index >= listLength))
{
if (result)
{
*result = RLE_LIST_INDEX_OUT_OF_BOUNDS;
}
return 0;
}
int counter = 0;
while (list->next)
{
counter += list->occur;
if (index < counter)
{
break;
}
list = list->next;
}
if (result)
{
*result = RLE_LIST_SUCCESS;
}
return list->letter;
}
char* RLEListExportToString(RLEList list, RLEListResult* result)
{
if (list == NULL)
{
if (result)
{
*result = RLE_LIST_NULL_ARGUMENT;
}
return 0;
}
RLEList temp = list;
int stringLength = 0;
while (temp && !((temp->letter == 0) && (temp->occur == 0)))
{
stringLength += 1 + countDigits(temp->occur) + 1;
temp = temp->next;
}
char *string = malloc(stringLength + 1);
if (string == NULL)
{
if (result)
{
*result = RLE_LIST_OUT_OF_MEMORY;
}
return NULL;
}
int index = 0;
while (list && !((list->letter == 0) && (list->occur == 0)))
{
string[index] = list->letter;
int digits = countDigits(list->occur);
int tempNum = list->occur;
for (int i = digits; i > 0; i--)
{
string[index + i] = ZERO_ASSCI + (tempNum % 10);
tempNum /= 10;
}
index += digits + 1;
string[index++] = '\n';
list = list->next;
}
string[index] = '\0';
if (result)
{
*result = RLE_LIST_SUCCESS;
}
return string;
}
RLEListResult RLEListMap(RLEList list, MapFunction map_function)
{
if ((list == NULL) || (map_function == NULL))
{
return RLE_LIST_NULL_ARGUMENT;
}
assert(list);
RLEList temp = list;
while (temp && !((temp->letter == 0) && (temp->occur == 0)))
{
temp->letter = map_function(temp->letter);
temp = temp->next;
}
RLEList nextNode = list->next;
while (nextNode) { // at least 2 nodes
if(list->letter == nextNode->letter)
{
list->occur += nextNode->occur;
RLEList toDelete = nextNode;
nextNode = nextNode->next;
list->next = nextNode;
free(toDelete);
} // after the if we have deleted the second of the 2 nodes
else // if the if didnt happen this means we can continue with nodes
{
nextNode = nextNode->next;
list = list->next;
}
}
return RLE_LIST_SUCCESS;
}
//------------------------ my functions ------------------------//
int countDigits(int number)
{
if (number < 0)
{
return -1;
}
int counter = 0;
while (number)
{
counter++;
number /= 10;
}
return counter;
}