]> git.mdlowis.com Git - archive/carl.git/commitdiff
Added initial implementation of doubly linked list
authorMichael D. Lowis <mike@mdlowis.com>
Sun, 28 Jun 2015 16:27:29 +0000 (12:27 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Sun, 28 Jun 2015 16:27:29 +0000 (12:27 -0400)
source/data/list.c [new file with mode: 0644]
source/data/list.h

diff --git a/source/data/list.c b/source/data/list.c
new file mode 100644 (file)
index 0000000..4ef2583
--- /dev/null
@@ -0,0 +1,92 @@
+#include <data/list.h>
+
+void list_init(list_t* list)
+{
+    list->head = NULL;
+    list->tail = NULL;
+}
+
+bool list_empty(list_t* list)
+{
+    return (list->head == NULL);
+}
+
+size_t list_size(list_t* list)
+{
+    size_t sz = 0;
+    list_node_t* node = list->head;
+    while (node != NULL) {
+        sz++;
+        node = node->next;
+    }
+    return sz;
+}
+
+list_node_t* list_front(list_t* list)
+{
+    return list->head;
+}
+
+void list_push_front(list_t* list, list_node_t* node)
+{
+    node->prev = NULL;
+    node->next = list->head;
+    list->head = node;
+    if (list->tail == NULL)
+        list->tail = node;
+}
+
+list_node_t* list_pop_front(list_t* list)
+{
+    list_node_t* node = list->head;
+    list->head = node->next;
+    if (list->head == NULL)
+        list->tail = NULL;
+    node->next = NULL;
+    return node;
+}
+
+list_node_t* list_back(list_t* list)
+{
+    return list->tail;
+}
+
+void list_push_back(list_t* list, list_node_t* node)
+{
+    node->next = NULL;
+    node->prev = list->tail;
+    list->tail = node;
+    if (list->head == NULL)
+        list->head = node;
+}
+
+list_node_t* list_pop_back(list_t* list)
+{
+    list_node_t* node = list->tail;
+    list->tail = node->prev;
+    if (list->tail == NULL)
+        list->head = NULL;
+    node->prev = NULL;
+    return node;
+}
+
+bool list_node_has_next(list_node_t* node)
+{
+    return (node->next != NULL);
+}
+
+list_node_t* list_node_next(list_node_t* node)
+{
+    return node->next;
+}
+
+bool list_node_has_prev(list_node_t* node)
+{
+    return (node->prev != NULL);
+}
+
+list_node_t* list_node_prev(list_node_t* node)
+{
+    return node->prev;
+}
+
index 5a4141dbec6d2b084fbed1c3f06af304176b9790..b6b413f5ac1580a5e4fe91af6c36e91fc94b4cd5 100644 (file)
@@ -4,6 +4,8 @@
 #ifndef LIST_H
 #define LIST_H
 
+#include <libc.h>
+
 typedef struct list_node_t {
     struct list_node_t* next;
     struct list_node_t* prev;
@@ -32,11 +34,11 @@ void list_push_back(list_t* list, list_node_t* node);
 
 list_node_t* list_pop_back(list_t* list);
 
-bool list_node_hasnext(list_node_t* node);
+bool list_node_has_next(list_node_t* node);
 
 list_node_t* list_node_next(list_node_t* node);
 
-bool list_node_hasprev(list_node_t* node);
+bool list_node_has_prev(list_node_t* node);
 
 list_node_t* list_node_prev(list_node_t* node);