Adventures in Machine Learning

Discover the Power of Doubly Circular Linked Lists in Python

Doubly Circular Linked Lists: An Overview

Data structures are essential tools in computer science that allow software developers to store, manage, and manipulate data. One of the most useful data structures is the Linked List.

A Linked List is a collection of nodes that are connected through pointers, forming a sequence of linked nodes. The Linked List data structure has many variants, one of which is the Doubly Circular Linked List.

A Doubly Circular Linked List is a type of linked list that has two pointers in each node, one pointing to the previous node and another to the next node. Additionally, the last node points to the first node, forming a circular structure.

This article aims to introduce the Doubly Circular Linked List data structure to readers. It explores the purpose, definition, traversal, and other important aspects of doubly circular linked lists.

Pre-Requisites

Understanding Linked Lists

Before diving into the Doubly Circular Linked List, it is essential to understand the basics of Linked Lists. A Linked List is a set of nodes that are linked to one another.

Each node consists of two parts, data, and a reference to the next node. The first node is called the head, and the last node is called the tail.

A Tail Node points to null, indicating the end of the list. A Circular Linked List is a type of Linked List where every node points to the next node, and the last node points back to the head.

Given this, the Doubly Circular Linked List is a linked list with bidirectional pointers that form a circular structure. The structure maintains a circular order of nodes, similar to a Circular Linked List.

Understanding Doubly Linked Lists

A Doubly Linked List is a variation of a simple linked list where each node maintains two pointers, one that points to the next node and another that points to the previous node. These pointers enable traversal of the linked list in both directions.

Each Node of a Doubly Linked List, apart from the data, has two pointers; prev and next. ‘prev’ is a pointer that refers to the previous node in the list, while ‘next’ points to the next node.

Traversal of a Doubly Linked List is accomplished in both directions; that is forwards or backward. To access the nth node of a Doubly Linked List, the list’s head or tail is traversed in either direction till both pointers converge at the desired position.

Doubly Circular Linked Lists

A Doubly Circular Linked List is a Linked List variation that is a combination of a doubly linked list and a circular linked list.

In a Doubly Circular Linked List, each node has two pointers: a pointer that points to the previous node and another to the next node. Apart from this, the last node’s next pointer points to the first node, while the first node’s prev pointer points to the last node, forming a Circle structure.

In contrast to a Simple Linked List or a Doubly Linked List, a Doubly Circular Linked List does not have a head or tail as such. The start and endpoint of a Doubly Circular Linked List are arbitrary since the reference to these nodes is explicit.

Traversal of a Doubly Circular Linked List is similar to a Simple Linked List, but the only difference is the last node of a doubly-linked list points to the head. Therefore, all the Doubly Circular Linked Lists functionalities, like accessing the nth node and traversing in a forward or reverse direction, are implemented in the same way.

Purpose of Doubly Circular Linked List

The purpose of a Doubly Circular Linked List is to provide an efficient data structure which can be utilized in various applications such as music players, messengers that work on the concept of chats, and to optimize operations such as insertion and deletion on large data sets. Doubly Circular Linked List enables forward and backward traversing without the need to traverse the entire linked list from the beginning, thus enhancing the search time complexity.

Also, the implementation of operations, such as deletion and insertion, allows these functionalities to be completed in constant time, with the aid of the given node reference, unlike simple linked lists.

Conclusion

In conclusion, the Doubly Circular Linked List is an essential data structure used in several computer science applications. This linked list variant combines the qualities of a Doubly Linked List and a Circular Linked List, thus enabling the user to traverse in both directions – forward or backward – without having to traverse the entire linked list from the start, and to delete or insert efficiently.

Understanding Circular Linked Lists

A Circular Linked List is a type of Linked List in which every node points to the next node, except for the last node, which points to the first node of the list. The last node of a Circular Linked List points back to the first node, forming a cycle.

The main purpose of Circular Linked Lists is to provide an efficient data structure for applications where you want to traverse the list in a circular manner, or when you want to add and remove elements frequently. Circular Linked Lists are an upgrade from Simple Linked Lists where you traverse the nodes till you find the end of the list.

In Circular Linked Lists, you can traverse the nodes from any node since there are cycles in the list.

Circularity

Circularity is the defining feature of the Circular Linked List. Every node in the Circular Linked List points to the next node, creating a chain of nodes.

Since the final node links to the first node to create the cycle, the Linked List is circular. The use of circular linkage in the Circular Linked List makes the list more flexible.

Traversal of the list in both the forward and backward directions is possible, and the traversal can begin from any node in the list. Additionally, deletion and insertion of any node take constant time, regardless of the position of the node.

Another notable benefit of a Circular Linked List is that it can be traversed multiple times by referencing any of the nodes in the linked list. This is especially useful when a computer program has a need to analyze a list multiple times, optimizing processing time.

Doubly Circular Linked Lists

Doubly Circular Linked Lists combine the properties of Doubly Linked Lists and Circular Linked Lists to create a versatile data structure. A Doubly Circular Linked List has bidirectional links that connect the nodes.

The first node’s prev pointer points to the last node, while the last node’s next pointer points to the first node, forming a cycle. The connections between each node in a Doubly Circular Linked List make it possible to traverse the list in both the forward direction and the backward direction.

This feature enhances the search efficiency of the data structure.

Unlike Simple and Circular Linked Lists, a Doubly Circular Linked List is a combination of the Doubly Linked List and Circular Linked List, thus making it more efficient in operations such as deletion and insertion, and increasing the speed at which the linked list can be traversed.

Traversal

