got the double pointer working

This commit is contained in:
Emanuel Rodriguez 2023-07-26 23:10:22 -07:00
parent c1614ed7d7
commit 1986404da5
2 changed files with 53 additions and 37 deletions

View File

@ -33,29 +33,30 @@ void array_append(i32_ArrayList* s, int v) {
} }
// takes the address of a pointer // takes the address of a pointer
void resize_arraylist(i32_ArrayList** arr) { i32_ArrayList *resize_arraylist(i32_ArrayList *arr) {
int new_size = (*arr)->capacity * 2; int new_cap = arr->capacity * 2;
i32_ArrayList* new_arr = realloc((*arr), (sizeof(int) * new_size) + sizeof(i32_ArrayList));
i32_ArrayList *new_arr = (i32_ArrayList *)realloc(arr, (sizeof(int) * new_cap) + sizeof(i32_ArrayList));
if (new_arr == NULL) { if (new_arr == NULL) {
fprintf(stderr, "ERROR: unable to resize array\n"); fprintf(stderr, "ERROR: unable to resize array\n");
exit(1); exit(1);
} }
(*arr) = new_arr; new_arr->capacity = new_cap;
(*arr)->capacity = new_size; return (new_arr);
} }
void array_append2(i32_ArrayList* arr, int v) { void array_append2(i32_ArrayList **arr_ptr, int v) {
i32_ArrayList *arr = *arr_ptr;
if (arr->index == arr->capacity) { if (arr->index == arr->capacity) {
// lets just double the capacity i32_ArrayList *new_arr = resize_arraylist(arr);
resize_arraylist(&arr); *arr_ptr = new_arr;
printf("Resized: size of arr: %d\n", arr->capacity); array_append(*arr_ptr, v);
}
printf("Before append: index = %d\n", arr->index); } else {
array_append(arr, v); array_append(arr, v);
printf("After append: index = %d\n", arr->index); }
} }
// create an array list and fill in with values from array // create an array list and fill in with values from array
@ -103,9 +104,6 @@ int32_t pop_from_array(i32_ArrayList* s) {
} }
} }
void grow_array_list(i32_ArrayList* s, int amount) {
}
void print_array_list(i32_ArrayList *arr) { void print_array_list(i32_ArrayList *arr) {
printf("["); printf("[");
for (int i = 0; i < arr->index; i++) { for (int i = 0; i < arr->index; i++) {
@ -156,14 +154,13 @@ int main() {
// this will shift the current 3 to 4, but this causes the 100 to be removed // this will shift the current 3 to 4, but this causes the 100 to be removed
array_insert_at(a, 3, 123); array_insert_at(a, 3, 123);
print_array_list(a); print_array_list(a);
array_append2(&a, 5000);
// lets implement v2 versions of these function that will grow
// the array when required
printf("calling the version 2 of the append function\n");
array_append2(a, 5656);
printf("the size of the array is %d, and the cap is %d\n", a->index, a->capacity);
print_array_list(a); print_array_list(a);
printf("--------------------------------\n");
print_array_list(b);
array_append2(&b, 100);
print_array_list(b);
// array_append(a, 14); // array_append(a, 14);
// print_array_list(a); // print_array_list(a);
// pop_from_array(a); // pop_from_array(a);

View File

@ -22,6 +22,25 @@ void bubble_sort(int arr[], int len) {
printf("total iterations: %d\n", total_iterations); printf("total iterations: %d\n", total_iterations);
} }
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
void bubble_sort_3(int arr[], int len) {
int swaps = 1;
while (swaps > 0) {
swaps = 0;
for (int i = 0; i < len - 1; i++) {
if (arr[i] > arr[i + 1]) {
swap(&arr[i], &arr[i + 1]);
swaps++;
}
}
}
}
void print_array(int* arr, int len) { void print_array(int* arr, int len) {
printf("[ "); printf("[ ");
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
@ -31,10 +50,10 @@ void print_array(int* arr, int len) {
} }
int main() { int main() {
int arr[5] = {2, 1, 3, 4, 5}; int arr[10] = {2, 1, 3, 4, 5, 7, 33, 7, 8, 20};
printf("before the sort:\n"); printf("before the sort:\n");
print_array(arr, 5); print_array(arr, 10);
bubble_sort(arr, 5); bubble_sort_3(arr, 10);
printf("after the sort:\n"); printf("after the sort:\n");
print_array(arr, 5); print_array(arr, 10);
} }