diff options
Diffstat (limited to 'sem3/algo/mm2')
-rw-r--r-- | sem3/algo/mm2/linked/llist.c | 29 | ||||
-rw-r--r-- | sem3/algo/mm2/linked/main.c | 10 | ||||
-rw-r--r-- | sem3/algo/mm2/linked/node.c | 16 | ||||
-rw-r--r-- | sem3/algo/mm2/queue.c | 31 |
4 files changed, 50 insertions, 36 deletions
diff --git a/sem3/algo/mm2/linked/llist.c b/sem3/algo/mm2/linked/llist.c index 41ab892..8044e0b 100644 --- a/sem3/algo/mm2/linked/llist.c +++ b/sem3/algo/mm2/linked/llist.c @@ -6,7 +6,8 @@ #define OUT_OFF_BOUNDS 2 -void llist_init(llist_t *list) { +void llist_init(llist_t *list) +{ /* Zero out structure */ list->head = list->root = NULL; @@ -14,12 +15,13 @@ void llist_init(llist_t *list) { } -void llist_append(llist_t *list, int val) { +void llist_append(llist_t *list, int val) +{ /* Insert node after HEAD */ list->head = node_insert(list->head, val); /* Check if was list is empty */ - if( list->len == 0 ) { + if ( list->len == 0 ) { /* Set root */ list->root = list->head; } @@ -28,9 +30,10 @@ void llist_append(llist_t *list, int val) { list->len++; } -node_t *llist_get_node(llist_t *list, unsigned int index) { +node_t *llist_get_node(llist_t *list, unsigned int index) +{ /* Check if we have it */ - if( index >= list->len ) { + if ( index >= list->len ) { return NULL; } @@ -42,7 +45,7 @@ node_t *llist_get_node(llist_t *list, unsigned int index) { node_t *node = direc > 0 ? list->root : list->head; /* Go to index */ - while( pos != index ) { + while ( pos != index ) { /* TODO kinda risky, we trust our math and len */ node = direc > 0 ? node->next : node->prev; @@ -52,10 +55,11 @@ node_t *llist_get_node(llist_t *list, unsigned int index) { return node; } -int llist_get(llist_t *list, unsigned int index) { +int llist_get(llist_t *list, unsigned int index) +{ /* Get node */ node_t *node = llist_get_node(list, index); - if( node == NULL ) { + if ( node == NULL ) { /* Yes i know this is stupid */ return -1; } @@ -65,19 +69,20 @@ int llist_get(llist_t *list, unsigned int index) { } -int llist_pop(llist_t *list, unsigned int index) { +int llist_pop(llist_t *list, unsigned int index) +{ /* Get node */ node_t *node = llist_get_node(list, index); - if( node == NULL ) { + if ( node == NULL ) { /* Yes i know this is stupid */ return -1; } /* Update root and head if we delete it */ - if( node == list->root ) { + if ( node == list->root ) { list->root = node->next; } - if( node == list->head ) { + if ( node == list->head ) { list->head = node->prev; } diff --git a/sem3/algo/mm2/linked/main.c b/sem3/algo/mm2/linked/main.c index 72b62cc..6f4eb6a 100644 --- a/sem3/algo/mm2/linked/main.c +++ b/sem3/algo/mm2/linked/main.c @@ -2,10 +2,11 @@ #include "node.h" #include "llist.h" -void list_print(node_t *root) { +void list_print(node_t *root) +{ int index = 0; /* Loop through notes and print them */ - while( root != NULL ) { + while ( root != NULL ) { /* Print a value */ printf("%d: %d\n", index++, root->val); @@ -15,7 +16,8 @@ void list_print(node_t *root) { } /* Remove node */ -int main() { +int main() +{ /* Do some stuff */ llist_t list; @@ -39,7 +41,7 @@ int main() { list_print(list.root); - while( list.len ) { + while ( list.len ) { printf("Popped %d\n", llist_pop(&list, 0)); } } diff --git a/sem3/algo/mm2/linked/node.c b/sem3/algo/mm2/linked/node.c index cce1be0..21f0993 100644 --- a/sem3/algo/mm2/linked/node.c +++ b/sem3/algo/mm2/linked/node.c @@ -4,10 +4,11 @@ #include <stdlib.h> /* Insert after node */ -node_t *node_insert(node_t *node, int val) { +node_t *node_insert(node_t *node, int val) +{ /* Create new node */ node_t *newNode = (node_t *) malloc( sizeof(node_t) ); - if( newNode == NULL ) { + if ( newNode == NULL ) { return NULL; } @@ -16,7 +17,7 @@ node_t *node_insert(node_t *node, int val) { newNode->prev = node; /* Check if there is node before */ - if( node == NULL ) { + if ( node == NULL ) { /* If not then there is no after */ newNode->next = NULL; return newNode; @@ -27,7 +28,7 @@ node_t *node_insert(node_t *node, int val) { node->next = newNode; /* Check node after */ - if( newNode->next != NULL ) { + if ( newNode->next != NULL ) { /* Backlink next node */ newNode->next->prev = newNode; } @@ -36,17 +37,18 @@ node_t *node_insert(node_t *node, int val) { } /* Pop node */ -int node_pop(node_t *node) { +int node_pop(node_t *node) +{ int val = node->val; /* Check prev */ - if( node->prev != NULL ) { + if ( node->prev != NULL ) { /* Point last node to next node */ node->prev->next = node->next; } /* Check next */ - if( node->next != NULL ) { + if ( node->next != NULL ) { node->next->prev = node->prev; } diff --git a/sem3/algo/mm2/queue.c b/sem3/algo/mm2/queue.c index a93f76a..a574acb 100644 --- a/sem3/algo/mm2/queue.c +++ b/sem3/algo/mm2/queue.c @@ -15,13 +15,14 @@ typedef struct { } queue_t; /* Queue functions */ -int queue_init(queue_t *q, size_t cap) { +int queue_init(queue_t *q, size_t cap) +{ /* Make the struct and set i to zero */ memset(q, 0, sizeof(queue_t)); /* Allocate the buffer */ q->buff = (int *) malloc(cap * sizeof(int)); - if( q->buff == NULL ) { + if ( q->buff == NULL ) { return 1; } @@ -30,15 +31,17 @@ int queue_init(queue_t *q, size_t cap) { return 0; } -void queue_free(queue_t *q) { +void queue_free(queue_t *q) +{ /* Free the heap buffer */ free(q->buff); } -int queue_place(queue_t *q, int val) { +int queue_place(queue_t *q, int val) +{ /* Check if full */ printf("len: %d\n", q->len); - if( q->len >= q->cap) { + if ( q->len >= q->cap) { printf("ERR: Full\n"); return EFULL; } @@ -53,15 +56,16 @@ int queue_place(queue_t *q, int val) { return 0; } -int queue_get(queue_t *q, int *val) { +int queue_get(queue_t *q, int *val) +{ /* Check if empty */ - if( !q->len ) { + if ( !q->len ) { printf("ERR: Empty\n"); return EMPTY; } /* Read value */ - if( val != NULL ){ + if ( val != NULL ){ *val = q->buff[q->tail]; } @@ -72,25 +76,26 @@ int queue_get(queue_t *q, int *val) { return 0; } -int main(void) { +int main(void) +{ int in; char com; queue_t q; queue_init(&q, 16); - for(;;) { + for (;;) { /* Read a command */ scanf("%c", &com); - if( com == 'w') { + if ( com == 'w') { printf("> "); scanf("%d", &in); queue_place(&q, in); - } else if( com == 'r' ) { + } else if ( com == 'r' ) { queue_get(&q, &in); printf("%d\n", in); - } else if( com == 'q' ) { + } else if ( com == 'q' ) { break; } } |