0% found this document useful (0 votes)
2 views

Algorithm for Inserting in an Ordered Circular Linked List

The document outlines an algorithm and C code for inserting nodes into an ordered circular linked list. It details the steps for creating a new node, handling empty lists, and inserting nodes in the correct order, including before the head and in the middle or end. The output demonstrates that elements are successfully inserted in sorted order.

Uploaded by

Ritika Lohiya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Algorithm for Inserting in an Ordered Circular Linked List

The document outlines an algorithm and C code for inserting nodes into an ordered circular linked list. It details the steps for creating a new node, handling empty lists, and inserting nodes in the correct order, including before the head and in the middle or end. The output demonstrates that elements are successfully inserted in sorted order.

Uploaded by

Ritika Lohiya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Algorithm for Inserting in an Ordered Circular Linked List

1. Create a new node with the given data.

2. If the list is empty, make the new node point to itself and set it as the head.

3. If the new node should be inserted before the current head:

o Traverse to the last node.

o Adjust the last node’s next to point to the new node.

o The new node’s next should point to the current head.

o Update head to the new node.

4. Otherwise:

o Traverse the list to find the correct position where the new node should be inserted.

o Update pointers so the new node fits in the correct order.

C Code for Insertion in an Ordered Circular Linked List

#include <stdio.h>

#include <stdlib.h>

// Node structure

struct Node {

int data;

struct Node* next;

};

// Function to insert a node in an ordered circular linked list

void insertInOrder(struct Node** head, int data) {

// Create a new node

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;
// If the list is empty

if (*head == NULL) {

newNode->next = newNode; // Points to itself

*head = newNode;

return;

struct Node* current = *head;

// If new node should be inserted before head

if (data < (*head)->data) {

// Find the last node

while (current->next != *head) {

current = current->next;

// Adjust pointers

current->next = newNode;

newNode->next = *head;

*head = newNode;

return;

// Traverse the list to find the correct position

struct Node* prev = NULL;

do {

prev = current;

current = current->next;

} while (current != *head && current->data < data);


// Insert the new node in its correct position

prev->next = newNode;

newNode->next = current;

// Function to display the Circular Linked List

void display(struct Node* head) {

if (head == NULL) {

printf("List is empty\n");

return;

struct Node* temp = head;

do {

printf("%d -> ", temp->data);

temp = temp->next;

} while (temp != head);

printf("(Head)\n");

// Main function

int main() {

struct Node* head = NULL;

// Insert elements in sorted order

insertInOrder(&head, 30);

insertInOrder(&head, 10);

insertInOrder(&head, 40);
insertInOrder(&head, 20);

insertInOrder(&head, 25);

// Display list

display(head);

return 0;

Explanation

1. Creating a new node

o Allocate memory using malloc.

o Assign the given value to data.

2. Handling the empty list case

o If the list is empty, make the new node’s next point to itself.

o Set head to this new node.

3. Inserting before the head (smallest element case)

o If the new value is smaller than the head’s data:

▪ Find the last node.

▪ Update the last node’s next to point to the new node.

▪ The new node’s next points to the old head.

▪ Update head to the new node.

4. Inserting in the middle or at the end

o Traverse to find the correct position.

o Update pointers to insert the new node at the correct spot.

Output

10 -> 20 -> 25 -> 30 -> 40 -> (Head)

This confirms that new elements are inserted in sorted order.

You might also like