summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2025-10-24 13:32:54 -0500
committersanine <sanine.not@pm.me>2025-10-24 13:32:54 -0500
commit24fbfb8c41c62b11506668a22074379115f57600 (patch)
tree1994109fe42df1e0589a9e96051a235b611ba487
parent30efa292790dfdc9eedefa97a9f82e1cac2aeaa0 (diff)
implement basic linked list append
-rw-r--r--lichen.c47
-rw-r--r--lichen.h9
-rw-r--r--lichen.test.c30
3 files changed, 86 insertions, 0 deletions
diff --git a/lichen.c b/lichen.c
index e8c8e19..f028d92 100644
--- a/lichen.c
+++ b/lichen.c
@@ -49,6 +49,53 @@ li_llnode_t * li_copy_llnode(li_llnode_t *n) {
void li_free_llnode(li_llnode_t *n, void (*free_data)(void*)) {
if (n != NULL) {
free_data(n->data);
+ li_free_llnode(n->next, free_data);
}
free(n);
}
+
+
+struct li_ll_t * li_alloc_ll() {
+ struct li_ll_t *list = malloc(sizeof(struct li_ll_t));
+ if (list == NULL) {
+ fprintf(stderr, "failed to allocate linked list\n");
+ return NULL;
+ }
+
+ list->tag = 0;
+ list->count = 0;
+ list->head = NULL;
+ list->end = NULL;
+ return list;
+}
+
+void li_free_ll(struct li_ll_t *list, void (*free_data)(void*)) {
+ if (list != NULL) {
+ li_free_llnode(list->head, free_data);
+ }
+ free(list);
+}
+
+
+void li_ll_append(struct li_ll_t *list, void *data) {
+ li_llnode_t *n = li_alloc_llnode();
+ n->data = data;
+ if (n == NULL) {
+ return;
+ }
+ if (list->end != NULL) {
+ list->end->next = n;
+ list->end = n;
+ } else {
+ list->head = n;
+ list->end = n;
+ }
+ list->count += 1;
+}
+
+struct li_ll_t * li_copy_list(struct li_ll_t *list) {
+ list = list;
+ return NULL;
+}
+
+
diff --git a/lichen.h b/lichen.h
index b30c8cb..a607d24 100644
--- a/lichen.h
+++ b/lichen.h
@@ -41,9 +41,18 @@ typedef struct li_ll_t {
int tag;
size_t count;
li_llnode_t *head;
+ li_llnode_t *end;
} li_ll_t;
+// allocate a linked list
+struct li_ll_t * li_alloc_ll();
+// free a linked list
+void li_free_ll(struct li_ll_t *list, void (*free_data)(void*));
+void li_ll_append(struct li_ll_t *list, void *data);
+struct li_ll_t * li_copy_list(struct li_ll_t *list);
+
+
#endif
diff --git a/lichen.test.c b/lichen.test.c
index 68d26d0..bca7d27 100644
--- a/lichen.test.c
+++ b/lichen.test.c
@@ -51,6 +51,36 @@ LILY_TEST("allocate, copy, and destroy llnode") {
#include LILY_PUSH_TEST()
+
+LILY_TEST("append to list") {
+ struct li_ll_t *list = li_alloc_ll();
+ REQUIRE_NEQ(list, NULL, "%p");
+
+ int a = 1; int b = 2; int c = 3;
+
+ li_ll_append(list, &a);
+ CHECK_EQ(list->count, 1, "%d");
+ REQUIRE_NEQ(list->head, NULL, "%p");
+ CHECK_EQ(list->head->data, &a, "%p");
+ CHECK_EQ(list->head->next, NULL, "%p");
+
+ li_ll_append(list, &b);
+ CHECK_EQ(list->count, 2, "%d");
+ REQUIRE_NEQ(list->head->next, NULL, "%p");
+ CHECK_EQ(list->head->next->data, &b, "%p");
+ CHECK_EQ(list->head->next->next, NULL, "%p");
+
+ li_ll_append(list, &c);
+ CHECK_EQ(list->count, 3, "%d");
+ REQUIRE_NEQ(list->head->next->next, NULL, "%p");
+ CHECK_EQ(list->head->next->next->data, &c, "%p");
+ CHECK_EQ(list->head->next->next->next, NULL, "%p");
+
+ li_free_ll(list, do_nothing);
+}
+#include LILY_PUSH_TEST()
+
+
#define LILY_FILE_END
#include LILY_REGISTER_TESTS()