summaryrefslogtreecommitdiff
path: root/linked_list.c
diff options
context:
space:
mode:
authormrb0nk500 <b0nk@b0nk.xyz>2022-07-26 18:16:18 -0300
committermrb0nk500 <b0nk@b0nk.xyz>2022-07-26 18:16:18 -0300
commite108eea9d5b6846a9c1e227db1bcb4e15aebaab1 (patch)
treeb79fe0dfbf4ddb5ed8db1a60c1dbe22b496e42f7 /linked_list.c
parentb0470f2db735f4dffda742fe38a7f9488a5ebc08 (diff)
linked_list: Add `linked_list` type.
This is a type generic doubly linked list implementation. I used the implementation from DevkitPro's libnds library as a baseline. Link to libnds implementation: https://github.com/devkitPro/libnds/blob/master/source/arm9/linkedlist.c
Diffstat (limited to 'linked_list.c')
-rw-r--r--linked_list.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/linked_list.c b/linked_list.c
new file mode 100644
index 0000000..792da4d
--- /dev/null
+++ b/linked_list.c
@@ -0,0 +1,59 @@
+#include <stdlib.h>
+#include "linked_list.h"
+
+linked_list *add_node(linked_list **tail, void *data) {
+ if (tail == NULL) {
+ return NULL;
+ } else {
+ linked_list *node = calloc(1, sizeof(linked_list));
+ if (node == NULL) {
+ return NULL;
+ } else {
+ node->next = NULL;
+ node->data = data;
+
+ if (*tail == NULL) {
+ node->prev = NULL;
+ *tail = node;
+ } else {
+ (*tail)->next = node;
+ node->prev = *tail;
+ }
+
+ return node;
+ }
+ }
+}
+
+
+void remove_node(linked_list *node) {
+ if (node != NULL) {
+ if (node->prev != NULL) {
+ node->prev->next = node->next;
+ }
+
+ if (node->next != NULL) {
+ node->next->prev = node->prev;
+ }
+
+ free(node);
+ }
+}
+
+linked_list *get_head(linked_list *node) {
+ linked_list *head;
+ for (head = node; head != NULL && head->prev != NULL; head = head->prev);
+ return head;
+}
+
+linked_list *get_tail(linked_list *node) {
+ linked_list *tail;
+ for (tail = node; tail != NULL && tail->next != NULL; tail = tail->next);
+ return tail;
+}
+
+int linked_list_size(linked_list *tail) {
+ int i = 0;
+ for (linked_list *node = tail; node != NULL; node = node->prev, ++i);
+ return i;
+}