-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrList.c
More file actions
398 lines (367 loc) · 6.94 KB
/
StrList.c
File metadata and controls
398 lines (367 loc) · 6.94 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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "StrList.h"
typedef struct _node
{
char *_data;
struct _node * _next;
}Node;
typedef struct _StrList
{
Node *_head;
size_t _size;
}StrList;
Node* Node_alloc(const char *data, Node* next)
{
Node* p = (Node*)malloc(sizeof(Node));
if (p == NULL)
{
return NULL;
}
p->_data = strdup(data);
p->_next = next;
return p;
}
void Node_free(Node* node)
{
if (node == NULL)
{
return;
}
free(node->_data);
free(node);
}
/*
* Allocates a new empty StrList.
* It's the user responsibility to free it with StrList_free.
*/
StrList* StrList_alloc()
{
StrList* p = (StrList*)malloc(sizeof(StrList));
if (p == NULL)
{
return NULL;
}
p->_head= NULL;
p->_size= 0;
return p;
}
/*
* Frees the memory and resources allocated to StrList.
* If StrList==NULL does nothing (same as free).
*/
void StrList_free(StrList* StrList)
{
if (StrList==NULL)
{
return;
}
Node* p1 = StrList->_head;
Node* p2;
while(p1 != NULL)
{
p2 = p1;
p1 = p1->_next;
Node_free(p2);
}
free(StrList);
}
/*
* 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)
{
return;
}
Node* p = Node_alloc(data, NULL);
if (p == NULL)
{
return;
}
if(StrList->_head == NULL)
{
StrList->_head = p;
}
else
{
Node* q = StrList->_head;
while(q->_next != NULL)
{
q = q->_next;
}
q->_next = p;
}
StrList->_size++;
}
/*
* Inserts an element at given index
*/
void StrList_insertAt(StrList* StrList,const char* data,int index)
{
if(index<0 || index>StrList->_size)
{
return;
}
Node* p= Node_alloc(data, NULL);
if (p == NULL)
{
return;
}
if(index==0)
{
p->_next = StrList->_head;
StrList->_head = p;
}
else
{
Node* q = StrList->_head;
for(int i=0; i<index-1; i++)
{
q = q->_next;
}
p->_next = q->_next;
q->_next = p;
}
StrList->_size++;
}
/*
* Returns the StrList first data.
*/
char* StrList_firstData(const StrList* StrList)
{
if(StrList->_head==NULL)
{
return NULL;
}
return StrList->_head->_data;
}
/*
* Prints the StrList to the standard output.
*/
void StrList_print(const StrList* StrList)
{
if (StrList == NULL || StrList->_head == NULL)
{
return;
}
Node* p= StrList->_head;
while(p != NULL)
{
printf("%s ", p->_data);
p= p->_next;
}
}
/*
Prints the word at the given index to the standard output.
*/
void StrList_printAt(const StrList* Strlist,int index)
{
if(index<0 || index>=Strlist->_size)
{
return;
}
Node* p= Strlist->_head;
for(int i=0; i<index; i++)
{
p= p->_next;
}
printf("%s\n", p->_data);
}
/*
* Return the amount of chars in the list.
*/
int StrList_printLen(const StrList* Strlist)
{
int count= 0;
Node* p= Strlist->_head;
while(p != NULL)
{
count+= strlen(p->_data);
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)
{
int count= 0;
Node* p= StrList->_head;
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 = StrList->_head;
Node* q = NULL;
while(p != NULL)
{
if(strcmp(p->_data, data)==0)
{
if(q==NULL)
{
StrList->_head= p->_next;
Node_free(p);
p = StrList->_head;
}
else
{
q->_next= p->_next;
Node_free(p);
p = q->_next;
}
StrList->_size--;
}
else
{
q = p;
p = p->_next;
}
}
}
/*
Given an index and a list, remove the string at that index.
*/
void StrList_removeAt(StrList* StrList, int index)
{
if(index<0 || index>=StrList->_size)
{
return;
}
Node* p= StrList->_head;
if(index==0)
{
StrList->_head = p->_next;
Node_free(p);
}
else
{
Node* q= NULL;
for(int i=0; i<index; i++)
{
q = p;
p = p->_next;
}
q->_next= p->_next;
Node_free(p);
}
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->_size != StrList2->_size)
{
return 0;
}
Node* p= StrList1->_head;
Node* q= StrList2->_head;
while(p != NULL && q != NULL)
{
if(strcmp(p->_data, q->_data)!=0)
{
return 0;
}
p = p->_next;
q = q->_next;
}
return 1;
}
/*
* Clones the given StrList.
* It's the user responsibility to free it with StrList_free.
*/
StrList* StrList_clone(const StrList* Strlist)
{
StrList* p = StrList_alloc();
Node* q = Strlist->_head;
while(q != NULL)
{
StrList_insertLast(p, q->_data);
q = q->_next;
}
return p;
}
/*
* Reverces the given StrList.
*/
void StrList_reverse( StrList* StrList)
{
Node* p= StrList->_head;
Node* q= NULL;
Node* r= NULL;
while(p != NULL)
{
r = q;
q = p;
p = p->_next;
q->_next = r;
}
StrList->_head = q;
}
/*
* Sort the given list in lexicographical order
*/
void StrList_sort( StrList* StrList)
{
Node* p= StrList->_head;
Node* q= NULL;
Node* r= NULL;
while(p != NULL)
{
q = p;
r = p->_next;
while(r != NULL)
{
if(strcmp(r->_data, q->_data) < 0)
{
q = r;
}
r = r->_next;
}
char* temp = p->_data;
p->_data = q->_data;
q->_data = temp;
p = p->_next;
}
}
/*
* Checks if the given list is sorted in lexicographical order
* returns 1 for sorted, 0 otherwise
*/
int StrList_isSorted(StrList* StrList)
{
// Empty list or a list with a single element is considered sorted
if (StrList->_head == NULL || StrList->_head->_next == NULL)
return 1;
Node* p = StrList->_head;
while(p->_next != NULL)
{
if (strcmp(p->_data, p->_next->_data) > 0)
return 0;
p = p->_next;
}
return 1;
}