moved some code from why2_free to remove_node

This commit is contained in:
Václav Šmejkal 2023-01-31 16:41:02 +01:00
parent 114695f653
commit 026f2fa458
Signed by: ENGO150
GPG Key ID: 4A57E86482968843

View File

@ -23,8 +23,6 @@ void push_to_list(void *pointer)
{ {
node_t *new_node = malloc(sizeof(node_t)); //CREATE ANOTHER ELEMENT node_t *new_node = malloc(sizeof(node_t)); //CREATE ANOTHER ELEMENT
node_t *node_buffer_1; node_t *node_buffer_1;
//node_t *node_buffer_2;
//int buffer = 0;
new_node -> pointer = pointer; //ALLOCATED POINTER new_node -> pointer = pointer; //ALLOCATED POINTER
new_node -> next = NULL; //NEXT NODE new_node -> next = NULL; //NEXT NODE
@ -41,26 +39,31 @@ void push_to_list(void *pointer)
} else } else
{ {
node_buffer_1 = head; node_buffer_1 = head;
//node_buffer_2 = head;
while (node_buffer_1 -> next != NULL) //GET 'LAST' NODE (next POINTER) while (node_buffer_1 -> next != NULL) //GET 'LAST' NODE (next POINTER)
{ {
node_buffer_1 = node_buffer_1 -> next; node_buffer_1 = node_buffer_1 -> next;
//buffer++; //THIS WILL BE USED FOR GETTING last POINTER
} }
/*for (int i = 0; i < buffer; i++) //GET NODE BEFORE THE 'LAST' ONE (last POINTER)
{
node_buffer_2 = node_buffer_2 -> next;
}*/
//LINK //LINK
node_buffer_1 -> next = new_node; node_buffer_1 -> next = new_node;
new_node -> last = node_buffer_1; new_node -> last = node_buffer_1;
//new_node -> last = node_buffer_2; //idk i made it like this but found much better and easies solution | i would rather keep this here just in case I fucked something up
} }
} }
void remove_node(node_t *node)
{
//REMOVE NODE
node -> last -> next = node -> next; //last WILL BE NEVER NULL CUZ CAN ONLY BE ANOTHER NODE OR HEAD
if (node -> next != NULL)
{
node -> next -> last = node -> last;
}
//DEALLOCATION
free(node); //TODO: Valgrind says node is still reachable, but it is deallocated here (I think)
}
//GLOBAL //GLOBAL
void *why2_malloc(unsigned long size) void *why2_malloc(unsigned long size)
{ {
@ -98,20 +101,9 @@ void why2_free(void *pointer)
while (node_buffer -> next != NULL) //if pointer won't be found, nothing will happen | idk if I wanna fix this or leave it like this while (node_buffer -> next != NULL) //if pointer won't be found, nothing will happen | idk if I wanna fix this or leave it like this
{ {
node_buffer = node_buffer -> next; node_buffer = node_buffer -> next;
if (node_buffer -> pointer == pointer) break; //FOUND
if (node_buffer -> pointer == pointer) //FOUND
{
//REMOVE NODE
node_buffer -> last -> next = node_buffer -> next; //last WILL BE NEVER NULL CUZ CAN ONLY BE ANOTHER NODE OR HEAD
if (node_buffer -> next != NULL)
{
node_buffer -> next -> last = node_buffer -> last;
}
//DEALLOCATION
free(node_buffer -> pointer);
free(node_buffer);
break;
}
} }
remove_node(node_buffer);
free(pointer);
} }