-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayList.c
More file actions
378 lines (277 loc) · 8.55 KB
/
ArrayList.c
File metadata and controls
378 lines (277 loc) · 8.55 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ArrayList.h"
/**
* Function createArrayList
* ------------------------
* Dynamically allocate space for a new ArrayList
* Initialize its internal to be of length length
* or DEFAULT_INIT_LEN, 10, whichever is greater.
* Properly initialize pointers in the array to NULL
* Set the size and capacity members of the list
*
* Returns a pointer to the new ArrayList
* Returns NULL if calls to malloc fails
*/
ArrayList *createArrayList(int length){
int index;
ArrayList *list = NULL;
if(length < DEFAULT_INIT_LEN)
length = DEFAULT_INIT_LEN;
list = (ArrayList *)malloc(sizeof(ArrayList));
if(list == NULL) return NULL;
list->array = (char **)malloc(length * sizeof(char *));
if(list->array == NULL) return NULL;
list->size = 0;
list->capacity = length;
for(index = 0; index < length; index++)
list->array[index] = NULL;
printf("-> Created new ArrayList of size %d.\n", length);
return list;
}
/**
* Function: destroyArrayList
* --------------------------
* Free any dynamically alloacted memory associated with list
* Return NULL
*/
ArrayList *destroyArrayList(ArrayList *list){
int index;
int length;
if(list == NULL) return NULL;
length = getSize(list);
for(index = 0; index < length; index++)
if(list->array[index] != NULL)
free(list->array[index]);
free(list->array);
free(list);
return NULL;
}
/**
* Function: expandArrayList
* -------------------------
* Dynamically allocate a new array of length length
* Copy the contents of list's old array into new array
* Free any memory associated with the old list->array
* Then set list->array to point to newly created array
* Be sure all pointers are properly initialized
* Update the size and capacity of ArrayList if needed
*
* Note: If length is less than or equal to list's
* current array capacity or if the list pointer is NULL,
* you shouldn't modify the ArrayList at all. If so,
* return from the function right away producing no output
*
* Returns a pointer to the ArrayList
* Returns NULL if any calls to malloc() fails
*/
ArrayList *expandArrayList(ArrayList *list, int length){
int index;
char ** array = NULL;
if(list == NULL) return NULL;
if(length <= list->capacity)
return list;
array = (char **)malloc(length * sizeof(char *));
if(array == NULL) return NULL;
for(index = 0; index < list->capacity; index++)
{
if(list->array[index] != NULL)
array[index] = list->array[index];
else
array[index] = NULL;
}
for(index = list->capacity; index < length; index++)
array[index] = NULL;
free(list->array);
list->array = array;
list->capacity = length;
printf("-> Expanded ArrayList to size %d.\n", length);
return list;
}
/**
* Function: trimArrayList
* -----------------------
* If list's capacity is greater than its current size,
* trim the length of the array to the current size
* Malloc a new array of the current size
* Copy the members of the array into new array
* Free the old array to avoid any memory leaks
*
* Returns a pointer to the ArrayList
* Returns NULL if malloc() fails or list ptr is NULL
*/
ArrayList *trimArrayList(ArrayList *list){
int index, size;
char ** array = NULL;
if(list == NULL) return NULL;
size = getSize(list);
if(list->capacity <= size)
return list;
array = (char **) malloc(size * sizeof(char*));
if(array == NULL) return NULL;
for(index = 0; index < size; index++)
array[index] = list->array[index];
free(list->array);
list->array = array;
list->capacity = size;
printf("-> Trimmed ArrayList to size %d.\n", size);
return list;
}
/**
* Function: put
* -------------
* Insert a copy of str into the next unused cell of array
* If list is already full, call expandArrayList() to grow
* the array of the length (capacity * 2 + 1) before inserting
* new element. When copying str into the array, allocate
* the minimum amount of space necessary to store the string
*
* Returns a pointer to the copy of new string inserted into array
* Returns NULL if the string couldn't be added or list is NULL
*/
char *put(ArrayList *list, char *str){
int index, stringLength;
if(list == NULL || str == NULL) return NULL;
if(list->capacity == getSize(list))
{
list = expandArrayList(list, list->capacity * 2 + 1);
if(list == NULL) return NULL;
}
index = getSize(list);
stringLength = strlen(str);
list->array[index] = (char *)malloc(stringLength+1);
if(list->array[index] == NULL) return NULL;
strncpy(list->array[index], str, stringLength);
list->array[index][stringLength] = '\0';
list->size = getSize(list) + 1;
return list->array[index];
}
/**
* Function: get
* -------------
* Attempts to return the element at specified index
* Protect user from index out of bounds error with array
*
* Returns a pointer to string at position index of array
* Returns NULL if index was out of bounds or ptr is NULL
*/
char *get(ArrayList *list, int index){
if(list == NULL) return NULL;
if(getSize(list) == -1) return NULL;
if(index > getSize(list) - 1) return NULL;
if(index < 0) return NULL;
if(list->array[index] == NULL) return NULL;
return list->array[index];
}
/**
* Function: set
* -------------
* If the array already has valid string at position index
* replace it with copy of str. Otherwise, the opeartion
* fails and we simply returns NULL.
* Ensure that no more space is used to store new
* copy of str than is absolutely necessary
*
* Returns a pointer to the copy of string placed in ArrayList
* Returns NULL if operation failed for any reason
*/
char *set(ArrayList *list, int index, char *str){
if(list == NULL) return NULL;
if(getSize(list) == -1) return NULL;
if(index > getSize(list) - 1) return NULL;
if(index < 0) return NULL;
if(list->array[index] == NULL) return NULL;
if(str == NULL) return NULL;
free(list->array[index]);
list->array[index] = (char *)malloc(strlen(str)+1);
if(list->array[index] == NULL) return NULL;
strncpy(list->array[index], str, strlen(str));
list->array[index][strlen(str)] = '\0';
return list->array[index];
}
/**
* Function: insertElement
* -----------------------
* Insert a copy of str at the specified index in array
* Any elements to right of index are shifted one space to right
* If specified index is greater than ArrayList's size,
* the element being inserted should be placed in
* first empty position in the list.
*
* Returns a pointer to the copy of string inserted into list
* Returns NULL if insertion fails for any reason
*/
char *insertElement(ArrayList *list, int index, char *str){
int startPos, endPos, i;
char * tmp;
if(list == NULL || str == NULL) return NULL;
if(list->capacity == getSize(list))
list = expandArrayList(list, list->capacity * 2 + 1);
if(index > getSize(list)){
tmp = put(list, str);
if(tmp == NULL) return NULL;
return tmp;
}
else {
startPos = index;
endPos = getSize(list);
for(i = endPos; i > startPos; i--)
list->array[i] = list->array[i-1];
list->array[index] = (char *)malloc(strlen(str) + 1);
tmp = list->array[index];
if(tmp == NULL) return NULL;
strncpy(tmp, str, strlen(str));
tmp[strlen(str)] = '\0';
list->size = getSize(list) + 1;
return tmp;
}
}
/**
* Function: removeElement
* -----------------------
* Remove the string to the specified index in the array
* Strings to the right of index are shifted one space to left
* ArrayList's size member should be updated accordingly
*
* Returns 1 if an element was successfully removed from array
* Returns 0 if list ptr is NULL or if index greater than size
*/
int removeElement(ArrayList *list, int index){
int startPos, endPos, i;
if(list == NULL) return 0;
if(index > getSize(list) - 1) return 0;
free(list->array[index]);
startPos = index;
endPos = getSize(list) - 1;
for(i = startPos; i < endPos; i++)
list->array[i] = list->array[i+1];
list->size = getSize(list) - 1;
return 1;
}
/**
* Function: getSize
* -----------------
* Returns the number of elements currently in the list
* Returns -1 if list ptr is NULL
*/
int getSize(ArrayList *list){
if(list == NULL)
return -1;
return list->size;
}
/**
* Function: printArrayList
* ------------------------
* Print all strings currently in the array
*
* Prints the string "(empty list)" if list ptr is NULL
* or if the list array has no elements inside them
*/
void printArrayList(ArrayList *list){
int index;
if(list == NULL) printf("(empty list)\n");
if(getSize(list) == 0) printf("(empty list)\n");
for(index = 0; index < getSize(list); index++)
printf("%s\n", get(list, index));
}