diff options
Diffstat (limited to 'linkedlist')
-rw-r--r-- | linkedlist/list.c | 46 | ||||
-rw-r--r-- | linkedlist/list.h | 16 | ||||
-rw-r--r-- | linkedlist/main.c | 165 | ||||
-rw-r--r-- | linkedlist/stack.c | 38 | ||||
-rw-r--r-- | linkedlist/stack.h | 18 | ||||
-rw-r--r-- | linkedlist/test.c | 28 |
6 files changed, 311 insertions, 0 deletions
diff --git a/linkedlist/list.c b/linkedlist/list.c new file mode 100644 index 0000000..701cd5f --- /dev/null +++ b/linkedlist/list.c @@ -0,0 +1,46 @@ +#include "list.h" + +void list_init(struct list** _listroot) +{ + if(*_listroot) + while(list_popFront(_listroot)); + + *_listroot = NULL; +} + +void list_add(int _data, struct list** _listroot) +{ + struct list **current = _listroot; + + while(*current) + current = &(*current)->next; + + *current = malloc(sizeof(**current)); + (*current)->data = _data; +} + +int list_popFront(struct list** _listroot) +{ + if(!*_listroot) + return 0; + + int data = (*_listroot)->data; + struct list *oldRoot = *_listroot; + + *_listroot = (*_listroot)->next; + free(oldRoot); + + return data; +} + +int list_get(int _index, struct list** _listroot) +{ + struct list* current = *_listroot; + + for(int i = 0; i < _index; i++) + { + current = current->next; + } + + return current->data; +} diff --git a/linkedlist/list.h b/linkedlist/list.h new file mode 100644 index 0000000..db9162b --- /dev/null +++ b/linkedlist/list.h @@ -0,0 +1,16 @@ +#include <stdio.h> +#include <stdlib.h> + +struct list +{ + struct list *next; + int data; +}; + +void list_init(struct list** _listroot); + +void list_add(int _data, struct list** _listroot); + +int list_popFront(struct list** _listroot); + +int list_get(int _index, struct list** _listroot); diff --git a/linkedlist/main.c b/linkedlist/main.c new file mode 100644 index 0000000..1d17923 --- /dev/null +++ b/linkedlist/main.c @@ -0,0 +1,165 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "stack.h" +#include "list.h" + +struct node +{ + struct node* nextS; + struct node* nextB; + int data; +}; + +struct node* root; + +struct node** getSmallest(struct node** _root) +{ + struct node** current = _root; + + while((*current)->nextS) + current = &((*current)->nextS); + + return current; +} + +struct node removeNode(int _data) +{ + struct node **current = &root; + struct node *removeNode = NULL; + struct node **newRoot = NULL; + struct node returnNode; + + while((*current)->data != _data) + current = (*current)->data < _data ? &(*current)->nextB : &(*current)->nextS; + + + printf("node to remove:%i\n", (*current)->data); + removeNode = *current; + + if(removeNode->nextB) + { + newRoot = getSmallest(&removeNode->nextB); + *current = *newRoot; + *newRoot = (*newRoot)->nextB; + + (*current)->nextS = removeNode->nextS; + (*current)->nextB = removeNode->nextB; + } + else + *current = (*current)->nextS;; + + returnNode = *removeNode; + free(removeNode); + + return returnNode; +} + +void insert(int _data) +{ + struct node** current = &root; + struct node* new = malloc(sizeof(*new)); + + new->nextS = NULL; + new->nextB = NULL; + new->data=_data; + + while (*current) + current = (*current)->data > _data ? &((*current)->nextS) : &((*current)->nextB); + + *current = new; +} + +void draw(void) +{ + struct node* current = root; + struct node* stackTop = NULL; + struct stack* stackpointer = NULL; + int depth = 0; + + stack_init(&stackpointer); + +start: + + while (current) + { + stack_add( (void*) current , &stackpointer); + + int i; + for(i = 0; i < depth; i++) + printf(" "); + + printf("|:%i\n", current->data); + + current = current->nextS; + + depth++; + } +nextinstack: + + stackTop = (struct node*)stack_get(&stackpointer); + if(!stackTop) + return; + + current = stackTop->nextB; + + if(current) + goto start; + + depth --; + goto nextinstack; +} + +void balance() +{ + struct node *current = root; + struct node *stackTop = NULL; + struct stack *stackpointer = NULL; + struct list *listroot = NULL; + + + stack_init(&stackpointer); + list_init(&listroot); + + //Create Inorder List +start: + while(current) + { + stack_add( (void*) current, &stackpointer); + current = current->nextS; + } + + stackTop = (struct stack*) stack_pop(&stackpointer); + stackTop ? current = stackTop->nextB :; + + if(current) + { + list_add(current->data, &listroot); + goto start; + } + + //TODO Balance + +} + +int main(int argc, char* argv[]) +{ + root = NULL; + + insert(7); + insert(5); + insert(10); + insert(11); + insert(1); + + //draw(); + balance(); + + printf("balanced"); + //removeNode(7); + + //draw(); + + printf("%i is the smallest obejct\n", (*getSmallest(&root))->data); + return 0; +} diff --git a/linkedlist/stack.c b/linkedlist/stack.c new file mode 100644 index 0000000..de77b72 --- /dev/null +++ b/linkedlist/stack.c @@ -0,0 +1,38 @@ +#include "stack.h" + +void stack_add(void* _data, struct stack** _stackpointer) +{ + struct stack* newsp = malloc(sizeof(*newsp)); + + newsp->data = _data; + newsp->next = *_stackpointer; + *_stackpointer = newsp; +} + +void* stack_get(struct stack** _stackpointer) +{ + void* return_data; + struct stack* oldsp; //Old stackpointer + + if(!*_stackpointer) + return NULL; + + return_data = (*_stackpointer)->data; + oldsp = *_stackpointer; + *_stackpointer = (*_stackpointer)->next; + free(oldsp); + + return return_data; +} + +void stack_init(struct stack** _stackpointer) +{ + stack_flush(_stackpointer); + *_stackpointer = NULL; +} + +void stack_flush(struct stack** _stackpointer) +{ + while(*_stackpointer) + stack_get(_stackpointer); +} diff --git a/linkedlist/stack.h b/linkedlist/stack.h new file mode 100644 index 0000000..d579da9 --- /dev/null +++ b/linkedlist/stack.h @@ -0,0 +1,18 @@ +//stack.h +// + +#include <stdlib.h> + +struct stack +{ + struct stack* next; + void* data; +}; + +void stack_add(void* _data, struct stack** _stackpointer); + +void* stack_get(struct stack** _stackpointer); + +void stack_init(struct stack** _stackpointer); + +void stack_flush(struct stack** _stackpointer); diff --git a/linkedlist/test.c b/linkedlist/test.c new file mode 100644 index 0000000..9c138d4 --- /dev/null +++ b/linkedlist/test.c @@ -0,0 +1,28 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "list.h" + +int main(void) +{ + struct list* listroot = malloc(sizeof(*listroot)); + + list_init(&listroot); + + for(int i = 0; i < 30; i++) + { + list_add(i, &listroot); + } + + printf("list created, printing\n"); + + for(int i = 0; i < 30; i++) + { + printf("Round %i, ", i); + printf("Element %i\n", list_get(i, &listroot)); + } + + printf("\n"); + + return 0; +} |