For working professionals
For fresh graduates
More
Data Structure Tutorial: Every…
1. Data Structure
2. Types of Linked Lists
3. Array vs Linked Lists in Data Structure
4. Stack vs. Queue Explained
5. Singly Linked List
6. Circular doubly linked list
7. Circular Linked List
Now Reading
8. Stack Implementation Using Array
9. Circular Queue in Data Structure
10. Dequeue in Data Structures
11. Bubble Sort Algorithm
12. Insertion Sort Algorithm
13. Shell Sort Algorithm
14. Radix Sort
15. Counting Sort Algorithm
16. Trees in Data Structure
17. Tree Traversal in Data Structure
18. Inorder Traversal
19. Optimal Binary Search Trees
20. AVL Tree
21. Red-Black Tree
22. B+ Tree in Data Structure
23. Expression Tree
24. Adjacency Matrix
25. Spanning Tree in Data Structure
26. Kruskal Algorithm
27. Prim's Algorithm in Data Structure
28. Bellman Ford Algorithm
29. Ford-Fulkerson Algorithm
30. Trie Data Structure
31. Floyd Warshall Algorithm
32. Rabin Karp Algorithm
33. What Is Dynamic Programming?
34. Longest Common Subsequence
35. Fractional Knapsack Problem
36. Greedy Algorithm
37. Longest Increasing Subsequence
38. Matrix Chain Multiplication
39. Subset Sum Problem
40. Backtracking Algorithm
41. Huffman Coding Algorithm
42. Tower of Hanoi
43. Stack vs Heap
44. Asymptotic Analysis
45. Binomial Distribution
46. Coin Change Problem
47. Fibonacci Heap
48. Skip List in Data Structure
49. Sparse Matrix
50. Splay Tree
51. Queue in Data Structure
52. Stack in Data Structure
53. Time and Space Complexity
54. Linked List in Data Structure
55. Stack And Queue: Roles & Functions
56. Doubly Linked List
57. Strongly Connected Components
58. Bucket Sort Algorithm
As a data structures enthusiast, I've spent countless hours exploring various techniques for storing and manipulating data efficiently. Linked lists have always fascinated me due to their dynamic nature.
Today, I want to delve into a specific type of linked list—the circular linked list (CLL). Throughout this article, we'll explore the concept of CLLs and implement them in different programming languages, showcasing their functionality through code examples.
Before we explore circular linked lists in data structure programs, let's establish common ground by revisiting basic linked lists. A linked list is a type of linear data structure where the nodes, or elements, are not kept in memory consecutively.
Each node contains data and a pointer that references the next node in the sequence. This pointer-based approach allows for dynamic memory allocation and efficient insertion/deletion operations. The two main categories of linked lists are:
Type of Linked List | Description |
Singly Linked List | A linear data structure where elements (nodes) are connected chain-like. A pointer to the following node in the sequence and data are contained in each node. The last node's pointer points to null, signifying the end of the list. |
Doubly Linked List | Similar to singly linked lists, each node has two pointers—one to the next node and one to the previous node. This allows bidirectional traversal (moving forward or backward through the list). |
Now, let's introduce the main topic – the circular linked list. In data structure programs, a circular linked list is an alternative to a singly linked list. In it, the last node's pointer doesn't point to null but instead circles back to the first node. This unique structure offers several advantages:
We have circular linked list applications in various real-world scenarios:
Application | Description |
Implementing Queues | A FIFO (First-In-First-Out) data structure where the first element added is the first element removed. |
Round-Robin Scheduling | Circular linked list algorithm in data structure that allocate CPU time to processes in a circular fashion. |
Game Development | Implementing circular buffers for network communication or managing game objects. |
Let's look at the inner workings of a CLL:
Nodes: Similar to singly linked lists, each node in a CLL has a data field and a pointer (next). But in a CLL, the circular loop is created by the last node's subsequent pointer pointing back to the first node.
Pointer Management: The key to CLLs lies in managing pointers effectively. Here's a crucial point: no explicit "head" node exists. Any node can be considered the starting point for traversal. We can use a temporary pointer variable to traverse the list and stop when it reaches the node from which it started.
Traversal Techniques: We use traversal techniques similar to singly linked lists to access elements in a CLL. We start with a pointer to any node (let's call it current) and iterate through the list by following the next pointers until the current points back to the starting node.
Code Example (C++)
C++
#include <iostream>
struct Node {
int data;
Node* next;
};
// Function that inserts a node at the start of the CLL
void insertAtBeginning(Node** head, int data) {
Node* newNode = new Node;
newNode->data = data;
// If list is empty, make the new node the head and tail (pointing to itself)
if (*head == nullptr) {
newNode->next = newNode;
*head = newNode;
return;
}
// Traverse to the last node
Node* last = *head;
while (last->next != *head) {
last = last->next;
}
// Insert the new node at the beginning
newNode->next = *head;
last->next = newNode;
*head = newNode;
}
// Function to print the CLL elements
void printList(Node* head) {
Node* current = head;
if (head == nullptr) {
std::cout << "List is empty" << std::endl;
return;
}
do {
std::cout << current->data << " ";
current = current->next;
} while (current != head);
std::cout << std::endl;
}
int main
Now we will look at insertion and deletion operations in circular linked list algorithm:
A circular linked list in data structure program allows for insertion at various positions:
We can modify the insertAtBeginning function from the previous example to achieve this:
C++
void insertAtBeginning(Node** head, int data) {
// ... existing code (create new node) ...
// If list is empty, handle it
if (*head == nullptr) {
// ... existing code ...
}
// Update last node's pointer to the new node
last->next = newNode;
*head = newNode;
}
To insert at the end, we traverse the list until the last node and update its next pointer to the new node, which then points back to the head.
Code Example (Inserting at the End):
C++
// Function to insert a node at the end of the CLL
void insertAtEnd(Node** head, int data) {
Node* newNode = new Node;
newNode->data = data;
// If list is empty, handle it (similar to insertAtBeginning)
// Traverse to the last node
Node* last = *head;
while (last->next != *head) {
last = last->next;
}
// Insert the new node at the end
newNode->next = *head;
last->next = newNode;
}
Similar to insertion, deletion can occur at different positions:
We must update the head pointer and adjust pointers accordingly.
Code Example (Deleting from the Beginning):
C++
// Function to delete a node from the beginning of the CLL
void deleteFromBeginning(Node** head) {
if (*head == nullptr) {
std::cout << "List is empty, deletion failed" << std::endl;
return;
}
// If there's only one node, set head to null
if (*head == (*head)->next) {
delete *head;
*head = nullptr;
return;
}
// Traverse to the last node
Node* last = *head;
while (last->next != *head) {
last = last->next;
}
// Store the node to be deleted
Node* temp = *head;
// Update head and last node pointers
*head = (*head)->next;
last->next = *head;
// Delete the temporary node
delete temp;
}
We've established the core functionalities of circular linked list in data structure programs. Now, let's delve deeper into how they compare with other data structures regarding performance and suitability for specific use cases.
Here's a breakdown of performance aspects for CLLs against common data structures:
Operation | Circular Linked List | Array | Singly Linked List | Doubly Linked List |
Access (random) | Not ideal | Efficient (by index) | Not efficient | Not efficient |
Access (sequential) | Efficient (forward) | Efficient | Efficient | Efficient (both ways) |
Insertion (beginning) | Efficient | Not efficient | Efficient | Efficient |
Insertion (middle) | Efficient | Not efficient | Efficient | Efficient |
Insertion (end) | Efficient | Not efficient | Efficient | Efficient |
Deletion (beginning) | Efficient | Not efficient | Efficient | Efficient |
Deletion (middle) | Efficient | Not efficient | Not efficient | Efficient |
Deletion (end) | Potentially less efficient | Not efficient | Not efficient | Efficient |
While we focused on C++ for code examples, CLLs can be implemented in various programming languages. Here's a brief overview:
Python offers a similar approach to C++. Here's an example of creating a Node class and inserting a node at the beginning:
Python Code
class Node:
def __init__(self, data):
self.data = data
self.next = None
# Function to insert a node at the beginning
def insertAtBeginning(head, data):
newNode = Node(data)
if head is None:
newNode.next = newNode
return newNode
last = head
while last.next != head:
last = last.next
newNode.next = head
last.next = newNode
return newNode
Java uses similar concepts but with class declarations and object-oriented principles. Here's an example of the Node class and inserting a node at the beginning:
Java Code
public class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
}
}
// Function to insert a node at the beginning
public static Node insertAtBeginning(Node head, int data) {
Node newNode = new Node(data);
if (head == null) {
newNode.next = newNode;
return newNode;
}
Node last = head;
while (last.next != head) {
last = last.next;
}
newNode.next = head;
last.next = newNode;
return newNode;
}
The core concepts of the circular linked list algorithm in data structures—nodes, pointers, and circular structure—remain consistent across programming languages. However, the syntax and implementation details may vary slightly based on the language's specific features.
Having grasped the fundamentals of CLLs, let's explore some advanced operations and compare them to other data structures.
CLLs offer unique capabilities beyond essential insertion and deletion. Here are a few examples:
Implementation details of these operations can be found in online resources or advanced data structures textbooks.
Now, let's compare CLLs with some commonly used data structures:
The choice between CLLs and other data structures depends on your specific needs. Consider factors like:
We have examined the idea behind circular linked lists, how they are implemented in C++, and how they are used in other programming languages. Additionally, we looked at sophisticated operations and contrasted CLLs with alternative data structures. You've given yourself invaluable knowledge to use CLLs wisely in your programming activities by grasping these ideas.
Please examine these code samples and attempt to implement them in the language of your choice. Learn the circular linked list in data structure program by experimenting with the insertion and deletion procedures.
1. What is a circular linked list and a doubly linked list?
A linked list variant known as a circular linked list (CLL) creates a closed loop by having the last node's pointer point back to the first node. In a doubly linked list, each node has two pointers: one to the node before it and one to the node after it.
2. What's the difference between a circular and a normal linked list?
Normal Linked List: The list ends when the pointer on the last node points to null.
A circular structure is created in a linked list when the pointer from the last node goes back to the first node.
3. How do you create a circular linked list in Python?
You can define a Node class with data and a next pointer. Functions can create nodes and manipulate the next pointers to establish the circular structure.
4. How do you find the last node in a circular linked list?
You can iterate through the list starting from any node. Since it's circular, when the next pointer of a node points back to the starting node, you've reached the last node.
5. What is a circular linked list?
A circular linked list (CLL) is a data structure where elements (nodes) are linked together in a circular fashion. The last node's pointer points back to the first node, creating a closed loop.
6. What are circular linked list types?
There isn't a concept of different "types" within circular linked lists. However, CLLs can be implemented with variations, such as storing only data in the nodes or adding fields for specific circular linked list applications.
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.