Deletion at Beginning(Front) in CSLL

There is two ways to do this task,we can use only HeadPointer, or we can use HeadPointer as well as TailPointer .Refer below Working Process :

Working Process

We can implement front delete operation in CSLL by two ways :
1- Using Only HeadPointer.
2- Using HeadPointer and TailPointer.

  • 1- Using Only HeadPointer :

  • 2- Using HeadPointer and TailPointer :

Code Implementation

As per the Two Working process We can implement front delete operation in CSLL by two ways :
1- Code Implementation for Using Only HeadPointer.
2- Code Implementation for Using HeadPointer and TailPointer.

  • 1- Code Implementation for Using Only HeadPointer :
                                   /*Using Only HeadPointer*/
#include <stdio.h>
#include<math.h>
#include<stdlib.h>
#include<iostream>
#include<conio.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 deleteNodeAtFrontInCSLL()            // Method of front Deletion
{
	struct CreateNode *Temp = HeadPointer;
	while (Temp->nextNode!=HeadPointer)
	{
		Temp = Temp->nextNode;
	}
	printf("%d is deleted from the front\n ", HeadPointer->data);
	HeadPointer = HeadPointer->nextNode;
	Temp->nextNode = HeadPointer;
}
int main()
{
	int choice = 0;
	//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 data before deletion process :\n");
	struct CreateNode* Temp = HeadPointer;
	while (Temp->nextNode != HeadPointer)
	{
		printf("%d ", Temp->data);
		Temp = Temp->nextNode;
	}
	printf("%d ", Temp->data);
	while (choice == 0)
	{
		printf("\nPress 0 for front deletion :");
		std::cin >> choice;
		if (choice == 0)
		{
			deleteNodeAtFrontInCSLL();
		}
		else
		{
			choice = 1; // dont delete the front node
		}
	}

	printf("Node 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;
}
  • 2- Code Implementation for Using HeadPointer and TailPointer :
                              /*Using HeadPointer and TailPointer*/
#include <stdio.h>
#include<math.h>
#include<stdlib.h>
#include<iostream>
#include<conio.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
struct CreateNode *TailPointer = NULL;    // Initialization of TailPointer
void deleteNodeAtFrontInCSLL()   // Method of front Deletion
{
	printf("%d is deleted from the front\n ", HeadPointer->data);
	HeadPointer = HeadPointer->nextNode;
	TailPointer->nextNode = HeadPointer;
}
int main()
{
	int choice = 0;
	//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
	TailPointer = Node5;  // Tailpointer pointing to the Node5

	printf("Node data before deletion process :\n");
	struct CreateNode* Temp = HeadPointer;
	while (Temp->nextNode != HeadPointer)
	{
		printf("%d ", Temp->data);
		Temp = Temp->nextNode;
	}
	printf("%d ", Temp->data);
	while (choice == 0)
	{
		printf("\nPress 0 for front deletion :");
		std::cin >> choice;
		if (choice == 0)
		{
			deleteNodeAtFrontInCSLL();
		}
		else
		{
			choice = 1; // dont delete the front node
		}
	}

	printf("Node 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

  • When we use HeaderPointer and also TailPointer then Time Complexity will be O(1).
  • When we use only HeaderPointer ,then 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 *