summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lichen.c22
-rw-r--r--lichen.h1
-rw-r--r--lichen.test.c66
3 files changed, 86 insertions, 3 deletions
diff --git a/lichen.c b/lichen.c
index f028d92..b0b0684 100644
--- a/lichen.c
+++ b/lichen.c
@@ -79,10 +79,10 @@ void li_free_ll(struct li_ll_t *list, void (*free_data)(void*)) {
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;
}
+ n->data = data;
if (list->end != NULL) {
list->end->next = n;
list->end = n;
@@ -93,9 +93,25 @@ void li_ll_append(struct li_ll_t *list, void *data) {
list->count += 1;
}
+
+void li_ll_prepend(struct li_ll_t *list, void *data) {
+ li_llnode_t *n = li_alloc_llnode();
+ if (n == NULL) {
+ return;
+ }
+ n->data = data;
+ if (list->head != NULL) {
+ n->next = list->head;
+ list->head = 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 a607d24..4bcd608 100644
--- a/lichen.h
+++ b/lichen.h
@@ -50,6 +50,7 @@ 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);
+void li_ll_prepend(struct li_ll_t *list, void *data);
struct li_ll_t * li_copy_list(struct li_ll_t *list);
diff --git a/lichen.test.c b/lichen.test.c
index bca7d27..622f480 100644
--- a/lichen.test.c
+++ b/lichen.test.c
@@ -81,6 +81,72 @@ LILY_TEST("append to list") {
#include LILY_PUSH_TEST()
+LILY_TEST("prepend 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_prepend(list, &b);
+ CHECK_EQ(list->count, 1, "%d");
+ REQUIRE_NEQ(list->head, NULL, "%p");
+ CHECK_EQ(list->head->data, &b, "%p");
+ CHECK_EQ(list->head->next, NULL, "%p");
+
+ li_ll_prepend(list, &a);
+ CHECK_EQ(list->count, 2, "%d");
+ REQUIRE_NEQ(list->head->next, NULL, "%p");
+ CHECK_EQ(list->head->data, &a, "%p");
+ CHECK_EQ(list->head->next->data, &b, "%p");
+ CHECK_EQ(list->head->next->next, NULL, "%p");
+
+ li_ll_prepend(list, &c);
+ CHECK_EQ(list->count, 3, "%d");
+ REQUIRE_NEQ(list->head->next->next, NULL, "%p");
+ CHECK_EQ(list->head->data, &c, "%p");
+ CHECK_EQ(list->head->next->data, &a, "%p");
+ CHECK_EQ(list->head->next->next->data, &b, "%p");
+ CHECK_EQ(list->head->next->next->next, NULL, "%p");
+
+ li_free_ll(list, do_nothing);
+}
+#include LILY_PUSH_TEST()
+
+
+LILY_TEST("mix prepends and appends") {
+ struct li_ll_t *list = li_alloc_ll();
+ REQUIRE_NEQ(list, NULL, "%p");
+
+ int a = 1; int b = 2; int c = 3;
+
+ li_ll_prepend(list, &b);
+ CHECK_EQ(list->count, 1, "%d");
+ REQUIRE_NEQ(list->head, NULL, "%p");
+ CHECK_EQ(list->head->data, &b, "%p");
+ CHECK_EQ(list->head->next, NULL, "%p");
+
+ li_ll_prepend(list, &a);
+ CHECK_EQ(list->count, 2, "%d");
+ REQUIRE_NEQ(list->head->next, NULL, "%p");
+ CHECK_EQ(list->head->data, &a, "%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->data, &a, "%p");
+ CHECK_EQ(list->head->next->data, &b, "%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()