declared why2_remove_back function

removes last element

so this is 1000th commit! Woah... i gone nuts

idk...

860917976483233792
This commit is contained in:
Václav Šmejkal 2023-04-12 17:02:34 +02:00
parent 3eaba0e0c4
commit acee29c374
Signed by: ENGO150
GPG Key ID: 4A57E86482968843
2 changed files with 25 additions and 0 deletions

View File

@ -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_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(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 why2_node_t *why2_find(why2_node_t *llist_head, void *value); //FIND ELEMENT IN LIST
#endif #endif

View File

@ -78,6 +78,30 @@ void why2_remove(why2_node_t *llist_head, why2_node_t *node)
free(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) why2_node_t *why2_find(why2_node_t *llist_head, void *value)
{ {
if (llist_head == NULL) return NULL; //EMPTY LIST if (llist_head == NULL) return NULL; //EMPTY LIST