0% found this document useful (0 votes)
27 views5 pages

Circular Singly Linked List

Uploaded by

Sinthu K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views5 pages

Circular Singly Linked List

Uploaded by

Sinthu K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Circular Singly Linked List

What is Circular Linked List?


In single linked list, every node points to its next node in the sequence and the last node points NULL. But in circular linked
list, every node points to its next node in the sequence but the last node points to the head node in the list.
Circular linked list is a sequence of elements in which every element has link to its next element in the sequence and the
last element has a link to the first element in the sequence.
That means circular linked list is similar to the single linked list except that the last node points to the head node in the list
Example

Operations
In a circular linked list, we perform the following operations...
1. Insertion
2. Deletion
3. Display
Before we implement actual operations, first we need to setup empty list. First perform the following steps before
implementing actual operations.
1. Include all the head files which are used in the program.
2. Declare all the user defined functions.
3. Define a Node structure with two members data and next
4. Define a Node pointer 'head' and set it to NULL.
5. Implement the main method by displaying operations menu and make suitable function calls in the main method to
perform user selected operation.

Insertion
In a circular linked list, the insertion operation can be performed in three ways. They are as follows...
 Inserting At Beginning of the list
 Inserting At End of the list
 Inserting At Specific location in the list

Inserting at Beginning of the list


Steps to insert a new node at beginning of the circular linked list...
1. Step 1: Create a newNode with given value.
2. Step 2: Check whether list is Empty (head == NULL)
3. Step 3: If it is Empty then, set head = newNode and newNode.next = head.
4. Step 4: If it is Not Empty then, define a Node pointer ‘tmp’ and initialize with 'head'.
5. Step 5: Keep moving the ‘tmp’ to its next node until it reaches to the last node (until ‘tmp.next == head').
6. Step 6: Set 'newNode.next =head', 'head = newNode' and 'tmp.next = head'.

1
Inserting at End of the list
Steps to insert a new node at end of the circular linked list...
1. Step 1: Create a newNode with given value.
2. Step 2: Check whether list is Empty (head == NULL).
3. Step 3: If it is Empty then, set head = newNode and newNode . next = head.
4. Step 4: If it is Not Empty then, define a node pointer tmp and initialize with head.
5. Step 5: Keep moving the tmp to its next node until it reaches to the last node in the list (until tmp. next == head).
6. Step 6: Set tmp. next = newNode and newNode . next = head.

Inserting at Specific location in the list (After a Node)


Steps to insert a new node after a node in the circular linked list...
1. Step 1: Create a newNode with given value.
2. Step 2: Check whether list is Empty (head == NULL)
3. Step 3: If it is Empty then, set head = newNode and newNode.next = head.
4. Step 4: If it is Not Empty then, define a node pointer tmp and initialize with head.
5. Step 5: Keep moving the tmp to its next node until it reaches to the node after which we want to insert the
newNode (until tmp. data is equal to e, here e is the node value after which we want to insert the newNode).

2
6. Step 6: Every time check whether tmp is reached to the last node or not. If it is reached to last node then display
'Given node is not found in the list!!! Insertion not possible!!!' and terminate the function. Otherwise move the
tmp to next node.
7. Step 7: If tmp is reached to the exact node after which we want to insert the newNode then check whether it is last
node (currentNode. next == head).
8. Step 8: If tmp is last node then set tmp. next = newNode and newNode. next = head.
9. Step 8: If tmp is not last node then set newNode. next = tmp. next and tmp. next = newNode.

Deletion
In a circular linked list, the deletion operation can be performed in three ways those are as follows...
1. Deleting from Beginning of the list
2. Deleting from End of the list
3. Deleting a Specific Node

Deleting from Beginning of the list


Steps to delete a node from beginning of the circular linked list...
1. Step 1: Check whether list is Empty (head == NULL)
2. Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.
3. Step 3: If it is Not Empty then, define two Node pointers 'tmp' and initialize 'tmp' with head.
4. Step 4: Check whether list is having only one node (tmp . next == head)
5. Step 5: If it is TRUE then set head = NULL and delete tmp (Setting Empty list conditions)
6. Step 6: If it is FALSE move the tmp until it reaches to the last node. (until tmp . next == head )
7. Step 7: Then set head = head. next, tmp. next = head.

3
Deleting from End of the list
Steps to delete a node from end of the circular linked list...
1. Step 1: Check whether list is Empty (head == NULL)
2. Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.
3. Step 3: If it is Not Empty then, define two Node pointers 'tmp1' and 'tmp2' and initialize 'tmp1' with head.
4. Step 4: Check whether list has only one Node (tmp1 . next == head)
5. Step 5: If it is TRUE. Then, set head = NULL and delete tmp1. And terminate from the function. (Setting Empty list
condition)
6. Step 6: If it is FALSE. Then, set 'tmp2 = tmp1 ' and move tmp1 to its next node. Repeat the same until tmp1 reaches
to the last node in the list. (until tmp1 . next == head)
7. Step 7: Set tmp2.next = head and delete tmp1.

Deleting a Specific Node from the list


Steps to delete a specific node from the circular linked list...
1. Step 1: Check whether list is Empty (head == NULL)
2. Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.
3. Step 3: If it is Not Empty then, define two Node pointers 'tmp1' and 'tmp2' and initialize 'tmp1' with head.
4. Step 4: Keep moving the tmp1 until it reaches to the exact node to be deleted or to the last node. And every
time set 'tmp2 = tmp1' before moving the 'tmp1' to its next node.

4
5. Step 5: If it is reached to the last node then display 'Given node not found in the list! Deletion not possible!!!'.
And terminate the function.
6. Step 6: If it is reached to the exact node which we want to delete, then check whether list is having only one
node (tmp1 . next == head)
7. Step 7: If list has only one node and that is the node to be deleted then set head = NULL and delete tmp1.
8. Step 8: If list contains multiple nodes then check whether tmp1 is the head node in the list (tmp1 == head).
9. Step 9: If tmp1 is the head node then set tmp2 = head and keep moving tmp2 to its next node until tmp2
reaches to the last node. Then set head = head.next, tmp2.next = head and delete tmp1.
10. Step 10: If tmp1 is not head node then check whether it is last node in the list (tmp1.next == head).
11. Step 11: If tmp1 is last node then set tmp2.next = head and delete tmp1.
12. Step 12: If tmp1 is not head node and not last node then set tmp2.next = tmp1.next and delete tmp1.

Displaying a circular Linked List


Steps to display the elements of a circular linked list...
1. Step 1: Check whether list is Empty (head == NULL)
2. Step 2: If it is Empty, then display 'List is Empty!!!' and terminate the function.
3. Step 3: If it is Not Empty then, define a Node pointer ' tmp' and initialize with head.
4. Step 4: Keep displaying tmp.data an arrow (→) until tmp reaches to the last node
5. Step 5: Finally display tmp.data with arrow (→) pointing to head.data.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy