Deletion of node at specific location in DLL

Deletion of Node at specific location in Doubly Linked List(DLL) is something complex process ,we have to follow the below Steps :

  • Take one Temp variable and assign it by HeadPointer
  • Traverse the List till location by Temp variable.
  • When reached at location node ,the we have to perform deletion operation.
  • Refer the below Work Process :

Work Process

Code Implementation

#include <stdio.h>
#include<iostream>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
struct CreateNode   // Node Structure Initialization
{
	int  data;          // for data element
	struct CreateNode* nextNode;   //for storing the address of next element
	struct CreateNode* prevNode;  // for storing the address of previuos element
};
struct CreateNode *HeadPointer = NULL;    // Initialization of HeadPointer

void DeleteNodeAtLocationDLL(int Position)   // Method of Random Deletion
{
	int flag = 1;
	struct CreateNode* Temp = HeadPointer; //Temp Node pointing to HeaderPoints
	while (flag < Position)
	{
		Temp = Temp->nextNode;
		flag++;
	}
	Temp->prevNode->nextNode = Temp->nextNode;
	Temp->nextNode->prevNode = Temp->prevNode;
	std::cout << "Node of Possition  " << Position << " has been deleted\n";

}
int main()
{
	int Position;
	//Create 5 node statically and Initialize manually
	struct CreateNode* Node1 = (struct CreateNode *)malloc(sizeof(struct CreateNode *));
	struct CreateNode* Node2 = (struct CreateNode *)malloc(sizeof(struct CreateNode *));
	struct CreateNode* Node3 = (struct CreateNode *)malloc(sizeof(struct CreateNode *));
	struct CreateNode* Node4 = (struct CreateNode *)malloc(sizeof(struct CreateNode *));
	struct CreateNode* Node5 = (struct CreateNode *)malloc(sizeof(struct CreateNode *));
	//Data Initialization
	Node1->data = 90;
	Node2->data = 100;
	Node3->data = 200;
	Node4->data = 300;
	Node5->data = 400;
	//Address Initialization
	Node1->prevNode = NULL;
	Node1->nextNode = Node2;

	Node2->prevNode = Node1;
	Node2->nextNode = Node3;

	Node3->prevNode = Node2;
	Node3->nextNode = Node4;

	Node4->prevNode = Node3;
	Node4->nextNode = Node5;

	Node5->prevNode = Node4;
	Node5->nextNode = NULL;

	HeadPointer = Node1;  // HeadPointer pointing to the Node1

	printf("Node Before Deletion : ");
	CreateNode* Temp = HeadPointer;
	while (Temp->nextNode != NULL)
	{
		printf("%d ", Temp->data);
		Temp = Temp->nextNode;
	}
	printf("%d ", Temp->data);
	printf("\nEnter the Position for Deletion :");
	std::cin >> Position;
	DeleteNodeAtLocationDLL(Position);
	printf("\nNode data after Deletion process :\n");
	Temp = HeadPointer;
	while (Temp->nextNode != NULL)
	{
		printf("%d ", Temp->data);
		Temp = Temp->nextNode;
	}
	printf("%d ", Temp->data);
	_getch();
	return 0;
}

Time & Space Complexity

  • Worst Case Time Complexity will be O(n).
  • Best Case Time Complexity will be O(1).
  • Average Case Time Complexity will be O(n).
  • Space Complexity will be O(1).

Leave a Reply

Your email address will not be published. Required fields are marked *