more work
This commit is contained in:
parent
1986404da5
commit
68ced98e24
|
@ -23,13 +23,9 @@ i32_ArrayList *new_arraylist(int cap) {
|
|||
}
|
||||
|
||||
// add to end of the array
|
||||
void array_append(i32_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 do_append(i32_ArrayList *s, int v) {
|
||||
s->data[s->index] = v;
|
||||
s->index++;
|
||||
}
|
||||
|
||||
// takes the address of a pointer
|
||||
|
@ -47,15 +43,15 @@ i32_ArrayList *resize_arraylist(i32_ArrayList *arr) {
|
|||
return (new_arr);
|
||||
}
|
||||
|
||||
void array_append2(i32_ArrayList **arr_ptr, int v) {
|
||||
void array_append(i32_ArrayList **arr_ptr, int v) {
|
||||
i32_ArrayList *arr = *arr_ptr;
|
||||
if (arr->index == arr->capacity) {
|
||||
i32_ArrayList *new_arr = resize_arraylist(arr);
|
||||
*arr_ptr = new_arr;
|
||||
array_append(*arr_ptr, v);
|
||||
do_append(*arr_ptr, v);
|
||||
|
||||
} else {
|
||||
array_append(arr, v);
|
||||
do_append(arr, v);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -63,7 +59,7 @@ void array_append2(i32_ArrayList **arr_ptr, int v) {
|
|||
i32_ArrayList *new_arraylist_from_array(int cap, int *arr) {
|
||||
i32_ArrayList *out = new_arraylist(cap);
|
||||
for (int i = 0; i < cap; i++) {
|
||||
array_append(out, arr[i]);
|
||||
do_append(out, arr[i]);
|
||||
}
|
||||
|
||||
return (out);
|
||||
|
@ -74,9 +70,10 @@ i32_ArrayList *new_arraylist_from_array(int cap, int *arr) {
|
|||
// gotta be careful and check that the index + 1 <= capacity otherwise we are in trouble
|
||||
void array_insert_at(i32_ArrayList *arr, int at_index, int32_t value) {
|
||||
if (at_index == arr->index) {
|
||||
array_append(arr, value);
|
||||
do_append(arr, value);
|
||||
}
|
||||
|
||||
// TODO: eh this should be much better
|
||||
if (at_index + 1 > arr->capacity) {
|
||||
printf("ERROR: this insert is not possible since the shift required would be over the capacity of the array\n");
|
||||
printf("You requested insert at %d but array capacity is set to %d\n", at_index, arr->capacity);
|
||||
|
@ -119,24 +116,24 @@ int main() {
|
|||
print_array_list(b);
|
||||
|
||||
// these should all work just fine
|
||||
array_append(a, 10);
|
||||
array_append(&a, 10);
|
||||
print_array_list(a);
|
||||
array_append(a, 11);
|
||||
array_append(&a, 11);
|
||||
print_array_list(a);
|
||||
array_append(a, 12);
|
||||
array_append(&a, 12);
|
||||
print_array_list(a);
|
||||
array_append(a, 13);
|
||||
array_append(&a, 13);
|
||||
print_array_list(a);
|
||||
array_append(a, 14);
|
||||
array_append(&a, 14);
|
||||
print_array_list(a);
|
||||
|
||||
// this one will error
|
||||
array_append(a, 100);
|
||||
array_append(&a, 100);
|
||||
|
||||
// so we remove one and then add
|
||||
pop_from_array(a);
|
||||
print_array_list(a);
|
||||
array_append(a, 100);
|
||||
array_append(&a, 100);
|
||||
print_array_list(a);
|
||||
|
||||
// now we test inserting different index
|
||||
|
@ -154,13 +151,19 @@ int main() {
|
|||
// this will shift the current 3 to 4, but this causes the 100 to be removed
|
||||
array_insert_at(a, 3, 123);
|
||||
print_array_list(a);
|
||||
array_append2(&a, 5000);
|
||||
array_append(&a, 5000);
|
||||
print_array_list(a);
|
||||
|
||||
printf("--------------------------------\n");
|
||||
print_array_list(b);
|
||||
array_append2(&b, 100);
|
||||
array_append(&b, 100);
|
||||
print_array_list(b);
|
||||
|
||||
int vals[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
|
||||
i32_ArrayList *c = new_arraylist_from_array(10, vals);
|
||||
|
||||
array_append(&c, 10);
|
||||
print_array_list(c);
|
||||
// array_append(a, 14);
|
||||
// print_array_list(a);
|
||||
// pop_from_array(a);
|
||||
|
|
|
@ -14,34 +14,34 @@ parse it as a tree???
|
|||
|
||||
typedef struct Node {
|
||||
int value;
|
||||
struct Node* prev;
|
||||
struct Node *prev;
|
||||
} Node;
|
||||
|
||||
typedef struct Stack {
|
||||
int len;
|
||||
Node* head;
|
||||
Node *head;
|
||||
} Stack;
|
||||
|
||||
Stack* new_stack() {
|
||||
Stack* s = malloc(sizeof(Stack));
|
||||
Stack *new_stack() {
|
||||
Stack *s = malloc(sizeof(Stack));
|
||||
s->head = NULL;
|
||||
s->len = 0;
|
||||
return (s);
|
||||
}
|
||||
|
||||
Node* new_node(int value) {
|
||||
Node* n = malloc(sizeof(Node));
|
||||
Node *new_node(int value) {
|
||||
Node *n = malloc(sizeof(Node));
|
||||
n->prev = NULL;
|
||||
n->value = value;
|
||||
|
||||
return (n);
|
||||
}
|
||||
|
||||
void json_file_to_tree(char* filepath) {
|
||||
void json_file_to_tree(char *filepath) {
|
||||
// TODO!!!!
|
||||
}
|
||||
|
||||
void push(Stack* stack, Node* node) {
|
||||
void push(Stack *stack, Node *node) {
|
||||
if (stack->len == 0) {
|
||||
stack->head = node;
|
||||
stack->len++;
|
||||
|
@ -52,9 +52,9 @@ void push(Stack* stack, Node* node) {
|
|||
}
|
||||
}
|
||||
|
||||
int pop(Stack* stack) {
|
||||
int pop(Stack *stack) {
|
||||
if (stack->len > 0) {
|
||||
Node* node = stack->head;
|
||||
Node *node = stack->head;
|
||||
int node_val = node->value;
|
||||
stack->head = node->prev;
|
||||
stack->len--;
|
||||
|
@ -67,12 +67,12 @@ int pop(Stack* stack) {
|
|||
|
||||
typedef struct IntBinaryNode {
|
||||
int value;
|
||||
struct IntBinaryNode* left;
|
||||
struct IntBinaryNode* right;
|
||||
struct IntBinaryNode *left;
|
||||
struct IntBinaryNode *right;
|
||||
} IntBinaryNode;
|
||||
|
||||
IntBinaryNode* new_int_binary_node(int value) {
|
||||
IntBinaryNode* n = malloc(sizeof(IntBinaryNode));
|
||||
IntBinaryNode *new_int_binary_node(int value) {
|
||||
IntBinaryNode *n = malloc(sizeof(IntBinaryNode));
|
||||
n->left = NULL;
|
||||
n->right = NULL;
|
||||
n->value = value;
|
||||
|
@ -81,17 +81,17 @@ IntBinaryNode* new_int_binary_node(int value) {
|
|||
}
|
||||
|
||||
typedef struct BinaryTree {
|
||||
IntBinaryNode* root;
|
||||
IntBinaryNode *root;
|
||||
} BinaryTree;
|
||||
|
||||
BinaryTree* new_binary_tree(IntBinaryNode* n) {
|
||||
BinaryTree* b = malloc(sizeof(BinaryTree));
|
||||
BinaryTree *new_binary_tree(IntBinaryNode *n) {
|
||||
BinaryTree *b = malloc(sizeof(BinaryTree));
|
||||
b->root = n;
|
||||
|
||||
return (b);
|
||||
}
|
||||
|
||||
void add_child_node(IntBinaryNode* parent, IntBinaryNode* child, char position) {
|
||||
void add_child_node(IntBinaryNode *parent, IntBinaryNode *child, char position) {
|
||||
if (position != 'l' && position != 'r') {
|
||||
printf("ERROR: position must be either (l)eft or (r)ight\n");
|
||||
exit(1);
|
||||
|
@ -115,7 +115,7 @@ void add_child_node(IntBinaryNode* parent, IntBinaryNode* child, char position)
|
|||
}
|
||||
|
||||
// prints the bottom row of a tree
|
||||
void print_binary_tree(IntBinaryNode* node) {
|
||||
void print_binary_tree(IntBinaryNode *node) {
|
||||
if (node->left == NULL && node->right == NULL) {
|
||||
printf("<%d>\n", node->value);
|
||||
} else {
|
||||
|
@ -124,7 +124,7 @@ void print_binary_tree(IntBinaryNode* node) {
|
|||
}
|
||||
}
|
||||
|
||||
void walk_tree_pre_order(IntBinaryNode* current_node, Stack* stack) {
|
||||
void walk_tree_pre_order(IntBinaryNode *current_node, Stack *stack) {
|
||||
if (current_node->left == NULL && current_node->right == NULL) {
|
||||
push(stack, new_node(current_node->value));
|
||||
} else {
|
||||
|
@ -134,7 +134,7 @@ void walk_tree_pre_order(IntBinaryNode* current_node, Stack* stack) {
|
|||
}
|
||||
}
|
||||
|
||||
void walk_tree_post_order(IntBinaryNode* current_node, Stack* stack) {
|
||||
void walk_tree_post_order(IntBinaryNode *current_node, Stack *stack) {
|
||||
if (current_node->left == NULL && current_node->right == NULL) {
|
||||
push(stack, new_node(current_node->value));
|
||||
} else {
|
||||
|
@ -144,7 +144,7 @@ void walk_tree_post_order(IntBinaryNode* current_node, Stack* stack) {
|
|||
}
|
||||
}
|
||||
|
||||
void walk_tree_in_order(IntBinaryNode* current_node, Stack* stack) {
|
||||
void walk_tree_in_order(IntBinaryNode *current_node, Stack *stack) {
|
||||
if (current_node->left == NULL && current_node->right == NULL) {
|
||||
push(stack, new_node(current_node->value));
|
||||
} else {
|
||||
|
@ -154,7 +154,7 @@ void walk_tree_in_order(IntBinaryNode* current_node, Stack* stack) {
|
|||
}
|
||||
}
|
||||
|
||||
void print_stack(Stack* stack) {
|
||||
void print_stack(Stack *stack) {
|
||||
if (stack->len == 0) {
|
||||
printf("empty stack\n");
|
||||
} else {
|
||||
|
@ -167,13 +167,13 @@ void print_stack(Stack* stack) {
|
|||
}
|
||||
}
|
||||
|
||||
void print_stack_v2(Stack* stack) {
|
||||
void print_stack_v2(Stack *stack) {
|
||||
if (stack->len == 0) {
|
||||
printf("ERROR: empty stack\n");
|
||||
} else {
|
||||
printf("[ ");
|
||||
int counter = 0;
|
||||
Node* curr = stack->head;
|
||||
Node *curr = stack->head;
|
||||
while (counter < stack->len) {
|
||||
printf("%d ", curr->value);
|
||||
curr = curr->prev;
|
||||
|
@ -190,8 +190,8 @@ int main() {
|
|||
10 11 44 77
|
||||
|
||||
*/
|
||||
IntBinaryNode* root_node = new_int_binary_node(12);
|
||||
BinaryTree* tree = new_binary_tree(root_node);
|
||||
IntBinaryNode *root_node = new_int_binary_node(12);
|
||||
BinaryTree *tree = new_binary_tree(root_node);
|
||||
|
||||
add_child_node(root_node, new_int_binary_node(4), 'l');
|
||||
add_child_node(root_node, new_int_binary_node(6), 'r');
|
||||
|
@ -201,9 +201,9 @@ int main() {
|
|||
add_child_node(root_node->right, new_int_binary_node(44), 'l');
|
||||
add_child_node(root_node->right, new_int_binary_node(77), 'r');
|
||||
|
||||
Stack* stack = new_stack();
|
||||
Stack* post_stack = new_stack();
|
||||
Stack* in_order_stack = new_stack();
|
||||
Stack *stack = new_stack();
|
||||
Stack *post_stack = new_stack();
|
||||
Stack *in_order_stack = new_stack();
|
||||
walk_tree_pre_order(root_node, stack);
|
||||
walk_tree_post_order(root_node, post_stack);
|
||||
walk_tree_in_order(root_node, in_order_stack);
|
||||
|
|
|
@ -0,0 +1,107 @@
|
|||
/*
|
||||
an ordered array is just a regular array except all elements must be in order,
|
||||
so there a few things to note:
|
||||
|
||||
1. inserting must respect order
|
||||
2. removing must respect order
|
||||
3. serching can be made faster with binary search
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct FixedOrderedArray {
|
||||
int capacity;
|
||||
int index; // index next to insert at, arr[index] should be empty
|
||||
int data[];
|
||||
} FixedOrderedArray;
|
||||
|
||||
FixedOrderedArray *new_fixed_ordered_array(int capacity) {
|
||||
FixedOrderedArray *arr = malloc(sizeof(FixedOrderedArray) + (sizeof(int) * capacity));
|
||||
arr->capacity = capacity;
|
||||
arr->index = 0;
|
||||
|
||||
return (arr);
|
||||
}
|
||||
|
||||
void fixed_ordered_array_insert(FixedOrderedArray *arr, int val) {
|
||||
if (arr->capacity == arr->index) {
|
||||
fprintf(stderr, "ERROR: array is at capacity, cannot append value\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (arr->index == 0) {
|
||||
arr->data[0] = val;
|
||||
arr->index++;
|
||||
} else if (val >= arr->data[arr->index - 1]) {
|
||||
arr->data[arr->index] = val;
|
||||
arr->index++;
|
||||
} else if (val <= arr->data[0]) { // its smallest to add to start
|
||||
for (int i = arr->index; i > 0; i--) { // shift everything to the right
|
||||
arr->data[i] = arr->data[i - 1];
|
||||
}
|
||||
arr->data[0] = val;
|
||||
arr->index++;
|
||||
} else { // look for where it belongs
|
||||
for (int i = 0; i < arr->index; i++) {
|
||||
if (arr->data[i] > val) { // we must shift everything to the right up to this index
|
||||
for (int j = arr->index; j > i; j--) {
|
||||
arr->data[j] = arr->data[j - 1];
|
||||
}
|
||||
arr->data[i] = val;
|
||||
arr->index++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool search_fixed_ordered_array(FixedOrderedArray *arr, int val) {
|
||||
if (arr->index == 0) {
|
||||
return (false);
|
||||
}
|
||||
|
||||
if (val < arr->data[0]) {
|
||||
return (false);
|
||||
}
|
||||
|
||||
if (val > arr->data[arr->index - 1]) {
|
||||
return (false);
|
||||
}
|
||||
|
||||
for (int i = 0; i < arr->index; i++) {
|
||||
if (val == arr->data[i]) {
|
||||
return (true);
|
||||
} else if (arr->data[i] > val) {
|
||||
return (false);
|
||||
}
|
||||
}
|
||||
return (false);
|
||||
}
|
||||
|
||||
void print_array_list(FixedOrderedArray *arr) {
|
||||
printf("[");
|
||||
for (int i = 0; i < arr->index; i++) {
|
||||
printf(" %d ", arr->data[i]);
|
||||
}
|
||||
printf("]\t<capacity: %d; index: %d>\n", arr->capacity, arr->index);
|
||||
}
|
||||
|
||||
int main() {
|
||||
FixedOrderedArray *arr = new_fixed_ordered_array(5);
|
||||
|
||||
fixed_ordered_array_insert(arr, 10);
|
||||
print_array_list(arr);
|
||||
fixed_ordered_array_insert(arr, 9);
|
||||
print_array_list(arr);
|
||||
fixed_ordered_array_insert(arr, 13);
|
||||
print_array_list(arr);
|
||||
fixed_ordered_array_insert(arr, 11);
|
||||
print_array_list(arr);
|
||||
fixed_ordered_array_insert(arr, 9);
|
||||
print_array_list(arr);
|
||||
|
||||
bool result = search_fixed_ordered_array(arr, 11);
|
||||
printf("the result is %d\n", result);
|
||||
}
|
Loading…
Reference in New Issue