replaced doubly linked list with single linked list in memory
there's now much smaller place for bugs, BUT THEY ARE STILL THERE wtf
This commit is contained in:
parent
d885e62502
commit
273caad781
@ -14,8 +14,7 @@ typedef struct node
|
|||||||
{
|
{
|
||||||
void *pointer;
|
void *pointer;
|
||||||
struct node *next;
|
struct node *next;
|
||||||
struct node *last;
|
} node_t; //SINGLE LINKED LIST
|
||||||
} node_t; //DOUBLY LINKED LIST
|
|
||||||
|
|
||||||
node_t *head = NULL;
|
node_t *head = NULL;
|
||||||
|
|
||||||
@ -23,65 +22,70 @@ void push_to_list(void *pointer)
|
|||||||
{
|
{
|
||||||
//CREATE NODE
|
//CREATE NODE
|
||||||
node_t *new_node = malloc(sizeof(node_t));
|
node_t *new_node = malloc(sizeof(node_t));
|
||||||
node_t *node_buffer = head;
|
node_t *buffer = head;
|
||||||
|
|
||||||
//ADD STUFF TO new_node
|
|
||||||
new_node -> pointer = pointer;
|
new_node -> pointer = pointer;
|
||||||
new_node -> next = NULL;
|
new_node -> next = NULL;
|
||||||
new_node -> last = NULL;
|
|
||||||
|
|
||||||
if (head == NULL) //FIRST NODE
|
if (head == NULL) //INIT LIST
|
||||||
{
|
{
|
||||||
head = new_node;
|
head = new_node;
|
||||||
} else //ADD AT THE END
|
|
||||||
{
|
|
||||||
while (node_buffer -> next != NULL) node_buffer = node_buffer -> next; //GO TO THE LAST NODE
|
|
||||||
|
|
||||||
//LINK AT THE END
|
|
||||||
node_buffer -> next = new_node;
|
|
||||||
new_node -> last = node_buffer;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void remove_node(node_t *node) //valgrind says this causes memory leaks ('still reachable'), but there is no chance like wtf
|
|
||||||
{
|
|
||||||
//REMOVE NODE
|
|
||||||
node_t *node_buffer = head;
|
|
||||||
|
|
||||||
while (node_buffer -> next != NULL) //GO TROUGH THE LIST
|
|
||||||
{
|
|
||||||
if (node_buffer == node) break; //FOUND
|
|
||||||
|
|
||||||
node_buffer = node_buffer -> next;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (node -> last != NULL)
|
|
||||||
{
|
|
||||||
node -> last -> next = node -> next;
|
|
||||||
} else
|
} else
|
||||||
{
|
{
|
||||||
head = node -> next;
|
while (buffer -> next != NULL) buffer = buffer -> next; //GET TO THE END OF LIST
|
||||||
|
|
||||||
|
buffer -> next = new_node; //LINK
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (node -> next != NULL)
|
void remove_node(node_t *node)
|
||||||
{
|
{
|
||||||
node -> next -> last = node -> last;
|
/*
|
||||||
} else
|
This whole function assumes the node is in the list, so if you use this without pushing it, bad things will happen :)
|
||||||
|
*/
|
||||||
|
|
||||||
|
node_t *buffer_1 = head;
|
||||||
|
node_t *buffer_2;
|
||||||
|
|
||||||
|
while (buffer_1 -> next != NULL) //GO TROUGH EVERY ELEMENT IN LIST
|
||||||
{
|
{
|
||||||
if (node -> last != NULL)
|
if (buffer_1 == node) break; //FOUND (IF THE WHILE GOES TROUGH THE WHOLE LIST WITHOUT THE break, I ASSUME THE LAST NODE IS THE CORRECT ONE)
|
||||||
{
|
|
||||||
node -> last -> next = NULL;
|
buffer_1 = buffer_1 -> next;
|
||||||
} else //LIST IS EMPTY NOW => DEALLOCATE
|
|
||||||
{
|
|
||||||
free(head);
|
|
||||||
head = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (buffer_1 == head) //node WAS THE FIRST NODE IN THE LIST
|
||||||
|
{
|
||||||
|
//UNLINK
|
||||||
|
head = buffer_1 -> next;
|
||||||
|
} else //wdyt
|
||||||
|
{
|
||||||
|
//GET THE NODE BEFORE node
|
||||||
|
buffer_2 = head;
|
||||||
|
|
||||||
|
while (buffer_2 -> next != buffer_1) buffer_2 = buffer_2 -> next;
|
||||||
|
|
||||||
|
//UNLINK
|
||||||
|
buffer_2 -> next = buffer_1 -> next;
|
||||||
}
|
}
|
||||||
|
|
||||||
//DEALLOCATION
|
//DEALLOCATION
|
||||||
free(node);
|
free(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
node_t *get_node(void *string)
|
||||||
|
{
|
||||||
|
node_t *buffer = head;
|
||||||
|
while (buffer -> next != NULL)
|
||||||
|
{
|
||||||
|
if (buffer -> pointer == string) return buffer;
|
||||||
|
|
||||||
|
buffer = buffer -> next;
|
||||||
|
}
|
||||||
|
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
//GLOBAL
|
//GLOBAL
|
||||||
void *why2_malloc(unsigned long size)
|
void *why2_malloc(unsigned long size)
|
||||||
{
|
{
|
||||||
@ -122,16 +126,12 @@ char *why2_strdup(char *string)
|
|||||||
void why2_free(void *pointer)
|
void why2_free(void *pointer)
|
||||||
{
|
{
|
||||||
//VARIABLES
|
//VARIABLES
|
||||||
node_t *node_buffer = head;
|
node_t *node = get_node(pointer);
|
||||||
|
|
||||||
//FIND pointer IN LINKED LIST
|
if (pointer == node)
|
||||||
while (node_buffer -> next != NULL)
|
|
||||||
{
|
{
|
||||||
if (node_buffer -> pointer == pointer) break; //FOUND
|
remove_node(node); //REMOVE FROM LIST IF FOUND
|
||||||
|
} //TODO: ELSE happens really often
|
||||||
|
|
||||||
node_buffer = node_buffer -> next;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pointer == node_buffer -> pointer) remove_node(node_buffer); //REMOVE FROM LIST IF FOUND
|
|
||||||
free(pointer);
|
free(pointer);
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user