diff --git a/arrays/arrays.c b/arrays/arrays.c index 48c23e7..40c3672 100644 --- a/arrays/arrays.c +++ b/arrays/arrays.c @@ -21,10 +21,16 @@ 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 +41,9 @@ Array *create_array (int capacity) { void destroy_array(Array *arr) { // Free all elements - + free(arr->elements); // Free array + free(arr); } @@ -47,13 +54,18 @@ void destroy_array(Array *arr) { void resize_array(Array *arr) { // Create a new element storage with double capacity - + int new_capacity = 2 * arr->capacity; + char **new_elements = malloc(new_capacity * sizeof(char*)); // Copy elements into the new storage - + + for (int i = 0; i < arr->count; i++) { + new_elements[i] = arr->elements[i]; + } // Free the old elements array (but NOT the strings they point to) - + free(arr->elements); // Update the elements and capacity to new values - + arr->elements = new_elements; + arr->capacity = new_capacity; } @@ -72,8 +84,13 @@ 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, "index is greater than count of array"); + return NULL; + } else { // Otherwise, return the element at the given index + return arr->elements[index]; + } } @@ -83,15 +100,24 @@ 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, "index is greater than count of array"); + } else { // Resize the array if the number of elements is over capacity - + if (arr->capacity <= arr->count) + { + resize_array(arr); + } // Move every element after the insert index to the right one position - // Copy the element and add it to the array - - // Increment count by 1 - + for (int i = arr->count - 1; i >= index; i--) + { + arr->elements[i + 1] = arr->elements[i]; + } + arr->elements[index] = element; + + arr->count++; + } } /***** @@ -101,11 +127,15 @@ 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->capacity <= arr->count) { + 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++; } /***** @@ -117,12 +147,25 @@ 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. + int flag = -1; // Don't forget to free its memory! + for (int i = 0; icount; i++){ + if (element == arr->elements[i]) + { + flag = i; + + } + if (flag != -1) + { + // Shift over every element after the removed element to the left one position + arr->elements[i] = arr->elements[i+1]; + } + } - // Shift over every element after the removed element to the left one position - + // printf("%d", flag); + free(arr->elements[arr->count]); // Decrement count by 1 - + arr->count--; } diff --git a/arrays/tempCodeRunnerFile.c b/arrays/tempCodeRunnerFile.c new file mode 100644 index 0000000..a11c255 --- /dev/null +++ b/arrays/tempCodeRunnerFile.c @@ -0,0 +1,4 @@ +(int i = arr->count - 1; i >= index; i--) + // { + // arr->elements[i + 1] = arr->elements[i]; + // } \ No newline at end of file