Circular Singly Linked List šŸŽÆ

beginner
15 min

Circular Singly Linked List šŸŽÆ

A Circular Singly Linked List (CSLL) is a type of linked list where the last node points back to the first node, creating a circular structure. This makes the list continuous and easier to traverse, as we can start from any node.

Let's dive into the world of Circular Singly Linked Lists and learn how to create, traverse, and manipulate them! šŸš€

Understanding the Basics šŸ“

Before we delve into the details, let's understand the basic components of a Singly Linked List:

  1. Node: A node consists of data and a reference to the next node.
  2. Head: The first node in the list, also known as the head, doesn't have a previous node.
  3. Tail: The last node in the list, also known as the tail, doesn't have a next node.
  4. Null Node: When the list is empty, the head and tail both point to a null node (None in Python, null in Java, etc.).

Singly Linked List

Now, let's modify this structure to create a Circular Singly Linked List:

Circular Singly Linked List

As you can see, the last node in the list (the tail) points back to the first node (the head), creating a circular structure.

Implementing a Circular Singly Linked List šŸ’”

Here's an example implementation of a Circular Singly Linked List in Python:

python
class Node: def __init__(self, data): self.data = data self.next = None class CircularLinkedList: def __init__(self): self.head = None def insert(self, data): new_node = Node(data) if not self.head: self.head = new_node self.head.next = self.head # Creating a circular structure else: last_node = self.head while last_node.next != self.head: last_node = last_node.next last_node.next = new_node new_node.next = self.head def traverse(self): if not self.head: print("List is empty.") return current = self.head print("Circular Singly Linked List: ", end=" ") do_once = True while True: print(current.data, end=" -> ") current = current.next if do_once: do_once = False else: if current == self.head: break
def delete(self, data): if not self.head: print("List is empty.") return if self.head.data == data: self.head = self.head.next if self.head == self.head.next: self.head = None return current = self.head while current.next != self.head: if current.next.data == data: current.next = current.next.next if current.next == self.head: self.head = current.next break current = current.next Now you can create, traverse, and manipulate a Circular Singly Linked List using the provided `CircularLinkedList` class! ## Real-world Applications šŸ’” Circular Singly Linked Lists find their use in various real-world scenarios: 1. **Implementing cache data structures**: Circular Singly Linked Lists are used in cache systems to efficiently store and retrieve frequently accessed data. 2. **Buffering data streams**: Circular Singly Linked Lists are employed to manage data streams where the data needs to be processed in a continuous manner. 3. **Creating a circular queue**: A circular queue is a type of data structure that uses a Circular Singly Linked List to store data in a first-in, first-out (FIFO) manner, with a fixed capacity. ## Quiz Time! šŸŽ² 1. What is a Circular Singly Linked List? A: A linear list with a tail pointing to the head. B: A circular list with no tail or head. C: A list with multiple heads and tails. 2. What is the purpose of the `insert` method in the `CircularLinkedList` class? A: To delete a node from the list. B: To insert a new node into the list. C: To traverse the list. 3. How can you traverse a Circular Singly Linked List using the `CircularLinkedList` class? A: By calling the `traverse` method on the list object. B: By calling the `traverse` method on the head node. C: By manually iterating through the nodes.

Now that you've learned about Circular Singly Linked Lists, you can further explore their advanced concepts and applications in data structures and algorithms. Happy coding! šŸŽ‰