Sunday, 23 July 2017

Circular Singly Linked List | Insertion Algorithm | Data Structures


A circular linked list is a simple linked list in which last node contain the address of first node. It has no beginning and no end means we can begin at any node and traverse the list in any direction, forward or backward, until we reach the same node where we started.

A single linked list can be made a circular linked list by simply storing address of the very first node in the next field of the last node.

linkedlist_image_missing

Creating a Circular Single Linked List with 'N' number of Nodes

Step 1: Create a new node using createnode()

NEW_NODE = createnode();

circular linked list create

Step 2: If the linked list is empthy (START == NULL)

START = NEW_NODE

Step 3: If the linked list is not empthy

PTR = START
while (PTR -> NEXT != NULL)
{
PTR = PTR -> NEXT
}
PTR -> NEXT = NEW_NODE

Step 4 : Repeat Steps 1,2,3 'N' times.

Step 5 : NEW_NODE ->NEXT = START

Step 6 : EXIT

Circular Singly Linked List - Inserting a New Node

Now we will see how a new node is added into an already existing circular linked list. It can be performed in two ways. They are as follows...

1. Inserting at Beginning of the list

2. Inserting at End of the list

Rest of the cases are similar to Singly Linked List insert operation.

Inserting a new node at the Beginning of a Circular Linked List

Step 1: Create the NEW_NODE using createnode()

NEW_NODE = createnode();

circular linked list insert

Step 2: If the linked list is empthy (START == NULL)

START = NEW_NODE
NEW_NODE -> NEXT = START

Step 3: If the linked list is not empthy

PTR = START
while(PTR -> NEXT != START)
{

PTR = PTR -> NEXT

}
PTR -> NEXT = NEW_NODE
NEW_NODE -> NEXT =START
START = NEWNODE

Step 4 : EXIT

Inserting a new node at the End of a Circular Linked List

Step 1: Create the NEW_NODE using createnode()

NEW_NODE = createnode();

cdelete

Step 2: If the linked list is empthy (START == NULL)

START = NEW_NODE
NEW_NODE -> NEXT = START

Step 3: If the linked list is not empthy

PTR = START
while(PTR -> NEXT!=START)
{
PTR = PTR -> NEXT
}
PTR -> NEXT = NEW_NODE
NEW_NODE -> NEXT =START

Step 4 : EXIT


Share this

3 Responses to "Circular Singly Linked List | Insertion Algorithm | Data Structures"

  1. Awesome work men amazing����

    ReplyDelete
  2. Amazing found every topic i needed to study👌 keepup the good work men

    ReplyDelete