diff options
Diffstat (limited to 'libcnary/node_list.c')
| -rw-r--r-- | libcnary/node_list.c | 28 |
1 files changed, 16 insertions, 12 deletions
diff --git a/libcnary/node_list.c b/libcnary/node_list.c index aee3bd6..f6c2c70 100644 --- a/libcnary/node_list.c +++ b/libcnary/node_list.c | |||
| @@ -28,12 +28,14 @@ | |||
| 28 | #include "node.h" | 28 | #include "node.h" |
| 29 | #include "node_list.h" | 29 | #include "node_list.h" |
| 30 | 30 | ||
| 31 | void node_list_destroy(node_list_t* list) { | 31 | void node_list_destroy(node_list_t list) |
| 32 | { | ||
| 32 | free(list); | 33 | free(list); |
| 33 | } | 34 | } |
| 34 | 35 | ||
| 35 | node_list_t* node_list_create() { | 36 | node_list_t node_list_create() |
| 36 | node_list_t* list = (node_list_t*)calloc(1, sizeof(node_list_t)); | 37 | { |
| 38 | node_list_t list = (node_list_t)calloc(1, sizeof(struct node_list)); | ||
| 37 | if (list == NULL) { | 39 | if (list == NULL) { |
| 38 | return NULL; | 40 | return NULL; |
| 39 | } | 41 | } |
| @@ -45,11 +47,12 @@ node_list_t* node_list_create() { | |||
| 45 | return list; | 47 | return list; |
| 46 | } | 48 | } |
| 47 | 49 | ||
| 48 | int node_list_add(node_list_t* list, node_t* node) { | 50 | int node_list_add(node_list_t list, node_t node) |
| 51 | { | ||
| 49 | if (!list || !node) return -1; | 52 | if (!list || !node) return -1; |
| 50 | 53 | ||
| 51 | // Find the last element in the list | 54 | // Find the last element in the list |
| 52 | node_t* last = list->end; | 55 | node_t last = list->end; |
| 53 | 56 | ||
| 54 | // Setup our new node as the new last element | 57 | // Setup our new node as the new last element |
| 55 | node->next = NULL; | 58 | node->next = NULL; |
| @@ -72,17 +75,18 @@ int node_list_add(node_list_t* list, node_t* node) { | |||
| 72 | return 0; | 75 | return 0; |
| 73 | } | 76 | } |
| 74 | 77 | ||
| 75 | int node_list_insert(node_list_t* list, unsigned int node_index, node_t* node) { | 78 | int node_list_insert(node_list_t list, unsigned int node_index, node_t node) |
| 79 | { | ||
| 76 | if (!list || !node) return -1; | 80 | if (!list || !node) return -1; |
| 77 | if (node_index >= list->count) { | 81 | if (node_index >= list->count) { |
| 78 | return node_list_add(list, node); | 82 | return node_list_add(list, node); |
| 79 | } | 83 | } |
| 80 | 84 | ||
| 81 | // Get the first element in the list | 85 | // Get the first element in the list |
| 82 | node_t* cur = list->begin; | 86 | node_t cur = list->begin; |
| 83 | 87 | ||
| 84 | unsigned int pos = 0; | 88 | unsigned int pos = 0; |
| 85 | node_t* prev = NULL; | 89 | node_t prev = NULL; |
| 86 | 90 | ||
| 87 | if (node_index > 0) { | 91 | if (node_index > 0) { |
| 88 | while (pos < node_index) { | 92 | while (pos < node_index) { |
| @@ -120,15 +124,16 @@ int node_list_insert(node_list_t* list, unsigned int node_index, node_t* node) { | |||
| 120 | return 0; | 124 | return 0; |
| 121 | } | 125 | } |
| 122 | 126 | ||
| 123 | int node_list_remove(node_list_t* list, node_t* node) { | 127 | int node_list_remove(node_list_t list, node_t node) |
| 128 | { | ||
| 124 | if (!list || !node) return -1; | 129 | if (!list || !node) return -1; |
| 125 | if (list->count == 0) return -1; | 130 | if (list->count == 0) return -1; |
| 126 | 131 | ||
| 127 | int node_index = 0; | 132 | int node_index = 0; |
| 128 | node_t* n; | 133 | node_t n; |
| 129 | for (n = list->begin; n; n = n->next) { | 134 | for (n = list->begin; n; n = n->next) { |
| 130 | if (node == n) { | 135 | if (node == n) { |
| 131 | node_t* newnode = node->next; | 136 | node_t newnode = node->next; |
| 132 | if (node->prev) { | 137 | if (node->prev) { |
| 133 | node->prev->next = newnode; | 138 | node->prev->next = newnode; |
| 134 | if (newnode) { | 139 | if (newnode) { |
| @@ -153,4 +158,3 @@ int node_list_remove(node_list_t* list, node_t* node) { | |||
| 153 | } | 158 | } |
| 154 | return -1; | 159 | return -1; |
| 155 | } | 160 | } |
| 156 | |||
