From acee29c374353447c8e3bcce7c26000a991e2f6f Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Wed, 12 Apr 2023 17:02:34 +0200 Subject: [PATCH] declared why2_remove_back function removes last element so this is 1000th commit! Woah... i gone nuts idk... 860917976483233792 --- include/llist.h | 1 + src/core/lib/utils/llist.c | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/include/llist.h b/include/llist.h index 6fca169..3422fff 100644 --- a/include/llist.h +++ b/include/llist.h @@ -27,6 +27,7 @@ typedef struct _why2_node void why2_push(why2_node_t *llist_head, void *value); //PUSH ELEMENT TO LIST BACK void why2_remove(why2_node_t *llist_head, why2_node_t *node); //REMOVE ELEMENT +void why2_remove_back(why2_node_t *llist_head); //REMOVE LAST ELEMENT why2_node_t *why2_find(why2_node_t *llist_head, void *value); //FIND ELEMENT IN LIST #endif \ No newline at end of file diff --git a/src/core/lib/utils/llist.c b/src/core/lib/utils/llist.c index da6c017..88c35f1 100644 --- a/src/core/lib/utils/llist.c +++ b/src/core/lib/utils/llist.c @@ -78,6 +78,30 @@ void why2_remove(why2_node_t *llist_head, why2_node_t *node) free(node); } +void why2_remove_back(why2_node_t *llist_head) +{ + if (llist_head == NULL) return; //EMPTY LIST + + why2_node_t *buffer = llist_head; + why2_node_t *deallocating_node; + + if (buffer -> next == NULL) //ONLY ONE NODE + { + deallocating_node = buffer; + + llist_head = NULL; + } else + { + while (buffer -> next -> next != NULL) buffer = buffer -> next; //GO TO THE NODE BEFORE END + + deallocating_node = buffer -> next; + + buffer -> next = NULL; //UNLINK + } + + free(deallocating_node); +} + why2_node_t *why2_find(why2_node_t *llist_head, void *value) { if (llist_head == NULL) return NULL; //EMPTY LIST