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