blob: cce1be0af0c584d5f5159ba5079105320b4c1eb9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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;
}
|