Traversal of a Doubly Circular Linked List is similar to other linked lists. To traverse the list, you start at any node and move either forward or backward to the next or previous node.

Since a Doubly Circular Linked List is a circular linked list, it is necessary to include traversal stopping conditions to prevent infinite loops. Accessing nodes in a Doubly Circular Linked List is achieved in a manner similar to accessing nodes in Circular Linked Lists.

One can access the nth node in a Doubly Circular Linked List by traversing the linked list in either direction from either the head or tail node till you reach the desired node.

Conclusion

In conclusion, Linked List data structures come in many variations, and each offers unique capabilities that solve specific problems. Circular Linked Lists are an efficient data structure for traversing a list of nodes in a circular manner, while Doubly Circular Linked Lists are a more versatile data structure that has all the features of Doubly Linked Lists and Circular Linked Lists while offering more functionality.

A Doubly Circular Linked List has bidirectional links, making it possible to move both forward and backward in the linked list, making it more efficient for searching and processing linked lists in both directions. Implementing

Doubly Circular Linked Lists in Python

Python is a popular and flexible programming language designed to be easy to read and write.

As such, it is an excellent language for data structures like Doubly Circular Linked Lists. In this section, we will look at how to create a Doubly Circular Linked List class in Python and various methods that can be used with the class.

Creation of Classes

To create a Doubly Circular Linked List in Python, we need to define a Node class and a main DCLL class. The Node class represents a single node within the linked list and stores the node’s data along with the pointers to the previous and next nodes.

class Node:
    def __init__(self, data):
        self.data = data
        self.prev = None
        self.next = None

The DCLL class represents the entire doubly circular linked list. It contains the head node and other methods such as append, insert, remove, index, get, size, and display, which we will explain shortly.

class DCLL:
    def __init__(self):
        self.head = None

Class Methods

The methods of the DCLL class are used to manipulate the data structure.

The append method adds a new node containing the data provided to the end of the linked list:

def append(self, data):
    new_node = Node(data)
    if self.head == None:
        self.head = new_node
        new_node.next = self.head
        new_node.prev = self.head
    else:
        temp = self.head
        while(temp.next != self.head):
            temp = temp.next
        temp.next = new_node
        new_node.prev = temp
        new_node.next = self.head
        self.head.prev = new_node

The insert method inserts a new node containing the data provided at the specified index in the linked list.

If the index is out of range, the method raises an error:

def insert(self, index, data):
    if index < 0 or index > self.size():
        raise Exception("Index out of range")
    elif index == 0:
        self.prepend(data)
    elif index == self.size():
        self.append(data)
    else:
        new_node = Node(data)
        current = self.head
        count = 0
        while count < index - 1:
            current = current.next
            count += 1
        new_node.next = current.next
        current.next.prev = new_node
        current.next = new_node
        new_node.prev = current

The remove method removes the node at the specified index in the linked list. If the index is out of range, the method raises an error:

def remove(self, index):
    if index < 0 or index >= self.size():
        raise Exception("Index out of range")
    elif index == 0:
        self.head = self.head.next
        self.head.prev = self.head.prev.prev
    else:
        current = self.head
        count = 0
        while count < index - 1:
            current = current.next
            count += 1
        current.next = current.next.next
        current.next.prev = current

The index method returns the index of the first occurrence of the data provided in the linked list:

def index(self, data):
    current = self.head
    count = 0
    while current != None:
        if current.data == data:
            return count
        count += 1
        current = current.next
    raise Exception("Data not found")

The get method returns the data of the node at the specified index in the linked list:

def get(self, index):
    if index < 0 or index >= self.size():
        raise Exception("Index out of range")
    current = self.head
    count = 0
    while count < index:
        current = current.next
        count += 1
    return current.data

The size method returns the number of nodes in the linked list:

def size(self):
    count = 0
    current = self.head
    while current != None:
        count += 1
        current = current.next
        if current == self.head:
            break
    return count

Finally, the display method prints the data of all the nodes in the linked list:

def display(self):
    current = self.head
    while current != None:
        print(current.data)
        current = current.next
        if current == self.head:
            break

The Output

After implementing the above methods, we can create and operate on an instance of the Doubly Circular Linked List class in Python. The following code demonstrates how we can create a Doubly Circular Linked List instance, append, insert, remove, and display elements in the list.

lst = DCLL()
lst.append(2)
lst.append(3)
lst.append(4)
lst.insert(1, 5)
lst.remove(3)
lst.display()

The output of the above code would be:

2
5
3

The code creates an instance of the DCLL class and appends three nodes containing data 2, 3, and 4. Next, the code inserts a node containing data 5 at index 1.

After that, the code removes the fourth node. Finally, the display method is called to print the data in all nodes of the linked list.

In conclusion, implementing a Doubly Circular Linked List in Python is straightforward and intuitive. We can create a class for the Doubly Circular Linked List and add various methods such as append, insert, remove, index, get, size, and display to operate the list.

By using these methods, we can manipulate the list in various ways, including insertion, deletion, and traversal. In conclusion, Linked Lists are an essential data structure in computer science with Circular Linked Lists and Doubly Circular Linked Lists being particularly efficient.

Circular Linked Lists is used to traverse lists of node in a circular manner combining nodes in a circular fashion.

Doubly Circular Linked Lists has bidirectional links, making it possible to traverse the list forward and backward, making it more efficient for searching and processing linked lists in both directions.

Implementing

Doubly Circular Linked Lists in Python is easy by defining a Node class and main DCLL class, including various methods such as append, insert, remove, index, get, size, and display. These methods enable the programmer to manipulate data structures efficiently and add or remove elements rapidly.

Popular Posts