-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrList.c
484 lines (433 loc) · 12.2 KB
/
StrList.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
#include "StrList.h"
#define INCORRECT_INDEX_ERROR "Error: incorrect index!"
/*
* StrList represents a StrList data structure.
*/
typedef struct _Node {
char* data;
struct _Node* next;
} Node;
typedef struct _StrList {
Node* head;
size_t size;
} StrList;
/*
* Allocates a new empty StrList.
* It's the user responsibility to free it with StrList_free.
* Returns a pointer to the allocated list or NULL if allocation failed.
*/
StrList* StrList_alloc(){
StrList* strList = NULL;
strList = (StrList*)malloc(sizeof(StrList));
if (strList == NULL) {
printf("%s\n", MEMORY_ALLOCATION_ERROR);
}
strList->head = NULL;
strList->size = 0;
return strList; // returns NULL if allocation failed
}
/*
* Frees the memory and resources allocated to StrList.
* If StrList==NULL does nothing (same as free).
* it's the user's responsibility to change strList to NULL afer freeing.
*/
void StrList_free(StrList* strList){
// Frees the list and all its nodes
if (strList != NULL) {
Node *p = strList->head;
Node *temp = p;
while (p != NULL){
temp = p;
p = p->next;
if (temp->data != NULL)
free(temp->data);
free(temp);
}
free(strList);
}
}
Node* Node_alloc(const char* data, Node* next){
// Create a new node
Node *node = NULL;
node = (Node*)malloc(sizeof(Node));
if (node == NULL){
printf("%s\n", MEMORY_ALLOCATION_ERROR);
return NULL;
}
node->data = (char*)malloc(sizeof(char) * (strlen(data) + 1));
if (node->data == NULL) {
printf("%s\n", MEMORY_ALLOCATION_ERROR);
free (node); // allocating memory for node->data failed, so free node as well.
return NULL;
}
strcpy(node->data, data); // deep copy data
return node;
}
void Node_free(Node* node){
if (node != NULL){
if (node->data != NULL){
free (node->data);
}
free (node);
}
}
/*
* Returns the number of elements in the StrList.
*/
size_t StrList_size(const StrList* strList){
return strList->size;
}
/*
* Inserts an element in the end of the StrList.
*/
void StrList_insertLast(StrList* strList, const char* data){
if (strList == NULL) {
strList = StrList_alloc();
}
// Create a new node
Node *temp = NULL;
temp = Node_alloc(data, NULL);
if (temp == NULL){
Node_free(temp);
return; // memory allocation failed
}
/* If strList->head in NULL - this is the first node.
else - find the last node and insert in there */
if (strList->head == NULL){
strList->head = temp;
temp->next = NULL;
}
else {
Node *p = strList->head;
while (p->next != NULL) {
p = p->next;
}
p->next = temp;
temp->next = NULL;
}
strList->size++;
}
/*
* Inserts an element at given index
assuming the index is valid
*/
void StrList_insertAt(StrList* strList, const char* data,int index){
// Check if list in not NULL and the index is avaialble
if (strList == NULL || index > strList->size || index < 0){
printf("%s\n", INCORRECT_INDEX_ERROR);
return;
}
// Create a new node
Node *temp = NULL;
temp = Node_alloc(data, NULL);
if (temp == NULL){
Node_free(temp);
return; // memory allocation failed
}
if (index == 0){ // insert at the beginning - need to change the head of the list
// If another node exists after the head, set it as temp's next, else, set temp's next to NULL
if (strList->head == NULL){
temp->next = NULL;
}
else {
if (strList->head->next != NULL){
temp->next = strList->head;
}
}
strList->head = temp;
}
else {
Node *p = strList->head;
int count = 0;
// Go through list until the node before the desired index is reached
while (count < index-1) {
p = p->next;
count++;
}
// "Push" temp between these 2 nodes - in desired index
temp->next = p->next;
p->next = temp;
}
strList->size++;
}
/*
* Returns the StrList first data.
*/
char* StrList_firstData(const StrList* strList){
if (strList != NULL && strList->head != NULL)
return strList->head->data;
return NULL;
}
/*
* Prints the StrList to the standard output.
*/
void StrList_print(const StrList* strList){
const Node *p = NULL;
if (strList != NULL) {
p = strList->head;
}
while (p != NULL){
if (p->data != NULL) {
printf("%s", p->data);
}
if (p->next != NULL){
printf(" ");
}
p = p->next;
}
printf("\n");
}
/*
Prints the word at the given index to the standard output.
*/
void StrList_printAt(const StrList* strList,int index){
if (index > strList->size || index < 0){
printf("%s\n", INCORRECT_INDEX_ERROR);
return;
}
// Go through list until the node at the desired index is reached
const Node *p = NULL;
if (strList != NULL){
p = strList->head;
}
int count = 0;
while (count != index && p != NULL) {
p = p->next;
count++;
}
if (p != NULL) {
printf("%s\n", p->data);
}
else { // if NULL, empty print
printf("\n");
}
}
/*
* Return the amount of chars in the list without spaces
*/
int StrList_printLen(const StrList* strList) {
int count = 0;
const Node *p = NULL;
if (strList != NULL){
p = strList->head;
}
while (p != NULL){
count += strlen(p->data); // length of curr data without the spaces
p = p->next;
}
return count;
}
/*
Given a string, return the number of times it exists in the list.
*/
int StrList_count(StrList* strList, const char* data){
const Node *p = NULL;
if (strList != NULL){
p = strList->head;
}
int count = 0;
while (p != NULL){
if (strcmp(p->data, data) == 0){
count++;
}
p = p->next;
}
return count;
}
/*
Given a string and a list, remove all the appearences of this string in the list.
*/
void StrList_remove(StrList* strList, const char* data){
Node *p = NULL;
if (strList != NULL){
p = strList->head;
}
// Deal with the head of the list - If the string matches - the list needs to change its head
while (p != NULL && p->data != NULL && strcmp(p->data, data) == 0){
strList->head = p->next;
free(p->data);
free(p);
p = strList->head;
strList->size--;
}
// Head is not the string, go through the rest of the list now
Node *temp = NULL;
if (p != NULL){
temp = p;
p = p->next;
}
while (p != NULL && p->data != NULL){
if (strcmp(p->data, data) == 0){
temp->next = p->next;
Node_free(p);
p = temp->next;
strList->size--;
}
else{
p = p->next;
temp = temp->next;
}
}
}
/*
Given an index and a list, remove the string at that index.
*/
void StrList_removeAt(StrList* strList, int index){
if (strList == NULL || strList->size == 0 || index > strList->size || index < 0){
printf("%s\n", INCORRECT_INDEX_ERROR);
return;
}
Node *p = NULL;
if (strList != NULL){
p = strList->head;
}
// Go through list until the node before the desired index is reached
int count = 0;
while (count < index-1 && p != NULL){
count++;
p = p->next;
}
Node *temp = p->next;
p->next = p->next->next;
if (temp != NULL){
Node_free(temp);
}
strList->size--;
}
/*
* Checks if two StrLists have the same elements
* returns 0 if not and any other number if yes
*/
int StrList_isEqual(const StrList* strList1, const StrList* strList2){
if (strList1 != NULL && strList2 != NULL) {
if (strList1->size != strList2->size){
// They are both not null and their sizes are different
return 0;
}
}
else{
if (strList1 == NULL && strList2 == NULL){
// Both null -> equal
return 1;
}
}
// Run over both lists, with a pointer on each
const Node *p1 = NULL;
const Node *p2 = NULL;
if (strList1 != NULL){
p1 = strList1->head;
}
if (strList2 != NULL){
p2 = strList2->head;
}
while (p1 != NULL && p2 != NULL){
if (p1->data != NULL && p2->data != NULL && strcmp(p1->data, p2->data) != 0){
return 0; // found difference, return false;
}
p1 = p1->next;
p2 = p2->next;
}
return 1;
}
/*
* Clones the given StrList.
* It's the user responsibility to free it with StrList_free.
*/
StrList* StrList_clone(const StrList* strList){
if (strList == NULL){
return NULL;
}
// list to be returned
StrList *clonedList = StrList_alloc();
// Goes over the given list, and for each node, it pushed a "new" one with the same data to the cloned list
const Node *p = NULL;
p = strList->head;
while (p != NULL) {
StrList_insertAt(clonedList, p->data, 0); // Insert at 0, "pushing" everyone to the right
p = p->next;
}
return clonedList;
}
/*
* Reverces the given StrList.
*/
void StrList_reverse(StrList* strList){
// We will use 3 pointers, each iteration we will "change the direction" of the "next" pointer
// If list is NULL or of size <= 1, no action needed
if (strList == NULL || strList->size <= 1){
return;
}
if (strList->size == 2){
// reverse from HEAD -> X -> Y to HEAD -> Y -> X
strList->head->next->next = strList->head->next;
strList->head = strList->head->next;
strList->head->next->next = NULL;
}
// list is of size 3 and more
// set 3 pointers (one after the other) on the start of the list
Node *bef = NULL, *curr = NULL, *next = NULL;
bef = strList->head; // head node
curr = bef->next; // head's next node
next = curr->next; // head's next's next node
bef->next = NULL; // Reverse the first node to point to NULL
while (next != NULL){
// reverse curr node to point to the one behind him
curr->next = bef;
// move all pointers 1 step
bef = curr;
curr = next;
next = next->next;
}
// Last node and move head to the start of the reversed list
curr->next = bef;
strList->head = curr;
}
/*
* Sort the given list in lexicographical order
*/
void StrList_sort(StrList* strList){
if (strList == NULL || strList->size <= 1 || StrList_isSorted(strList) == 1){
return;
}
Node *p, *lastNode = NULL; // pointer for iteration over the list
int swaps = 0;
/*
We are using bubble-sort to send the "biggest" data to the end of the list,
while keeping a pointer on the end of the unsorted "range", (each iteration we have 1 less node to visit).
'swaps' holds the number of swaps while going through the list,
if no swaps were made -> the list is sorted.
*/
do {
swaps = 0;
p = strList->head;
// While the end is not met
while (p->next != lastNode){
if (strcmp(p->data, p->next->data) > 0){
char *temp = p->data;
p->data = p->next->data;
p->next->data = temp;
swaps++;
}
p = p->next;
}
lastNode = p; // p is in place now, save this node so we don't need to visit him again
} while (swaps != 0); // keep going until no swaps are made -> list is sorted
}
/*
* Checks if the given list is sorted in lexicographical order
* returns 1 for sorted, 0 otherwise
*/
int StrList_isSorted(StrList* strList){
if (strList->size <= 1){
return 1;
}
const Node *p = strList->head;
while (p->next != NULL){
// strcmp compares lexicographically. if 2 sequential nodes aren't sorted, return false;
if (strcmp(p->data, p->next->data) > 0){
return 0;
}
p = p->next;
}
return 1;
}