C++ program to insert in a sorted circular linked list
February 21, 2023
How to insert in a sorted circular linked list in C++?
C++ program to insert in a sorted circular linked list means to insert the data in a circular linked list in a way that its logical order does not get disturbed. To do this first we need to ensure that the list we are working upon is already sorted else we will have to sort the given list before performing the operation.
Steps write a C++ program to insert in a sorted circular linked list
Write set of codes to construct circular linked list.
Create a function to build the circular linked list.
Make a function to print the circular linked list data.
Now create a function for insertion in a sorted list.
If current node pointer is equal to head insert new node here.
Else if data of current is greater then data of new node then insert new node before current node.
Else insert new node after current node.
Print the new list with newly inserted node.
structNode{int num;structNode*next;};
Algorithm to insert in a sorted circular linked list in C++
#include<iostream> //haeder files
using namespace std;
// create node
struct Node
{
int data;
struct Node *next;
} *head;
void insertStart (struct Node **head, int data) //function to create linked list
{
struct Node *newNode = (struct Node *) malloc (sizeof (struct Node));
newNode->data = data;
// if its the first node being entered
if (*head == NULL)
{
*head = newNode; // assigning itself as head
(*head)->next = *head; // assigning itself as next node
cout << newNode->data << " Inserted\n";
return;
}
// if CLL already as >=1 node
struct Node *curr = *head;
// traverse till last node in CLL
while (curr->next != *head)
{
curr = curr->next;
}
curr->next = newNode; // last node's next as this new node
newNode->next = *head; // new node's next as current head node
*head = newNode; // changing head node to this new node
cout << newNode->data << " Inserted\n";
// previous head node becomes 2nd node
}
// this function for insert the new node in linked list
void sortedInsert (struct Node **head, int n)
{
struct Node *nextNode = (struct Node *) malloc (sizeof (struct Node));
nextNode->data = n;
struct Node *thisNode = (struct Node *) malloc (sizeof (struct Node));
thisNode = *head;
// Case 1 of the above algo
if (thisNode == NULL)
{
nextNode->next = nextNode;
*head = nextNode;
}
// Case 2 of the above algo
else if (thisNode->data >= nextNode->data)
{
/* If value is smaller than head's value then
we need to change next of last node */
while (thisNode->next != *head)
thisNode = thisNode->next;
thisNode->next = nextNode;
nextNode->next = *head;
*head = nextNode;
}
// Case 3 of the above algo
else
{
/* Locate the node before the point of insertion */
while (thisNode->next != *head && thisNode->next->data < nextNode->data)
thisNode = thisNode->next;
nextNode->next = thisNode->next;
thisNode->next = nextNode;
}
}
//this Function to print nodes in a given linked list
void display (struct Node *head)
{
cout << "\nCircular Linked List : " << endl;
// if circular linked list is empty currently
if (head == NULL)
return;
struct Node *temp = head;
// since we need to take care of circular nature of linked list
do
{
cout << temp->data << " ";
temp = temp->next;
}
while (temp != head);
cout << endl;
}
// main method start
int main ()
{
struct Node *head = NULL;
insertStart (&head, 50);
insertStart (&head, 40);
insertStart (&head, 30);
insertStart (&head, 20);
insertStart (&head, 10);
cout << "Before insertion ";
display (head);
sortedInsert (&head, 33);
cout << "After insertion ";
display (head);
return 0;
}
Output:
50 Inserted
40 Inserted
30 Inserted
20 Inserted
10 Inserted
Before insertion
Circular Linked List :
10 20 30 40 50
After insertion
Circular Linked List :
10 20 30 33 40 50
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment