End Insertion of node is something complex 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 End 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 two Working process We can also implement 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 HeaderPointer*/
#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 insertNodeAtEndInCSLL(int data) // Method of End Addition
{
//Memory Initialization for NewNode
struct CreateNode* NewNode = (struct CreateNode *)malloc(sizeof(struct CreateNode *));
struct CreateNode *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;
Temp->nextNode = NewNode;
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;
insertNodeAtEndInCSLL(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->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 HeaderPointer 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 insertNodeAtEndInCSLL(int data) // Method of End 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;
HeadPointer = NewNode;
TailPointer = HeadPointer;
NewNode->nextNode = HeadPointer;
printf("NewNode has been added...\n");
}
else
{
NewNode->data = data;
NewNode->nextNode = HeadPointer;
TailPointer->nextNode = NewNode;
TailPointer = NewNode;
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;
insertNodeAtEndInCSLL(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).