DSA LAB 8
DSA LAB 8
Tools Required
● PC with Windows 7 Professional
● Visual Studio 2010
// Node structure
struct Node {
int data;
Node* next;
Node* prev;
};
class DoublyCircularLinkedList {
private:
Node* head;
public:
DoublyCircularLinkedList() { head = nullptr; }
// Insert at end
void insert(int value) {
Node* newNode = new Node;
newNode->data = value;
if (!head) {
newNode->next = newNode;
newNode->prev = newNode;
head = newNode;
} else {
Node* tail = head->prev;
tail->next = newNode;
newNode->prev = tail;
newNode->next = head;
head->prev = newNode;
}
}
int main() {
DoublyCircularLinkedList list;
list.insert(10);
list.insert(20);
list.insert(30);
list.insert(40);
list.remove(20);
cout << "After deleting 20: ";
list.display();
return 0;
}
Tasks
Task 1: Detecting and Removing Cycles:
How would you verify if a given doubly linked list is actually circular?
How would you break a doubly circular linked list and convert it into a normal
doubly linked list?
1. To verify if a given doubly linked list is circular, we can start by checking the head
node’s connections. In a circular doubly linked list, the head->prev pointer should point
to the last node, and the last node’s next pointer should point back to the head. To
confirm, we can traverse the list from the head using the next pointers. If we eventually
return to the head, it confirms that the list is circular.
CODE:
void checkcircular()
{
Node *temp = head->next;
while (temp != head || temp == NULL)
{
temp = temp->next;
}
if (head==temp)
{
cout<<"List is Circular"<<endl;
}
else
cout<<"List is not circular"<<endl;
}
2. To break a circular doubly linked list and convert it into a normal doubly linked list, we
need to remove the circular connections between the head and the last node. Since in a
circular doubly linked list, the last node can be accessed using head->prev, we set last-
>next to NULL to break the forward link. Similarly, we update head->prev to NULL to
break the backward link. This ensures that the list no longer loops back to the head,
effectively converting it into a standard doubly linked list.
CODE:
void breakCircular()
{
A group of n people are standing in a circle, and every kth person is eliminated until only
one remains. Implement this using a doubly circular linked list.
CODE:
#include <iostream>
using namespace std;
struct Node
{
int data;
Node *next;
Node *prev;
Node(int n)
{
data = n;
next = NULL;
prev = NULL;
}
};
class LinkedList
{
private:
Node *head;
public:
LinkedList()
{
head = nullptr;
}
void insert(int n)
{
temp->next = head;
head->prev = temp;
cout << "Inserted Successfully" << endl;
}
void EliminationGame(int k)
{
Node *temp = head;
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
Node *deletingnode = temp;
temp = temp->next;
delete deletingnode;
}
void display()
{
if (head == NULL)
{
cout << "List is empty." << endl;
return;
}
Node *temp = head;
do
{
cout << temp->data << " <-> ";
temp = temp->next;
} while (temp != head);
cout << endl;
}
};
int main()
{
LinkedList D1;
int n, k;
cout << "Enter Number of people:";
cin >> n;
cout << "Enter Number of Steps:";
cin >> k;
D1.insert(n);
D1.display();
D1.EliminationGame(k);
return 0;
}
Given a doubly circular linked list with an even number of nodes, how would you split
it into two equal-sized doubly circular linked lists?
How would you handle the case if the number of nodes is odd?
The same code will be implemented for odd number of nodes but the 1st half list will have 1 extra
node.
CODE:
#include <iostream>
using namespace std;
struct Node
{
int data;
Node *next;
Node *prev;
Node(int n)
{
data = n;
next = NULL;
prev = NULL;
}
};
class LinkedList
{
private:
Node *head;
public:
LinkedList()
{
head = nullptr;
}
if (temp->next == temp)
{
head = NULL;
}
else
{
temp->next->prev = temp->prev;
temp->prev->next = temp->next;
if (temp == head)
{
head = temp->next;
}
}
delete temp;
}
tail->next = secondHead;
secondHead->prev = tail;
L1.head = head;
L2.head = secondHead;
}
void display()
{
if (head == NULL)
{
cout << "List is empty." << endl;
return;
}
Node *temp = head;
do
{
cout << temp->data << " <-> ";
temp = temp->next;
} while (temp != head);
cout << "(head)" << endl;
}
};
int main()
{
LinkedList D1, L1, L2;
D1.insert(10);
D1.insert(20);
D1.insert(30);
D1.insert(40);
D1.insert(50);
D1.insert(60);
D1.insert(70);
D1.insert(80);
D1.insert(90);
D1.insert(100);
D1.splitlist(L1, L2);
return 0;
}
Task 4: Merging Two Doubly Circular Linked Lists:
How would you merge two sorted doubly circular linked lists into a single sorted list
while maintaining circularity?
#include <iostream>
using namespace std;
struct Node {
int data;
Node *next;
Node *prev;
Node(int n) {
data = n;
next = prev = NULL;
}
};
class DoublyCircularLinkedList {
public:
Node *head;
DoublyCircularLinkedList() {
head = nullptr;
}
void display() {
if (!head) {
cout << "List is empty." << endl;
return;
}
Node *temp = head;
do {
cout << temp->data << " <-> ";
temp = temp->next;
} while (temp != head);
cout << "(head)" << endl;
}
do {
Node *newNode;
if (!second || (first && first->data <= second->data)) {
newNode = new Node(first->data);
first = first->next;
if (first == head) first = nullptr;
} else {
newNode = new Node(second->data);
second = second->next;
if (second == L2.head) second = nullptr;
}
if (!mergedHead) {
mergedHead = mergedTail = newNode;
mergedHead->next = mergedHead;
mergedHead->prev = mergedHead;
} else {
mergedTail->next = newNode;
newNode->prev = mergedTail;
newNode->next = mergedHead;
mergedHead->prev = newNode;
mergedTail = newNode;
}
} while (first || second);
head = mergedHead;
}
};
int main() {
DoublyCircularLinkedList L1, L2;
L1.insert(10);
L1.insert(30);
L1.insert(50);
L1.insert(70);
L2.insert(20);
L2.insert(40);
L2.insert(60);
L2.insert(80);
L1.mergeSortedLists(L2);
return 0;
}