Doubly Linked List deletion

delete node from beginning of a doubly linked list

Doubly Linked List deletion

use code from the create and display nodes and getnode function to assign data click here

  1. create a function deletebeg( )
  2. create a *temp pointer
  3. initialize head to the temp pointer (temp=head)
Doubly Linked List deletion

4.move head pointer to the next node (head=head->next)

Doubly Linked List deletion

5.change previous address field of head node to NULL (head ->prev = NULL)

Doubly Linked List deletion

6.delete temp node using free keyword in c

Doubly Linked List deletion

7.first node will be deleted

Doubly Linked List deletion

code to delete node from beginning of a doubly linked list

void deletebeg()
{
node *temp;
if(head == NULL)
{
printf("\n Empty list");
getch();
return ;
}
else
{
temp = head;
head = head -> next;
head -> prev = NULL;
free(temp);
}
}

delete node from ending of a doubly linked list

use code from the create and display nodes and getnode function to assign data click here

  1. create a function deleteend( )
  2. create a *temp pointer
  3. initialize head to the temp pointer (temp=head)
Doubly Linked List deletion

4.move temp pointer to the last node by changing the address in the temp pointer (temp=temp->next)until the condition (temp -> next != NULL)

Doubly Linked List deletion

5.change last-second node next value to NULL, by the below code

temp -> prev -> next = NULL
Doubly Linked List deletion

6.delete temp node using free keyword in c

Doubly Linked List deletion

7.last node will be deleted

Doubly Linked List deletion

code to delete node from ending of a doubly linked list

void deleteend()
{
node *temp;
if(head == NULL)
{
printf("\n Empty list");
getch();
return ;
}
else
{
temp = head;
while(temp -> next != NULL)
temp = temp -> next;
temp -> prev -> next = NULL;
free(temp);
temp = NULL;
}
}
Last updated on by vishal devxo