From dbcccca82e8f567214307bb10f19d11ae74f0095 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B0=D1=88=D0=B5=20=D0=98=D0=BC=D1=8F?= Date: Sat, 29 Nov 2025 04:23:30 -0500 Subject: [PATCH] sfds --- list.c | 137 +++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 118 insertions(+), 19 deletions(-) diff --git a/list.c b/list.c index 11309fd..d17b05d 100644 --- a/list.c +++ b/list.c @@ -1,11 +1,9 @@ - #include #include #include "util.h" #include "list.h" - void list_init(list *l) { @@ -26,21 +24,48 @@ void list_clear(list *l, list_free_cb cb) { assert(l); - // + list_node *current = l->head; + while (current != NULL) { + list_node *next = current->next; + if (cb != NULL) { + cb(current); + } + current = next; + } + l->head = NULL; + l->tail = NULL; } void list_push_front(list *l, list_node *item) { assert(l && item); - // + list_node_orphan(item); + + if (l->head == NULL) { + l->head = item; + l->tail = item; + } else { + item->next = l->head; + l->head->prev = item; + l->head = item; + } } void list_push_back(list *l, list_node *item) { assert(l && item); - // + list_node_orphan(item); + + if (l->tail == NULL) { + l->head = item; + l->tail = item; + } else { + item->prev = l->tail; + l->tail->next = item; + l->tail = item; + } } list_node * @@ -61,39 +86,82 @@ size_t list_len(list *l) { assert(l); - // - return 0; + size_t count = 0; + list_node *current = l->head; + while (current != NULL) { + count++; + current = current->next; + } + return count; } list_node * list_get(list *l, int idx) { assert(l); - // - return NULL; + if (idx < 0) return NULL; + + list_node *current = l->head; + int i = 0; + while (current != NULL && i < idx) { + current = current->next; + i++; + } + return current; } void list_insert(list *l, list_node *item, int idx) { assert(l && item); - // + + if (idx <= 0) { + list_push_front(l, item); + return; + } + + list_node *current = list_get(l, idx); + if (current == NULL) { + list_push_back(l, item); + } else { + list_node_orphan(item); + item->prev = current->prev; + item->next = current; + current->prev->next = item; + current->prev = item; + } } list_node * list_pop_front(list *l) { assert(l); - // - return NULL; + if (l->head == NULL) return NULL; + + list_node *node = l->head; + l->head = node->next; + if (l->head != NULL) { + l->head->prev = NULL; + } else { + l->tail = NULL; + } + return list_node_orphan(node); } list_node * list_pop_back(list *l) { assert(l); - // - return NULL; + if (l->tail == NULL) return NULL; + + list_node *node = l->tail; + l->tail = node->prev; + if (l->tail != NULL) { + l->tail->next = NULL; + } else { + l->head = NULL; + } + return list_node_orphan(node); } int @@ -114,18 +182,49 @@ list_node * list_remove(list *l, int idx) { assert(l); - // - return NULL; + if (idx < 0) return NULL; + + if (idx == 0) { + return list_pop_front(l); + } + + list_node *node = list_get(l, idx); + if (node == NULL) return NULL; + + if (node->next != NULL) { + node->next->prev = node->prev; + } + if (node->prev != NULL) { + node->prev->next = node->next; + } + + if (node == l->tail) { + l->tail = node->prev; + } + + return list_node_orphan(node); } void list_reverse(list *l) { assert(l); - // + list_node *current = l->head; + list_node *temp = NULL; + + while (current != NULL) { + temp = current->prev; + current->prev = current->next; + current->next = temp; + current = current->prev; + } + + if (temp != NULL) { + l->tail = l->head; + l->head = temp->prev; + } } - void list_pass(list *l, list_pass_cb cb, void *opaq) { @@ -133,4 +232,4 @@ list_pass(list *l, list_pass_cb cb, void *opaq) list_for_each(iter, l) { cb(iter, opaq); } -} \ No newline at end of file +}