Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 61 additions & 4 deletions arrays/arrays.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +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;
}


Expand All @@ -36,8 +41,13 @@ void destroy_array(Array *arr) {

// Free all elements

// Free array
for (int i = 0; i < arr->count; i++) {
free(arr->elements[i]);
}

// Free array
free(arr->elements);
free(arr);
}

/*****
Expand All @@ -47,13 +57,17 @@ void destroy_array(Array *arr) {
void resize_array(Array *arr) {

// Create a new element storage with double capacity
Array *new_array = Array *create_array((2 * (sizeof(*arr->capacity))));

// Copy elements into the new storage
for (int i = 0; i < arr->count; i++) {
arr_append(*new_array, arr->elements[i]);
}

// Free the old elements array (but NOT the strings they point to)
free(arr);

// Update the elements and capacity to new values

}


Expand All @@ -73,7 +87,12 @@ char *arr_read(Array *arr, int index) {

// Throw an error if the index is greater or equal to than the current count

if (index >= arr->count) {
exit(1);
}

// Otherwise, return the element at the given index
return (&arr + (index * sizeof(char *)));
}


Expand All @@ -85,15 +104,27 @@ 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) {
exit(1);
}

// Resize the array if the number of elements is over capacity
if (arr->capacity <= (arr->count + 1)) {
resize_array(arr);
}

// Move every element after the insert index to the right one position
for (int i = arr->count; i >= index; i--) {
char *moved_element;
moved_element = &(arr->elements[i] + 1);
*moved_element = arr->elements[i];
}

// Copy the element (hint: use `strdup()`) and add it to the array
*arr->elements[index] = strdup(*element);

// Increment count by 1

arr->count++;
}

/*****
Expand All @@ -102,11 +133,23 @@ void arr_insert(Array *arr, char *element, int index) {
void arr_append(Array *arr, char *element) {

// Resize the array if the number of elements is over capacity
if (arr->capacity <= arr->count) {
arr->capacity++;
realloc(arr, (capacity * sizeof(*arr)));
}

// or throw an error if resize isn't implemented yet.
if (arr->capacity <= arr->count) {
exit(1);
}

// Copy the element and add it to the end of the array
char *el;
el = (&arr + (arr->capacity * sizeof(char *)));
strcpy(*el, *element);

// Increment count by 1
arr->count++;

}

Expand All @@ -120,11 +163,25 @@ void arr_remove(Array *arr, char *element) {

// Search for the first occurence of the element and remove it.
// Don't forget to free its memory!
int position;

for (int i = 0; i <= arr->count; i++) {

if (arr->elements[i] == element) {
free(arr->elements[i]);
position = i;
}

if ((position != NULL) && (i > position)) {
&arr->elements[i]--;
}
}

// Shift over every element after the removed element to the left one position

// Decrement count by 1

// Decrement count by 1
arr->count--;
}


Expand Down