Reputation: 2473
I am working on a generic form of a double linked list, so I have :
list structure :
int size;
struct elem *head;
struct elem *current;
struct elem *tail;
elem structure :
void *data;
struct elem *next;
struct elem *prev;
I am having a hard time implementing a function allowing me to push a new element before the current one. Here is what I came up with for the moment :
t_list *my_list_add_before(t_list *list, void *data)
{
struct elem *elem = malloc(sizeof(*elem));
elem->data = data;
if(list->size == 0)
{
list->head = elem;
list->current = elem;
list->tail = elem;
elem->prev = NULL;
elem->next = NULL;
}
else
{
elem->next = list->current;
elem->prev = list->current->prev;
elem->prev->next = elem;
list->current->prev = elem;
}
list->size = list->size + 1;
return (list);
}
But my list->head->next
seem to point to NULL
. What is the issue?
Upvotes: 0
Views: 74
Reputation: 1046
You should modify list->current->prev->next
before list->current->prev
, currently you effectively do elem->next = elem
.
Upvotes: 2