fix: Doubly Linked List algorithm bug

This commit is contained in:
Samrat De 2020-05-21 17:32:51 +05:30
parent d963df6fa6
commit f55c76d0fb

View File

@ -38,12 +38,36 @@ void insert(int x)
void remove(int x) void remove(int x)
{ {
node *t = start; node *t = start;
while (t->val != x) while (t != NULL && t->val != x)
{ {
t = t->next; t = t-> next;
} }
if (t == NULL)
{
return;
}
// if first element is removed
if (t->prev == NULL)
{
if (t->next == NULL)
{
start = NULL;
}
else
{
start = t->next;
start->prev = NULL;
}
}
else if (t->next == NULL)
{
t->prev->next = NULL;
}
else
{
t->prev->next = t->next; t->prev->next = t->next;
t->next->prev = t->prev; t->next->prev = t->prev;
}
delete t; delete t;
} }