diff --git a/c/a.out b/c/a.out index 301e664..1113980 100755 Binary files a/c/a.out and b/c/a.out differ diff --git a/c/arraylist.c b/c/arraylist.c new file mode 100644 index 0000000..a11dd56 --- /dev/null +++ b/c/arraylist.c @@ -0,0 +1,61 @@ +#include +#include + +typedef struct ArrayList { + int capacity; + int index; + int data[]; +} ArrayList; + +ArrayList* new_arraylist(int cap) { + ArrayList* arr = malloc(sizeof(ArrayList) + cap * sizeof(int)); + arr->capacity = cap; + arr->index = 0; + for (int i = 0; i < cap; i++) { + arr->data[i] = 0; + } + + return arr; +} + +void push_to_array(ArrayList* s, int v) { + if (s->index == s->capacity) { + printf("you attempted to insert %d, but array is at capacity cannot add mode values\n", v); + } else { + s->data[s->index] = v; + s->index++; + } +} + +void pop_from_array(ArrayList* s) { + if (s->index == 0) { + printf("there is nothing to remove!\n"); + } else { + s->index--; + } +} + +void grow_array_list(ArrayList* s, int amount) { +} + +void print_array_list(ArrayList* arr) { + printf("["); + for (int i = 0; i < arr->index; i++) { + printf(" %d ", arr->data[i]); + } + printf("]\n"); +} + +int main() { + ArrayList* a = new_arraylist(5); + push_to_array(a, 10); + push_to_array(a, 11); + push_to_array(a, 12); + push_to_array(a, 12); + push_to_array(a, 12); + push_to_array(a, 12); + push_to_array(a, 12); + pop_from_array(a); + push_to_array(a, 155); + print_array_list(a); +} \ No newline at end of file diff --git a/c/mazer-solver.c b/c/mazer-solver.c new file mode 100644 index 0000000..e69de29 diff --git a/c/qs.c b/c/qs.c new file mode 100644 index 0000000..3ed2c64 --- /dev/null +++ b/c/qs.c @@ -0,0 +1,49 @@ +#include + +// partition takes an array and a low and hi values and partitions the array +// by rearrating and placing everything smaller then the pivot point to the left +// of it and everything greater than it on the right +int partition(int arr[], int lo, int hi) { + int pivot_value = arr[hi]; + int idx = -1; // this will keep track of current location of values less then the pivot + + for (int i = 0; i < hi; i++) { + if (arr[i] <= pivot_value) { + idx++; + + // if less than the pivot then swap the ith value with the current idx + int tmp = arr[i]; + arr[i] = arr[idx]; + arr[idx] = tmp; + } + } + + // after the iteration we still need to reposition our pivot_value to the last + // position that idx was after the for loop ends, this will mean that everything + // to left of idx will be less than the pivot point + idx++; + arr[hi] = arr[idx]; + arr[idx] = pivot_value; +} + +void quick_sort(int arr[], int lo, int hi) { + if (lo >= hi) { + printf("error!"); + } + + int current_pivot = partition(arr, lo, hi); + quick_sort(arr, lo, current_pivot - 1); + quick_sort(arr, current_pivot + 1, hi); +} + +int main() { + int array_len = 5; + int arr[5] = {5, 4, 3, 2, 1}; + quick_sort(arr, 0, array_len); + + printf("[ "); + for (int i = 0; i < array_len; i++) { + printf("%d ", arr[i]); + } + printf("]\n"); +} \ No newline at end of file diff --git a/c/stack.c b/c/stack.c index 2ec2106..35619e3 100644 --- a/c/stack.c +++ b/c/stack.c @@ -39,11 +39,12 @@ Stack* new_stack() { return (s); } -bool pop(Stack* s) { +int pop(Stack* s) { if (s->length == 0) { return false; } else { Node* n = s->head; + int val = n->value; if (s->length == 1) { s->head = NULL; } else { @@ -51,7 +52,7 @@ bool pop(Stack* s) { } free(n); s->length--; - return true; + return val; } } @@ -103,32 +104,23 @@ int main() { printf("the value at the top of the stack is: %d\n", peek(s)); printf("the length of the stack is: %d\n", s->length); - // bool res = pop(s); - // printf("the value at the top of the stack is: %d\n", peek(s)); - // printf("the length of the stack is: %d\n", s->length); + printf("the value at the top of the stack is: %d\n", pop(s)); + printf("the length of the stack is: %d\n", s->length); - // res = pop(s); - // printf("the value at the top of the stack is: %d\n", peek(s)); - // printf("the length of the stack is: %d\n", s->length); + printf("the value at the top of the stack is: %d\n", pop(s)); + printf("the length of the stack is: %d\n", s->length); - // res = pop(s); - // printf("the value at the top of the stack is: %d\n", peek(s)); - // printf("the length of the stack is: %d\n", s->length); + printf("the value at the top of the stack is: %d\n", pop(s)); + printf("the length of the stack is: %d\n", s->length); - // res = pop(s); - // printf("the value at the top of the stack is: %d\n", peek(s)); - // printf("the length of the stack is: %d\n", s->length); - // res = pop(s); - // printf("the value at the top of the stack is: %d\n", peek(s)); - // printf("the length of the stack is: %d\n", s->length); - // res = pop(s); - // printf("the value at the top of the stack is: %d\n", peek(s)); - // printf("the length of the stack is: %d\n", s->length); - // res = pop(s); - // printf("the value at the top of the stack is: %d\n", peek(s)); - // printf("the length of the stack is: %d\n", s->length); - - delete_stack(s); + printf("the value at the top of the stack is: %d\n", pop(s)); + printf("the length of the stack is: %d\n", s->length); + printf("the value at the top of the stack is: %d\n", pop(s)); + printf("the length of the stack is: %d\n", s->length); + printf("the value at the top of the stack is: %d\n", pop(s)); + printf("the length of the stack is: %d\n", s->length); + printf("the value at the top of the stack is: %d\n", pop(s)); + printf("the length of the stack is: %d\n", s->length); return (0); } \ No newline at end of file