aboutsummaryrefslogtreecommitdiff
path: root/sem3/algo/mm2/linked
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2020-02-15 20:28:59 +0100
committerJulian T <julian@jtle.dk>2020-02-15 20:28:59 +0100
commit8076d839bf69e619f2e504122ec2ceb790a0b978 (patch)
treefa6cb7c649939c3a002676ce142105360d3d2fd6 /sem3/algo/mm2/linked
parentf17e8e195bd1ee3f911ae311dce8e8f7df4f8457 (diff)
Changed to a more consistent codestyle
Diffstat (limited to 'sem3/algo/mm2/linked')
-rw-r--r--sem3/algo/mm2/linked/llist.c29
-rw-r--r--sem3/algo/mm2/linked/main.c10
-rw-r--r--sem3/algo/mm2/linked/node.c16
3 files changed, 32 insertions, 23 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;
}