Circular List - Doubly
Circular List - Doubly
DSA-Lab-Manual
PMAS-Arid Agriculture University, Rawalpindi.
Gujrat Institute of Management Sciences
LAB # 5
Doubly Circular Link list
Objectives:
At the end of the lab, students are expected to be able to do the following
Declare and initialize a Doubly Circular link list.
Perform different operations on Doubly Circular link list.
Introduction:
Doubly Circular Linked List:
A doubly circular linked list is a type of linked list where:
1. Each node has three components: data, a pointer to the next node, and a pointer to
the previous node.
2. The last node's next pointer points to the head node, and the head node's prev
pointer points to the last node, forming a circular structure.
Advantages:
Bidirectional Traversal: Nodes can be traversed in both directions using next and prev
pointers.
Efficient Circular Structure: Makes it easy to implement applications like round-robin
scheduling.
Quick Insert/Delete: Can insert or delete at any position without shifting elements, especially
when a pointer to the node is given.
Efficient Looping: Traversing the list multiple times is straightforward due to its circular nature
Disadvantages:
Extra Memory: Each node requires extra space for the prev pointer.
Complex Implementation: Requires careful handling of pointers, especially during insertion and
deletion operations.
Debugging Difficulties: Circular nature can lead to infinite loops if not handled properly.
Overhead: Managing both next and prev pointers increases computational overhead.
basic example of a node structure in C++:
struct Node {
int data;
Node* next;
Node * pre;
};
DSA-Lab-Manual
PMAS-Arid Agriculture University, Rawalpindi.
Gujrat Institute of Management Sciences
Code:
#include <iostream>
using namespace std;
// Node structure
struct Node {
int data;
Node* next;
Node* prev;
};
public:
// Constructor
DoublyCircularLinkedList() : head(nullptr) {}
DSA-Lab-Manual
PMAS-Arid Agriculture University, Rawalpindi.
Gujrat Institute of Management Sciences
// Main function
int main() {
DoublyCircularLinkedList list;
list.insert(10);
list.insert(20);
list.insert(30);
list.display();
list.remove(20);
list.display();
DSA-Lab-Manual
PMAS-Arid Agriculture University, Rawalpindi.
Gujrat Institute of Management Sciences
list.remove(40);
list.display();
return 0;
}
Code:
#include <iostream>
using namespace std;
// Node structure
struct Node {
int data;
Node* next;
Node* prev;
};
public:
// Constructor
DoublyLinkedList() : head(nullptr) {}
DSA-Lab-Manual
PMAS-Arid Agriculture University, Rawalpindi.
Gujrat Institute of Management Sciences
// Main function
int main() {
DoublyLinkedList list;
DSA-Lab-Manual
PMAS-Arid Agriculture University, Rawalpindi.
Gujrat Institute of Management Sciences
} else {
cout << "Value " << valueToSearch << " is not in the list.\n";
}
valueToSearch = 50;
if (list.search(valueToSearch)) {
cout << "Value " << valueToSearch << " is found in the list.\n";
} else {
cout << "Value " << valueToSearch << " is not in the list.\n";
}
return 0;
}
Task:
Write a program to implement a doubly Circular Singly Linked List with the following
functionalities:
1. Create a list.
2. Insert a node at the beginning, end, or a specific position.
3. Delete a node from the beginning, end, or a specific position.
4. Search for an element in the list and display its position(s).
5. Update the value of a node at a specific position.
6. Display the list.
Include a menu-driven interface to perform these operations repeatedly until the user chooses to
exit
Code:
#include <iostream>
using namespace std;
DSA-Lab-Manual
PMAS-Arid Agriculture University, Rawalpindi.
Gujrat Institute of Management Sciences
private:
Node *head, *tail, *current, *previous;
public:
LinkedList()
{
head = tail = current = previous = nullptr;
}
DSA-Lab-Manual
PMAS-Arid Agriculture University, Rawalpindi.
Gujrat Institute of Management Sciences
if (position == 1)
{
insertAtStart(value);
return;
}
if (current->next == head)
{
append(value); // insert at end if position is beyond last element
}
else
{
newNode->next = current->next;
newNode->prev = current;
current->next->prev = newNode;
current->next = newNode;
}
}
DSA-Lab-Manual
PMAS-Arid Agriculture University, Rawalpindi.
Gujrat Institute of Management Sciences
if (head == nullptr)
{
cout << "---> List is empty!" << endl;
return;
}
Node *temp = head;
if (head == tail)
{
head = tail = nullptr;
}
else
{
tail->next = head->next;
head->next->prev = tail;
head = head->next;
}
delete temp;
}
DSA-Lab-Manual
PMAS-Arid Agriculture University, Rawalpindi.
Gujrat Institute of Management Sciences
if (position == 1)
{
deleteAtStart();
return;
}
current = head;
for (int i = 1; i < position && current != head; ++i)
{
previous = current;
current = current->next;
}
if (current == head)
{
cout << "---> Position out of range!" << endl;
return;
}
previous->next = current->next;
current->next->prev = previous;
if (current == tail)
{
tail = previous; // Update tail if last element is deleted
}
delete current;
}
current = head;
do
{
if (current->data == value)
{
cout << "---> " << value << " is found at position " << pos <<
endl;
return;
}
current = current->next;
pos++;
DSA-Lab-Manual
PMAS-Arid Agriculture University, Rawalpindi.
Gujrat Institute of Management Sciences
cout << "---> " << value << " is not found in the list." << endl;
}
if (position < 1)
{
cout << "---> Invalid position!" << endl;
return;
}
current = head;
if (current == head)
{
cout << "---> Position out of range!" << endl;
}
else
{
current->data = newValue;
cout << "---> Node at position " << position << " updated to " <<
newValue << endl;
}
}
DSA-Lab-Manual
PMAS-Arid Agriculture University, Rawalpindi.
Gujrat Institute of Management Sciences
current = head;
do
{
cout << current->data << " <-> ";
current = current->next;
} while (current != head);
cout << "(back to head)" << endl;
}
};
do
{
cout << "\n***-***-***- DOUBLY CIRCULAR LINKED LIST -***-***-***\n"
<< "1. Insertion\n"
<< "2. Deletion\n"
<< "3. Searching\n"
<< "4. Updation\n"
<< "5. Display\n"
<< "6. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
Insertion(list);
break;
case 2:
Deletion(list);
DSA-Lab-Manual
PMAS-Arid Agriculture University, Rawalpindi.
Gujrat Institute of Management Sciences
break;
case 3:
list.search();
break;
case 4:
list.update();
break;
case 5:
list.display();
break;
case 6:
cout << "\n--> Exiting Program...\n";
break;
default:
cout << "\n--> Warning! Invalid input.\n";
break;
}
} while (choice != 7);
return 0;
}
int choice;
switch (choice)
{
case 1:
list.insertAtStart(value);
break;
DSA-Lab-Manual
PMAS-Arid Agriculture University, Rawalpindi.
Gujrat Institute of Management Sciences
case 2:
int position;
cout << "Enter the position to insert: ";
cin >> position;
list.insertAtPosition(value, position);
break;
case 3:
list.insertAtEnd(value);
break;
case 4:
cout << "\n--> Exiting Insertion...\n";
break;
default:
cout << "\n--> Invalid input!\n";
break;
}
switch (choice)
{
case 1:
list.deleteAtStart();
break;
case 2:
int position;
cout << "Enter the position to insert: ";
cin >> position;
list.deleteAtPosition(position);
DSA-Lab-Manual
PMAS-Arid Agriculture University, Rawalpindi.
Gujrat Institute of Management Sciences
break;
case 3:
list.deleteAtEnd();
break;
case 4:
cout << "\n--> Exiting Deletion...\n";
break;
default:
cout << "\n--> Invalid input!\n";
break;
}
DSA-Lab-Manual
PMAS-Arid Agriculture University, Rawalpindi.
Gujrat Institute of Management Sciences
DSA-Lab-Manual