### Executive Summary

Imagine a train with a multitude of carriages, each connecting to the next and the previous one, and a circular track that allows the train to go around infinitely without end or beginning. This article is a deep dive into the world of doubly linked lists and circularly linked lists - data structures that mirror this train analogy. From their essential structures, technical advantages, and intricate manipulations, to the application in computer science problems, we will explore them all. Key takeaways will be understanding their operational complexity, designing algorithms for their manipulation, and visualizing their workings for improved comprehension. This discussion will touch both on the theoretical underpinnings and practical implementation in C++. Remember, as we delve into these structures, they are tools to solve a myriad of problems, not just abstract concepts. Read on to explore a domain where computing science artistry meets intellectual challenge and innovative solutions.

### Introduction

Imagine you have a multitude of connected nodes, each holding critical data. A one-way street would limit your navigation, demanding that you traverse from the first to the last, even if the required node is closer from the end. That's where doubly linked lists come in, providing a two-way street for seamless and efficient navigation. The extension to this - circularly linked lists, add another dimension, allowing an infinite loop through the list and omitting the need to track the start or the end.

### 1. Doubly Linked Lists

A Doubly Linked List (DLL) is a complex type of linked list which consists of a set of sequentially linked records called nodes. Each node contains three fields: two link fields (references to the previous and to the next node in the sequence of nodes) and one data field. The starting and ending node's previous and next link is null, respectively.

#### 1.1 Properties

Some key properties of a DLL include:

- Each node points to both the next node and the previous node, providing two-way navigation.
- The first node's (head) previous link and the last node's (tail) next link are null, signifying the end of the list.
- It allows for efficient insertion and deletion of nodes from both ends.
- Traversal can occur from both the head or the tail, facilitating reverse navigation.

#### 1.2 C++ Implementation

```
struct Node {
int data;
Node* prev;
Node* next;
};
Node* newNode(int data) {
Node* node = new Node;
node->data = data;
node->prev = node->next = nullptr;
return node;
}
void push(Node** head_ref, Node* new_node) {
new_node->next = *head_ref;
new_node->prev = nullptr;
if ((*head_ref) != nullptr) {
(*head_ref)->prev = new_node;
}
*head_ref = new_node;
}
```

#### 1.3 Operations

A number of common operations can be performed on DLLs, including search, insert, delete, reverse, and traverse. Each of these operations has its own time complexity and use case. Let's discuss the insertion and deletion operations.

##### 1.3.1 Insertion

The insertion operation in a DLL involves adding a new node to the list. It can occur in four scenarios:

- Insertion at the front
- Insertion at the end
- Insertion after a given node
- Insertion before a given node

```
void insertAfter(Node* prev_node, int new_data) {
if (prev_node == nullptr) {
cout << "Previous node cannot be null";
return;
}
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = prev_node->next;
prev_node->next = new_node;
new_node->prev = prev_node;
if (new_node->next != nullptr)
new_node->next->prev = new_node;
}
```

##### 1.3.2 Deletion

Deletion operation involves removing an existing node from the list. The delete operation can be complex as we need to adjust the previous and next pointers of the adjacent nodes.

```
void deleteNode(Node** head_ref, Node* del) {
if (*head_ref == nullptr || del == nullptr)
return;
if (*head_ref == del)
*head_ref = del->next;
if (del->next != nullptr)
del->next->prev = del->prev;
if (del->prev != nullptr)
del->prev->next = del->next;
free(del);
return;
}
```

### 2. Circularly Linked Lists

Circularly linked lists (CLLs) are an advanced type of linked list where all nodes are connected to form a circle. There is no NULL at the end. A circular linked list can be a singly circular linked list or a doubly circular linked list.

#### 2.1 Properties

Key properties of a CLL include:

- The last node points to the first node of the list, forming a cycle.
- The first node points to the last node in a doubly circular linked list, forming a two-way navigation cycle.
- Any point in the list can be a starting point, as it cycles infinitely.
- CLLs allow for efficient round-robin resource sharing, cyclic permutations and algorithmic problem solving.

#### 2.2 C++ Implementation

```
struct Node {
int data;
Node *next, *prev;
};
Node* newNode(int data) {
Node* temp = new Node;
temp->data = data;
temp->next = temp->prev = nullptr;
return temp;
}
void insertEnd(Node** start, int data) {
if (*start == nullptr) {
Node* new_node = newNode(data);
new_node->next = new_node->prev = new_node;
*start = new_node;
return;
}
Node *last = (*start)->prev;
Node* new
_node = newNode(data);
new_node->next = *start;
(*start)->prev = new_node;
new_node->prev = last;
last->next = new_node;
}
```

#### 2.3 Operations

Just like DLLs, a number of operations can be performed on CLLs, including search, insert, delete, reverse, and traverse. Let's explore insertion and deletion.

##### 2.3.1 Insertion

Insertion operation in a CLL involves adding a new node to the list. It can occur at three positions:

- Insertion at the beginning
- Insertion at the end
- Insertion in the middle

```
void insertStart(Node** start, int data) {
Node *last = (*start)->prev;
Node* new_node = newNode(data);
new_node->next = *start;
new_node->prev = last;
last->next = (*start)->prev = new_node;
*start = new_node;
}
```

##### 2.3.2 Deletion

Deletion operation in a CLL involves removing an existing node from the list. Like DLLs, deletion can also be a complex operation as we need to adjust the next and prev pointers of the adjacent nodes.

```
void deleteNode(Node** start, int key) {
if (*start == nullptr)
return;
Node *curr = *start, *prev;
while (curr->data != key) {
if (curr->next == *start) {
printf("\nGiven node is not found"
" in the list!!!");
break;
}
prev = curr;
curr = curr->next;
}
if (curr->next == *start) {
*start = nullptr;
free(curr);
return;
}
if (curr == *start) {
prev = (*start)->prev;
*start = (*start)->next;
prev->next = *start;
(*start)->prev = prev;
free(curr);
}
else if (curr->next == *start) {
prev->next = *start;
(*start)->prev = prev;
free(curr);
} else {
Node* temp = curr->next;
prev->next = temp;
temp->prev = prev;
free(curr);
}
}
```

### Wrapping Up

Having explored the intricate world of doubly linked lists and circularly linked lists, we now stand at the crossroads of data structures, with a deeper understanding and newfound appreciation for these highly versatile and essential structures. Whether it's traversing a DLL from either end or looping infinitely in a CLL, we've uncovered the mechanisms that drive these list types and the complex operations we can perform.

Remember, mastering these structures is a journey, not a destination. As we delve deeper into their properties, operations, and intricate implementation details, we invite you to ponder on their myriad applications and potential for problem-solving.

In our next discourse, we will transition from this circular platform to the more dimensional world of trees, dissecting their root, branches, and leaves. We will explore how they grow in computer memory, branch out to solve problems, and contribute to the lush forest of computer science data structures. So, stay connected as we continue our journey, uncovering the intricate and fascinating world of data structures.