Deletion at Specific Location in CSLL

Location base deletion is similar to the Insertion of node at specific location.we will be follow the below procedure :

  • First we have to declare an Temp Variable and also one flag.
  • flag will go towards the position value.
  • when we will reached at required position node,then we have to arrange the node. and delete the node of specific position.

Working 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 *HeadPointer = NULL;    // Initialization of HeadPointer

void DeleteNodeAtLocationInCSLL(int Position)   // Method of Random deletion
{
	int flag = 1;
	struct CreateNode* Temp = HeadPointer; //Temp Node
	while (flag < Position-1 )
	{
		Temp = Temp->nextNode;
		flag++;
	}
	Temp->nextNode = Temp->nextNode->nextNode;
	std::cout << "\nNode-" << Position << " has been Deleted ";

}
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 *));
	Node1->data = 90;
	Node2->data = 100;
	Node3->data = 200;
	Node4->data = 300;
	Node5->data = 400;

	Node1->nextNode = Node2;
	Node2->nextNode = Node3;
	Node3->nextNode = Node4;
	Node4->nextNode = Node5;
	Node5->nextNode = Node1;

	HeadPointer = Node1;  // HeadPointer pointing to the Node1
	printf("Node Before Deletion : ");
	struct CreateNode* Temp = HeadPointer;
	while (Temp->nextNode != HeadPointer)
	{
		std::cout << Temp->data << " ";
		Temp = Temp->nextNode;
	}
	std::cout << Temp->data << " ";
	printf("\nEnter Position of Node for Deletion :");
	std::cin >> Position;
	DeleteNodeAtLocationInCSLL(Position);
	printf("\nNode data after Deletion process :\n");
	Temp = HeadPointer;
	while (Temp->nextNode != HeadPointer)
	{
		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 *