aboutsummaryrefslogtreecommitdiff
path: root/sem3/algo/mm2/linked/node.c
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2020-02-11 12:24:56 +0100
committerJulian T <julian@jtle.dk>2020-02-11 12:24:56 +0100
commit6db1a2cdd3b96731f2e092d55d8c2136eabc52d0 (patch)
tree2be8fae8ce82d708ed9f00f376dda14420850e80 /sem3/algo/mm2/linked/node.c
parent57305119e05559c1c37e903aef89cd43f44c42c9 (diff)
Rename and cleanup
Diffstat (limited to 'sem3/algo/mm2/linked/node.c')
-rw-r--r--sem3/algo/mm2/linked/node.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/sem3/algo/mm2/linked/node.c b/sem3/algo/mm2/linked/node.c
new file mode 100644
index 0000000..cce1be0
--- /dev/null
+++ b/sem3/algo/mm2/linked/node.c
@@ -0,0 +1,57 @@
+#include "node.h"
+
+#include <string.h>
+#include <stdlib.h>
+
+/* 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;
+}
+
+/* Pop node */
+int node_pop(node_t *node) {
+ int val = node->val;
+
+ /* 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;
+ }
+
+ /* Free memory */
+ free(node);
+
+ return val;
+}