-
Notifications
You must be signed in to change notification settings - Fork 0
/
strList.c
289 lines (256 loc) · 5.87 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
#include "StrList.h"
#include <string.h>
typedef struct _node {
char *_data;
struct _node *_next;
} Node;
struct _StrList {
Node* _head;
size_t _size;
};
Node* Node_alloc(char* data,
Node* next) {
Node* p= (Node*)malloc(sizeof(Node));
p->_data= data;
p->_next= next;
return p;
}
void Node_free(Node* node) {
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));
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){
Node* p = Node_alloc(data, NULL);
if(StrList->_head == NULL) {
StrList->_head = p;
++(StrList->_size);
free(p);
}
else{
Node* current = StrList->_head;
while(current->_next != NULL){
current=current->_next;
}
current->_next=p;
}
++(StrList->_size);
free(p);
}
/*
* Inserts an element at given index
*/
void StrList_insertAt(StrList* StrList, const char* data,int index) {
Node* p = Node_alloc(data, NULL);
if(index == 0){
p->_next = StrList->_head;
StrList->_head = p;
++(StrList->_size);
free(p);
}
else{
int i=0;
Node* current = StrList->_head;
while(i < index -1 && current->_next != NULL){
current=current->_next;
i++;
}
if(current != NULL){
Node* temp = current->_next;
p->_next = temp;
current->_next = p;
++(StrList->_size);
}
else {free(p);}
}
}
/*
* Returns the StrList first data.
*/
char* StrList_firstData(const StrList* StrList){
return StrList->_head->_data;
}
/*
* Prints the StrList to the standard output.
*/
void StrList_print(const StrList* StrList){
Node* p = StrList->_head;
while(p != NULL){
printf("(%.2f)->" , p->_data);
p=p->_next;
}
printf("|| size:%zu\n", StrList->_size);
free(p);
}
/*
Prints the word at the given index to the standard output.
*/
void StrList_printAt(const StrList* Strlist,int index){
if(index==0){StrList_print(Strlist);}
else{
int counter = 0;
Node* p = Strlist->_head;
while(p != NULL && counter < index - 1){
p=p->_next;
counter++;
}
if(p != NULL){
StrList_print(p);
}
else{
free(p);
}
}
}
/*
* Return the amount of chars in the list.
*/
int StrList_printLen(const StrList* Strlist){
// check this again
Node* p = Strlist->_head;
int ans = 0;
while(p != NULL){
ans+=strlen(p->_data);
p=p->_next;
}
free(p);
return ans;
}
/*
Given a string, return the number of times it exists in the list.
*/
int StrList_count(StrList* StrList, const char* data){
Node* p = StrList->_head;
int ans = 0;
while(p!=NULL){
if(strcmp(p->_data,data) == 0){
ans++;
}
p=p->_next;
}
free(p);
return ans;
}
/*
Given a string and a list, remove all the appearences of this string in the list.
*/
void StrList_remove(StrList* StrList, const char* data){
//check this again
Node* current = StrList->_head;
Node* prev = NULL;
if(strcmp(current->_data,data) == 0){
StrList->_head = current->_next;
free(current);
}
else{
while(current != NULL){
if(strcmp(current->_data,data) != 0){
prev->_next = current->_next;
}
else{
prev->_next=current;
current=current->_next;
}
}
free(current);
free(prev);
}
}
/*
Given an index and a list, remove the string at that index.
*/
void StrList_removeAt(StrList* StrList, int index){
if(index==0){StrList_remove(StrList, StrList->_head->_data);}
//check this again
Node* p = StrList->_head;
int i=0;
while(p != NULL && i < index -1){
p=p->_next;
i++;
}
if(i == index){
StrList_remove(p,p->_data);
}
free(p);
}
/*
* 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){
const equal =0, not_equal = 1;
const Node* p1 = StrList1->_head;
const Node* p2 = StrList2->_head;
while(p1 != NULL){
if(p2 == NULL || strcmp(p1->_data,p2->_data) == not_equal){
return not_equal;
}
p1=p1->_next;
p2=p2->_next;
}
if(p2 != NULL){
free(p1);
free(p2);
return not_equal;
}
free(p1);
free(p2);
return equal;
}
/*
* Clones the given StrList.
* It's the user responsibility to free it with StrList_free.
*/
StrList* StrList_clone(const StrList* StrList){
//TODO: check this again
//StrList* clone = StrList_alloc();
const Node* p = StrList->_head;
//Node** copy = &(clone->_head);
//clone->_size=StrList->_size;
while(p != NULL){
// *copy = Node_alloc(p->_data,NULL);
p = p->_next;
// copy = &((*copy)->_next);
}
//return clone;
}
/*
* Reverces the given StrList.
*/
void StrList_reverse( StrList* StrList){
Node* prev = NULL;
Node* current = StrList->head;
Node* next = NULL;
}