Skip to content
Open
Show file tree
Hide file tree
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
81 changes: 62 additions & 19 deletions arrays/arrays.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}

Expand All @@ -35,8 +41,9 @@ Array *create_array (int capacity) {
void destroy_array(Array *arr) {

// Free all elements

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

}

Expand All @@ -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;
}


Expand All @@ -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];
}
}


Expand All @@ -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++;
}
}

/*****
Expand All @@ -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++;
}

/*****
Expand All @@ -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; i<arr->count; 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--;
}


Expand Down
4 changes: 4 additions & 0 deletions arrays/tempCodeRunnerFile.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(int i = arr->count - 1; i >= index; i--)
// {
// arr->elements[i + 1] = arr->elements[i];
// }