0% found this document useful (0 votes)
215 views30 pages

Circular Single Linked List

The document discusses circular linked lists. In a circular linked list, the last node points to the first node, forming a circular chain. Basic operations on circular linked lists include insertion, deletion, traversal, and search. Insertion and deletion at the beginning and end of the list are demonstrated along with algorithms. Traversal involves accessing each node in order using a pointer that moves from node to node until reaching the header node again.

Uploaded by

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

Circular Single Linked List

The document discusses circular linked lists. In a circular linked list, the last node points to the first node, forming a circular chain. Basic operations on circular linked lists include insertion, deletion, traversal, and search. Insertion and deletion at the beginning and end of the list are demonstrated along with algorithms. Traversal involves accessing each node in order using a pointer that moves from node to node until reaching the header node again.

Uploaded by

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

Data Structures

UNIT-2

Circular Single Linked List


(Or)
Circular Linked List
Types of Linked Lists

1. Singly Linked Lists (or) Single Linked list


2. Circular Linked Lists (or) Circular Single Linked List
3. Doubly Linked Lists (or) Double Linked List
1. Circular Doubly Linked Lists (or) Circular
Double Linked list
Circular Linked List
• In a circular linked list, the last node contains a pointer to the first
node of the list.
• Structure of Circular Linked List

Header

• Types of Circular linked List:


 circular singly linked list
 circular doubly linked list.
Memory representation
of a circular linked list
Header
Basic Operations on Circular Linked List
1. Insertion
2. Deletion
3. Traverse
4. Search

Other operations
5. Destroy
6. IsEmpty
7. reverse a linked list
8. Copying
9. Merging (combine two linked lists)
Insertion on Circular linked list
• There are various positions where can be
node inserted.

Case 1: The new node is inserted at the beginning


of the circular linked list.
Case 2: The new node is inserted at the end of
the circular linked list.

Rest of the cases of INSERTION are same as that given


for singly linked lists.
Inserting a Node at the Beginning of a
Circular Linked List
• Suppose we want to add a new node with data 9 as a first node
of the list. New node
9
Header Given Circular Linked List

Make a temporary ptr to point to the first node of the List


ptr
Header
Move ptr to point to last node of list

Header ptr

Header ptr

Repeat moving of ptr until we reach last node of the List


After few steps, we reach the last node of the List
ptr
Header

Add the new node in between PTR and Header


ptr
Header
1

2
Make header to point to the new node.

Header

Inserting a new node at the beginning of a circular


linked list is completed successfully
Algorithm to insert a new node at the beginning of Circular Linked
List
Step 1: Allocate memory for new node
Step 2: SET NewNode  DATA = Value
Step 3: SET PTR = Header
Step 4: Repeat Step 5 while PTR 
Step 5: NEXT != Header
PTR = PTR  NEXT
Step 6: [END OF LOOP]
Step 7: SET NewNode  NEXT =
Step 8 : Header SET PTR  NEXT =
Step 9: NewNode SET Header =
NewNode
Time Complexity : O(n)
EXIT
Inserting a Node at the End of a Circular
Linked List
• Suppose we want to add a new node with data 9 as a Last node
of the list. New node
9
Header Given Circular Linked List

Make a temporary ptr to point to the first node of the List


ptr
Header
Move ptr to point to last node of list

Header ptr

Header ptr

Repeat moving of ptr until we reach last node of the List


After few steps, we reach the last node of the List
ptr
Header

Add the new node after the node pointed by PTR.


ptr
Header
2

1
Insertion is completed
Algorithm to insert a new node at the End of Circular Linked List

Step 1: Allocate memory for new node


Step 2: SET NewNode  DATA = Value
Step 3: SET NewNode  NEXT =
Step 4: Header SET PTR = Header
Step 5: Repeat Step 6 while PTR  NEXT != Header
Step 6: PTR = PTR  NEXT
[END OF LOOP]
Step 7: SET PTR  NEXT = NewNode
Step 8 : EXIT

Time Complexity for Insertion on Circular Linked List: O(n)


Deleting a Node from a Circular
Linked List
• We will see two cases of deletion on circular linked list.

Case 1: The first node is deleted.


Case 2: The last node is deleted.

Rest of the cases of deletion are same as that


given for singly linked lists.
Deleting the First Node from a Circular Linked List
• Suppose we want to delete data value 1 from the list.
Header Given Circular Linked List

Make a temporary ptr to point to the first node of the List

ptr
Header
Move ptr to point to last node of list

Header ptr

Header ptr

Repeat moving of ptr until we reach last node of the List


After few steps, we reach the last node of the List
ptr
Header

The NEXT part of PTR is made to point to the second node of the list

ptr
Header

1
Destroy first node
ptr
Header

Make the header to point to new first node

Header

Deletion is completed
Algorithm to delete a node at the front of Circular Linked List
Step 1: IF Header = NULL
Print “List is empty”
Go to Step 8
[END OF IF]
Step 2: SET PTR =
Step 3: Header
Step 4: Repeat Step 4 while PTR  NEXT != Header
SET PTR = PTR  NEXT
Step 5: [END OF LOOP]
Step 6: SET PTR NEXT = Header  NEXT
Step 7: Free Header
Step 8: SET Header = PTR  NEXT
EXIT
Time Complexity : O(n)
Deleting the Last Node from a Circular Linked List
• Suppose we want to delete data value 5 from the list.
Header Given Circular Linked List

Make two temporary pointers ptr and preptrto point to the first
node of the List
ptr
Header
Preptr
Preptr Move PTR to point to the last node of the list and PREPTR
always points to the node preceding PTR.

Header ptr

Header Preptr ptr

Repeat moving of ptr,preptr until we reach last node of the List


After few steps, we reach the last node of the List
Preptr
ptr
Header

Make the Preptr's next part to store address of first node

Preptr ptr
Header
Destroy the node pointed by ptr

Header ptr

Final List after deletion


Header

Deletion is completed
Algorithm to delete a node at the last of Circular Linked List
Step 1: IF Header = NULL
Print “List is empty”
Go to Step 8
[END OF IF]
Step 2: SET PTR =
Header
Step 3: SET Preptr =
Step 4: Header
Step 5: Repeat Step
4 and 5 while PTR
 NEXT != Header
Step 6: SET
Step 7: Preptr =
Step 8: EXIT PTR Time Complexity : O(n)
SET PTR = PTR  NEXT
Traversing a Circular Linked List
• Traversing a linked list means accessing the nodes of the
list in order to perform some processing on them.
• Example: Displaying the contents of Linked list, Counting
number of nodes in the linked list, etc..
Algorithm for Displaying Circular Linked List:
Step -1: ptr = header
Step-2: Repeat Steps-3 and 4 while ptr
Step-3:
Next!= print ptr  data
Step-4: Set ptr = ptr next
Header [END OF LOOP]
Step- 5: print ptr  data #prints last
Step 5: Stop node
Time Complexity: O(n)
Displaying Circular Linked list
header
Given Linked List

10 1500 20 2050 30 2100 40


1000 1500 2050 2100

Start Traversing

header ptr

10 1500 20 2050 30 2100 40


1000 1500 2050 2100

Print 10 and move the ptr to next node


header ptr

10 1500 20 2050 30 2100 40


1000 1500 2050 2100

Print 20 and move the ptr to next node

header ptr

10 1500 20 2050 30 2100 40


1000 1500 2050 2100

Print 30 and move the ptr to next node


header ptr

10 1500 20 2050 30 2100 40


1000 1500 2050 2100

Print 40 and stop traversing (Since Next of ptr is header)

You might also like