From 185af2c969c5d90ebcf7948e3b9fd8695b78256f Mon Sep 17 00:00:00 2001
From: Marvin Borner
Date: Tue, 15 Sep 2020 18:30:27 +0200
Subject: Added some list functions

---
 libc/inc/list.h |  5 ++++-
 libc/list.c     | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++------
 2 files changed, 54 insertions(+), 7 deletions(-)

(limited to 'libc')

diff --git a/libc/inc/list.h b/libc/inc/list.h
index 0a5c6c0..0c3668d 100644
--- a/libc/inc/list.h
+++ b/libc/inc/list.h
@@ -20,6 +20,9 @@ struct list *list_new();
 /* struct node *list_new_node(); */ // TODO: Make node-specific things static/private?
 /* void list_add_node(struct list *list, struct node *node); */
 struct node *list_add(struct list *list, void *data);
-void list_remove(struct list *list, struct node *node);
+struct list *list_remove(struct list *list, struct node *node);
+struct node *list_last(struct list *list);
+struct list *list_swap(struct list *list, struct node *a, struct node *b);
+struct node *list_first_data(struct list *list, void *data);
 
 #endif
diff --git a/libc/list.c b/libc/list.c
index 89de4d0..7baae92 100644
--- a/libc/list.c
+++ b/libc/list.c
@@ -25,7 +25,7 @@ struct node *list_new_node()
 
 struct node *list_add_node(struct list *list, struct node *node)
 {
-	if (list == NULL)
+	if (!list || !node)
 		return NULL;
 
 	if (list->head == NULL) {
@@ -45,6 +45,49 @@ struct node *list_add_node(struct list *list, struct node *node)
 	return node;
 }
 
+struct node *list_last(struct list *list)
+{
+	if (!list || !list->head)
+		return NULL;
+
+	struct node *iterator = list->head;
+	while (iterator != NULL) {
+		if (iterator->next == NULL)
+			return iterator;
+		iterator = iterator->next;
+	}
+
+	return NULL;
+}
+
+struct node *list_first_data(struct list *list, void *data)
+{
+	if (!list || !list->head || !data)
+		return NULL;
+
+	struct node *iterator = list->head;
+	while (iterator != NULL) {
+		if (iterator->data == data)
+			return iterator;
+		iterator = iterator->next;
+	}
+
+	return NULL;
+}
+
+// TODO: Actually swap the nodes, not the data
+struct list *list_swap(struct list *list, struct node *a, struct node *b)
+{
+	if (!list || !list->head || !a || !b)
+		return NULL;
+
+	void *tmp = a->data;
+	a->data = b->data;
+	b->data = tmp;
+
+	return list;
+}
+
 struct node *list_add(struct list *list, void *data)
 {
 	struct node *node = list_new_node();
@@ -53,23 +96,24 @@ struct node *list_add(struct list *list, void *data)
 }
 
 // Maybe list_remove_node?
-void list_remove(struct list *list, struct node *node)
+struct list *list_remove(struct list *list, struct node *node)
 {
-	if (list == NULL || list->head == NULL)
-		return;
+	if (!list || !list->head || !node)
+		return NULL;
 
 	if (list->head == node) {
 		list->head = list->head->next;
-		return;
+		return list;
 	}
 
 	struct node *iterator = list->head->next;
 	while (iterator != node) {
 		iterator = iterator->next;
 		if (iterator == NULL)
-			return;
+			return NULL;
 	}
 
 	iterator->prev->next = iterator->next;
 	iterator->next->prev = iterator->prev;
+	return list;
 }
-- 
cgit v1.2.3