aboutsummaryrefslogtreecommitdiff
path: root/sem3/algo/mm2/linked
diff options
context:
space:
mode:
Diffstat (limited to 'sem3/algo/mm2/linked')
-rw-r--r--sem3/algo/mm2/linked/llist.c127
-rw-r--r--sem3/algo/mm2/linked/llist.h8
-rw-r--r--sem3/algo/mm2/linked/main.c59
-rw-r--r--sem3/algo/mm2/linked/node.c81
-rw-r--r--sem3/algo/mm2/linked/node.h8
5 files changed, 139 insertions, 144 deletions
diff --git a/sem3/algo/mm2/linked/llist.c b/sem3/algo/mm2/linked/llist.c
index 8044e0b..e4166ee 100644
--- a/sem3/algo/mm2/linked/llist.c
+++ b/sem3/algo/mm2/linked/llist.c
@@ -3,92 +3,89 @@
#include <string.h>
#include <stdio.h>
-
#define OUT_OFF_BOUNDS 2
void llist_init(llist_t *list)
{
- /* Zero out structure */
- list->head = list->root = NULL;
-
- list->len = 0;
+ /* Zero out structure */
+ list->head = list->root = NULL;
+ list->len = 0;
}
void llist_append(llist_t *list, int val)
{
- /* Insert node after HEAD */
- list->head = node_insert(list->head, val);
+ /* Insert node after HEAD */
+ list->head = node_insert(list->head, val);
- /* Check if was list is empty */
- if ( list->len == 0 ) {
- /* Set root */
- list->root = list->head;
- }
+ /* Check if was list is empty */
+ if (list->len == 0) {
+ /* Set root */
+ list->root = list->head;
+ }
- /* Increase count */
- list->len++;
+ /* Increase count */
+ list->len++;
}
node_t *llist_get_node(llist_t *list, unsigned int index)
{
- /* Check if we have it */
- if ( index >= list->len ) {
- return NULL;
- }
-
- /* Find the best way to go down the list */
- int direc = index > (list->len / 2) ? -1 : 1;
-
- /* Setup start location */
- int pos = direc > 0 ? 0 : list->len-1;
- node_t *node = direc > 0 ? list->root : list->head;
-
- /* Go to index */
- while ( pos != index ) {
- /* TODO kinda risky, we trust our math and len */
- node = direc > 0 ? node->next : node->prev;
-
- pos += direc;
- }
-
- return node;
+ /* Check if we have it */
+ if (index >= list->len) {
+ return NULL;
+ }
+
+ /* Find the best way to go down the list */
+ int direc = index > (list->len / 2) ? -1 : 1;
+
+ /* Setup start location */
+ int pos = direc > 0 ? 0 : list->len - 1;
+ node_t *node = direc > 0 ? list->root : list->head;
+
+ /* Go to index */
+ while (pos != index) {
+ /* TODO kinda risky, we trust our math and len */
+ node = direc > 0 ? node->next : node->prev;
+
+ pos += direc;
+ }
+
+ return node;
}
int llist_get(llist_t *list, unsigned int index)
{
- /* Get node */
- node_t *node = llist_get_node(list, index);
- if ( node == NULL ) {
- /* Yes i know this is stupid */
- return -1;
- }
-
- /* Return value */
- return node->val;
+ /* Get node */
+ node_t *node = llist_get_node(list, index);
+ if (node == NULL) {
+ /* Yes i know this is stupid */
+ return -1;
+ }
+
+ /* Return value */
+ return node->val;
}
-
int llist_pop(llist_t *list, unsigned int index)
{
- /* Get node */
- node_t *node = llist_get_node(list, index);
- if ( node == NULL ) {
- /* Yes i know this is stupid */
- return -1;
- }
-
- /* Update root and head if we delete it */
- if ( node == list->root ) {
- list->root = node->next;
- }
- if ( node == list->head ) {
- list->head = node->prev;
- }
-
- /* Keep len up to date */
- list->len--;
-
- /* Delete stuff */
- return node_pop(node);
+ /* Get node */
+ node_t *node = llist_get_node(list, index);
+ if (node == NULL) {
+ /* Yes i know this is stupid */
+ return -1;
+ }
+
+ /* Update root and head if we delete it */
+ if (node == list->root) {
+ list->root = node->next;
+ }
+ if (node == list->head) {
+ list->head = node->prev;
+ }
+
+ /* Keep len up to date */
+ list->len--;
+
+ /* Delete stuff */
+ return node_pop(node);
}
diff --git a/sem3/algo/mm2/linked/llist.h b/sem3/algo/mm2/linked/llist.h
index e52be89..14130c4 100644
--- a/sem3/algo/mm2/linked/llist.h
+++ b/sem3/algo/mm2/linked/llist.h
@@ -4,9 +4,9 @@
#include "node.h"
typedef struct {
- node_t *root;
- node_t *head;
- unsigned int len;
+ node_t *root;
+ node_t *head;
+ unsigned int len;
} llist_t;
void llist_init(llist_t *list);
@@ -19,4 +19,4 @@ int llist_get(llist_t *list, unsigned int index);
int llist_pop(llist_t *list, unsigned int index);
-#endif
+#endif
diff --git a/sem3/algo/mm2/linked/main.c b/sem3/algo/mm2/linked/main.c
index 6f4eb6a..8637796 100644
--- a/sem3/algo/mm2/linked/main.c
+++ b/sem3/algo/mm2/linked/main.c
@@ -4,44 +4,43 @@
void list_print(node_t *root)
{
- int index = 0;
- /* Loop through notes and print them */
- while ( root != NULL ) {
- /* Print a value */
- printf("%d: %d\n", index++, root->val);
+ int index = 0;
+ /* Loop through notes and print them */
+ while (root != NULL) {
+ /* Print a value */
+ printf("%d: %d\n", index++, root->val);
- /* Next value */
- root = root->next;
- }
+ /* Next value */
+ root = root->next;
+ }
}
/* Remove node */
int main()
{
-
- /* Do some stuff */
- llist_t list;
- llist_init(&list);
+ /* Do some stuff */
+ llist_t list;
+ llist_init(&list);
- llist_append(&list, 11); // 0
- llist_append(&list, 22); // 1
- llist_append(&list, 33); // 2
- llist_append(&list, 44); // 3
- llist_append(&list, 89); // 4
- llist_append(&list, 12); // 5
- llist_append(&list, 2); // 6
- llist_append(&list, 1); // 7
- llist_append(&list, 7); // 8
- llist_append(&list, 232);// 9
+ llist_append(&list, 11); // 0
+ llist_append(&list, 22); // 1
+ llist_append(&list, 33); // 2
+ llist_append(&list, 44); // 3
+ llist_append(&list, 89); // 4
+ llist_append(&list, 12); // 5
+ llist_append(&list, 2); // 6
+ llist_append(&list, 1); // 7
+ llist_append(&list, 7); // 8
+ llist_append(&list, 232); // 9
- list_print(list.root);
- printf("%d\n", llist_get(&list, 5));
- llist_pop(&list, 9);
- printf("%d\n", llist_get(&list, 7));
+ list_print(list.root);
+ printf("%d\n", llist_get(&list, 5));
+ llist_pop(&list, 9);
+ printf("%d\n", llist_get(&list, 7));
- list_print(list.root);
+ list_print(list.root);
- while ( list.len ) {
- printf("Popped %d\n", llist_pop(&list, 0));
- }
+ 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 21f0993..87f2e4c 100644
--- a/sem3/algo/mm2/linked/node.c
+++ b/sem3/algo/mm2/linked/node.c
@@ -6,54 +6,53 @@
/* Insert after node */
node_t *node_insert(node_t *node, int val)
{
- /* Create new node */
- node_t *newNode = (node_t *) malloc( sizeof(node_t) );
- if ( newNode == NULL ) {
- return NULL;
- }
-
-
- newNode->val = val;
- newNode->prev = node;
-
- /* Check if there is node before */
- if ( node == NULL ) {
- /* If not then there is no after */
- newNode->next = NULL;
- return newNode;
- }
-
- /* Set next node */
- newNode->next = node->next;
- node->next = newNode;
-
- /* Check node after */
- if ( newNode->next != NULL ) {
- /* Backlink next node */
- newNode->next->prev = newNode;
- }
-
- return newNode;
+ /* Create new node */
+ node_t *newNode = (node_t *)malloc(sizeof(node_t));
+ if (newNode == NULL) {
+ return NULL;
+ }
+
+ newNode->val = val;
+ newNode->prev = node;
+
+ /* Check if there is node before */
+ if (node == NULL) {
+ /* If not then there is no after */
+ newNode->next = NULL;
+ return newNode;
+ }
+
+ /* Set next node */
+ newNode->next = node->next;
+ node->next = newNode;
+
+ /* Check node after */
+ if (newNode->next != NULL) {
+ /* Backlink next node */
+ newNode->next->prev = newNode;
+ }
+
+ return newNode;
}
/* Pop node */
int node_pop(node_t *node)
{
- int val = node->val;
+ int val = node->val;
- /* Check prev */
- if ( node->prev != NULL ) {
- /* Point last node to next node */
- node->prev->next = node->next;
- }
+ /* Check prev */
+ if (node->prev != NULL) {
+ /* Point last node to next node */
+ node->prev->next = node->next;
+ }
- /* Check next */
- if ( node->next != NULL ) {
- node->next->prev = node->prev;
- }
+ /* Check next */
+ if (node->next != NULL) {
+ node->next->prev = node->prev;
+ }
- /* Free memory */
- free(node);
+ /* Free memory */
+ free(node);
- return val;
+ return val;
}
diff --git a/sem3/algo/mm2/linked/node.h b/sem3/algo/mm2/linked/node.h
index 027926b..27f29c6 100644
--- a/sem3/algo/mm2/linked/node.h
+++ b/sem3/algo/mm2/linked/node.h
@@ -2,9 +2,9 @@
#define NODE_H
typedef struct node_struct {
- int val;
- struct node_struct *next;
- struct node_struct *prev;
+ int val;
+ struct node_struct *next;
+ struct node_struct *prev;
} node_t;
/** @brief Create a new node after specified node
@@ -20,4 +20,4 @@ node_t *node_insert(node_t *node, int val);
*/
int node_pop(node_t *node);
-#endif
+#endif