Front Insertion of node is very easy in Circular Singly Linked List(CSLL). we have to apply some basic node arrangement.
- First we need one new node for inserting, So allocate the memory for new node.
- and then insert the new node at beginning of CSLL.
Working Process
We can implement front 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 working process ,we can also code it 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 HeadPointer Only*/
#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 insertNodeAtFrontInCSLL(int data) // Method of front Addition
{
//Memory Initialization for NewNode
struct CreateNode* NewNode = (struct CreateNode *)malloc(sizeof(struct CreateNode *));
struct CreateNode* Temp = NULL;
Temp = HeadPointer;
if (NewNode != NULL)
{
if (HeadPointer == NULL)
{
NewNode->data = data;
HeadPointer = NewNode;
NewNode->nextNode = HeadPointer;
printf("NewNode has been added...\n");
}
else
{
while (Temp->nextNode!= HeadPointer)
{
Temp = Temp->nextNode;
}
NewNode->data = data;
NewNode->nextNode = HeadPointer;
HeadPointer = NewNode;
Temp->nextNode = HeadPointer;
printf("NewNode has been added...\n");
}
}
else
{
printf("Memory is not Initialized for NewNode.");
}
}
int main()
{
int data, choice;
do
{
printf("Enter the data for insertion :");
std::cin >> data;
insertNodeAtFrontInCSLL(data);
printf("Press 0 for more insertion :");
std::cin >> choice;
} while (choice == 0);
struct CreateNode* Temp = HeadPointer;
printf("Node data after insertion process :\n");
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 <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
struct CreateNode *TailPointer = NULL; // Initialization of TailPointer
void insertNodeAtFrontInCSLL(int data) // Method of front Addition
{
//Memory Initialization for NewNode
struct CreateNode* NewNode = (struct CreateNode *)malloc(sizeof(struct CreateNode *));
if (NewNode != NULL)
{
if (HeadPointer == NULL || TailPointer == NULL)
{
NewNode->data = data;
NewNode->nextNode = NULL;
HeadPointer = NewNode;
TailPointer = HeadPointer;
printf("NewNode has been added...\n");
}
else
{
NewNode->data = data;
NewNode->nextNode = HeadPointer;
HeadPointer = NewNode;
TailPointer->nextNode = HeadPointer;
printf("NewNode has been added...\n");
}
}
else
{
printf("Memory is not Initialized for NewNode.");
}
}
int main()
{
int data, choice;
do
{
printf("Enter the data for insertion :");
std::cin >> data;
insertNodeAtFrontInCSLL(data);
printf("Press 0 for more insertion :");
std::cin >> choice;
} while (choice == 0);
printf("Node data after insertion process :\n");
struct CreateNode *Temp = HeadPointer;
while (Temp != TailPointer)
{
printf("%d ", Temp->data);
Temp = Temp->nextNode;
}
printf("%d ", Temp->data);
_getch();
return 0;
}
Time & Space Complexity
- When we use HeaderPointer Only then Time Complexity will be O(n).
- When we use HeaderPointer and also TailPointer then Time Complexity will be O(1).
- Space Complexity will be O(1).