Doubly Linked List deletion

delete node from beginning of a doubly linked list

deleting node in doubly linked list

import 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 1

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

Doubly Linked List deletion 2

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

Doubly Linked List deletion 3

6.delete temp node using free keyword in c

 delete node from beginning of a doubly linked list

7.first node will be deleted

Doubly Linked List deletion 4

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

import 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 5

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 6

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

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

6.delete temp node using free keyword in c

 delete node from ending of a doubly linked list

7.last node will be deleted

Doubly Linked List deletion 8

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;
	}
}
Default image
Vishal Devxo
Vishal Devxo is a DevOps engineer and a Backend developer, he spends all his time for creating good tutorials with better visuals and blogging, developed some projects based on Python-Django, some hacking modules and scripts in python