diff --git a/arrays/arrays.c b/arrays/arrays.c index 48c23e7..11df1e4 100644 --- a/arrays/arrays.c +++ b/arrays/arrays.c @@ -21,11 +21,14 @@ typedef struct Array { *****/ Array *create_array (int capacity) { // Allocate memory for the Array struct - + Array *arr = malloc(sizeof(Array)); // Set initial values for capacity and count - + arr->capacity = capacity; + arr->count = 0; // Allocate memory for elements + arr->elements = malloc(capacity * sizeof(char *)); + return arr; } @@ -35,8 +38,9 @@ Array *create_array (int capacity) { void destroy_array(Array *arr) { // Free all elements - + free(arr->elements); // Free array + free(arr); } @@ -47,11 +51,10 @@ void destroy_array(Array *arr) { void resize_array(Array *arr) { // Create a new element storage with double capacity - + arr->capacity *= 2; + arr->elements = realloc(arr->elements, arr->capacity * sizeof(char *)); // Copy elements into the new storage - // Free the old elements array (but NOT the strings they point to) - // Update the elements and capacity to new values } @@ -72,8 +75,12 @@ void resize_array(Array *arr) { char *arr_read(Array *arr, int index) { // Throw an error if the index is greater than the current count - + if (index > arr->count){ + fprintf(stderr, "IndexError: Index %d out of range\n", index); + return NULL; + } // Otherwise, return the element at the given index + return arr->elements[index]; } @@ -83,28 +90,42 @@ char *arr_read(Array *arr, 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, "IndexError: Index %d out of range\n", index); + return; + } // Resize the array if the number of elements is over capacity - + if (arr->count >= arr->capacity) { + resize_array(arr); + } // Move every element after the insert index to the right one position - + for (int i = arr->count; i > index; i--) { + arr->elements[i] = arr->elements[i-1]; + } // 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) { - + // Resize the array if the number of elements is over capacity + if (arr->capacity == arr->count) { + resize_array(arr); + } // or throw an error if resize isn't implemented yet. // Copy the element and add it to the end of the array - + char *new_str = strdup(element); + // arr->elements[arr->count] = new_str; + // char *new_element = element; + arr->elements[arr->count] = new_str; // Increment count by 1 + arr->count++; } @@ -117,11 +138,19 @@ void arr_append(Array *arr, char *element) { void arr_remove(Array *arr, char *element) { // Search for the first occurence of the element and remove it. + for (int i=0; i < arr->count; i++) { + if (arr->elements[i] == element) { + arr->elements[i] = NULL; + free(arr->elements[i]); + for (int j = i; j < arr->count; j++) { + arr->elements[j] = arr->elements[j + 1]; + } + } + } // Don't forget to free its memory! - // Shift over every element after the removed element to the left one position - // Decrement count by 1 + arr->count--; } @@ -149,6 +178,8 @@ int main(void) arr_insert(arr, "STRING1", 0); arr_append(arr, "STRING4"); + printf("TESTING READ: %s\n", arr_read(arr, 0)); + printf("TESTING READ: %s\n", arr_read(arr, 1)); arr_insert(arr, "STRING2", 0); arr_insert(arr, "STRING3", 1); arr_print(arr); diff --git a/arrays/arrays.exe b/arrays/arrays.exe new file mode 100644 index 0000000..14a7af6 Binary files /dev/null and b/arrays/arrays.exe differ