diff --git a/arrays/arrays.c b/arrays/arrays.c index 48c23e7..e5dd728 100644 --- a/arrays/arrays.c +++ b/arrays/arrays.c @@ -21,11 +21,13 @@ typedef struct Array { *****/ Array *create_array (int capacity) { // Allocate memory for the Array struct - + Array *newArray = malloc(sizeof(Array)); // Set initial values for capacity and count - + newArray->capacity = capacity; + newArray->count = 0; // Allocate memory for elements - + newArray->elements = calloc(capacity, sizeof(char *)); + return newArray; } @@ -35,9 +37,14 @@ Array *create_array (int capacity) { void destroy_array(Array *arr) { // Free all elements - + for (int i = 0; i < arr->count; i++) + { + arr->elements[i] = NULL; + free(arr->elements[i]); + } // Free array - + free(arr->elements); + free(arr); } /***** @@ -47,13 +54,26 @@ void destroy_array(Array *arr) { void resize_array(Array *arr) { // Create a new element storage with double capacity + char **newElements = calloc((2 * arr->capacity), sizeof(char *)); // Copy elements into the new storage + for (int i = 0; i < arr->count; i++) + { + newElements[i] = arr->elements[i]; + } // Free the old elements array (but NOT the strings they point to) - // Update the elements and capacity to new values + for (int i = 0; i < arr->count; i++) + { + arr->elements[i] = NULL; + free(arr->elements[i]); + } + free(arr->elements); + // Update the elements and capacity to new values + arr->elements = newElements; + arr->capacity = arr->capacity * 2; } @@ -69,43 +89,72 @@ void resize_array(Array *arr) { * * Throw an error if the index is out of range. *****/ -char *arr_read(Array *arr, int index) { +char *arr_read(Array *arr, int index) +{ + if (arr->count > index) + { + printf("Error! that index doesn't exist."); + } + else + { + return arr->elements[index]; + } // Throw an error if the index is greater than the current count // Otherwise, return the element at the given index } - /***** * Insert an element to the array at the given index *****/ -void arr_insert(Array *arr, char *element, int index) { +void arr_insert(Array *arr, char *element, int index) +{ // Throw an error if the index is greater than the current count + if (index > arr->count) + { + fprintf(stderr, "Index is beyond array bounds."); + exit(1); + } // Resize the array if the number of elements is over capacity + if (arr->count + 1 > arr->capacity) + { + resize_array(arr); + } // Move every element after the insert index to the right one position + for (int i = index; i < arr->count; i++) + { + arr->elements[i + 1] = arr->elements[i]; + } // Copy the element and add it to the array + arr->elements[index] = element; // Increment count by 1 - + arr->count++; } /***** * Append an element to the end of the array *****/ -void arr_append(Array *arr, char *element) { +void arr_append(Array *arr, char *element) +{ // Resize the array if the number of elements is over capacity // or throw an error if resize isn't implemented yet. + if (arr->count + 1 > arr->capacity) + { + resize_array(arr); + } // Copy the element and add it to the end of the array + arr->elements[arr->count] = element; // Increment count by 1 - + arr->count++; } /***** @@ -114,33 +163,55 @@ void arr_append(Array *arr, char *element) { * * Throw an error if the value is not found. *****/ -void arr_remove(Array *arr, char *element) { +void arr_remove(Array *arr, char *element) +{ + int index = 0; // Search for the first occurence of the element and remove it. - // Don't forget to free its memory! - - // Shift over every element after the removed element to the left one position - - // Decrement count by 1 - + for (int i = 0; i < arr->count; i++) + { + if (arr->elements[i] == element) + { + index = i; + + // Don't forget to free its memory! + arr->elements[i] = NULL; + free(arr->elements[i]); + + // Shift over every element after the removed element to the left one position + for (int i = index; i < arr->count; i++) + { + arr->elements[i] = arr->elements[i + 1]; + } + + // Decrement count by 1 + arr->count--; + } + else if (i == arr->count) + { + fprintf(stderr, "Element not found."); + exit(1); + } + } } - /***** * Utility function to print an array. *****/ -void arr_print(Array *arr) { +void arr_print(Array *arr) +{ printf("["); - for (int i = 0 ; i < arr->count ; i++) { + for (int i = 0; i < arr->count; i++) + { printf("%s", arr->elements[i]); - if (i != arr->count - 1) { + if (i != arr->count - 1) + { printf(","); } } printf("]\n"); } - #ifndef TESTING int main(void) {