moved some code from why2_free to remove_node
This commit is contained in:
parent
114695f653
commit
026f2fa458
@ -23,8 +23,6 @@ void push_to_list(void *pointer)
|
||||
{
|
||||
node_t *new_node = malloc(sizeof(node_t)); //CREATE ANOTHER ELEMENT
|
||||
node_t *node_buffer_1;
|
||||
//node_t *node_buffer_2;
|
||||
//int buffer = 0;
|
||||
|
||||
new_node -> pointer = pointer; //ALLOCATED POINTER
|
||||
new_node -> next = NULL; //NEXT NODE
|
||||
@ -41,26 +39,31 @@ void push_to_list(void *pointer)
|
||||
} else
|
||||
{
|
||||
node_buffer_1 = head;
|
||||
//node_buffer_2 = head;
|
||||
|
||||
while (node_buffer_1 -> next != NULL) //GET 'LAST' NODE (next POINTER)
|
||||
{
|
||||
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
|
||||
node_buffer_1 -> next = new_node;
|
||||
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
|
||||
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
|
||||
{
|
||||
node_buffer = node_buffer -> next;
|
||||
|
||||
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;
|
||||
}
|
||||
if (node_buffer -> pointer == pointer) break; //FOUND
|
||||
}
|
||||
|
||||
remove_node(node_buffer);
|
||||
free(pointer);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user