Dsa 2
Dsa 2
#include <iostream>
using namespace std;
struct Node {
char data;
Node* Prev;
Node* Next;
};
class CircularDoublyLinkedList {
private:
Node* Start;
public:
CircularDoublyLinkedList() : Start(NULL) {}
void createInitialNodes() {
Start = new Node{'A', NULL, NULL};
Node* dump = Start;
char values[] = {'B', 'C', 'D', 'E'};
for (char val : values) {
dump->Next = new Node{val, dump, NULL};
dump = dump->Next;
}
dump->Next = Start;
Start->Prev = dump;
}
void display() {
if (!Start) {
cout << "List is empty." << endl;
return;
}
Node* temp = Start;
do {
cout << temp->data << " ";
temp = temp->Next;
} while (temp != Start);
cout << endl;
}
void insertAtStart(char value) {
Node* dump = new Node{value, Start ? Start->Prev : NULL, Start ? Start : NULL};
if (!Start) {
dump->Next = dump->Prev = Start = dump;
} else {
Start->Prev->Next = dump;
Start->Prev = dump;
Start = dump;
}
}
void insertAtEnd(char value) {
if (!Start) {
insertAtStart(value);
} else {
Node* dump = new Node{value, Start->Prev, Start};
Start->Prev->Next = dump;
Start->Prev = dump;
}
}
void insertAfter(char value, char afterValue) {
if (!Start) return;
Node* current = Start;
do {
if (current->data == afterValue) {
Node* dump = new Node{value, current, current->Next};
current->Next->Prev = dump;
current->Next = dump;
return;
}
current = current->Next;
} while (current != Start);
}
void deleteAtStart() {
if (!Start) return;
Node* temp = Start;
if (Start->Next == Start) {
Start = NULL;
} else {
Start->Prev->Next = Start->Next;
Start->Next->Prev = Start->Prev;
Start = Start->Next;
}
delete temp;
}
void deleteAtEnd() {
if (!Start) return;
Node* temp = Start->Prev;
if (temp == Start) {
delete Start;
Start = NULL;
} else {
temp->Prev->Next = Start;
Start->Prev = temp->Prev;
delete temp;
}
}
void deleteAfter(char afterValue) {
if (!Start) return;
Node* current = Start;
do {
if (current->data == afterValue) {
Node* toDelete = current->Next;
if (toDelete == Start) {
deleteAtStart();
} else {
current->Next = toDelete->Next;
toDelete->Next->Prev = current;
delete toDelete;
}
return;
}
current = current->Next;
} while (current != Start);
}
};
int main() {
CircularDoublyLinkedList list;
list.createInitialNodes();
int choice;
char value, afterValue;
do {
cout << "\nMenu:\n"
<< "1. Insert at start\n"
<< "2. Insert at end\n"
<< "3. Insert after\n"
<< "4. Delete at start\n"
<< "5. Delete at end\n"
<< "6. Delete after\n"
<< "7. Display list\n"
<< "8. Exit\n"
<< "Enter choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value: ";
cin >> value;
list.insertAtStart(value);
break;
case 2:
cout << "Enter value: ";
cin >> value;
list.insertAtEnd(value);
break;
case 3:
cout << "Enter value: ";
cin >> value;
cout << "Insert after: ";
cin >> afterValue;
list.insertAfter(value, afterValue);
break;
case 4:
list.deleteAtStart();
break;
case 5:
list.deleteAtEnd();
break;
case 6:
cout << "Delete after: ";
cin >> afterValue;
list.deleteAfter(afterValue);
break;
case 7:
list.display();
break;
case 8:
cout << "Exiting." << endl;
break;
default:
cout << "Invalid choice!" << endl;
}
} while (choice != 8);
return 0;
}
Algorithm for Circular Doubly Linked List:
1. Initialize List:
o Create a class CircularDoublyLinkedList.
o Set the Start pointer to NULL (empty list).
2. Create Initial Nodes:
o Create the first node and assign its Next and Prev pointers to itself.
o For each new node (values B, C, D, E), set its Prev to the previous node and its
Next to the Start node.
o Set the Prev of the Start node to the last node.
3. Display List:
o Traverse the list starting from Start.
o Print each node’s data until you loop back to Start.
4. Insert at Start:
o Create a new node.
o If the list is empty, point the new node’s Next and Prev to itself.
o If the list is not empty, update the Next and Prev of the existing nodes, and set the
new node as Start.
5. Insert at End:
o If the list is empty, use the insertAtStart method.
o If the list is not empty, create a new node and update the Next of the last node and
the Prev of the Start node.
6. Insert After a Specific Node:
o Traverse the list until the node with the specified value is found.
o Insert the new node after this node by updating its Next and Prev pointers.
7. Delete at Start:
o If the list has only one node, set Start to NULL.
o If the list has more than one node, update the Next and Prev pointers of the
surrounding nodes and delete the first node.
8. Delete at End:
o If the list has only one node, delete it and set Start to NULL.
o If the list has more than one node, update the Next and Prev pointers of the
surrounding nodes and delete the last node.
9. Delete After a Specific Node:
o Traverse the list until the node with the specified value is found.
o Delete the node after this node by updating the Next and Prev pointers of the
surrounding nodes.
10. Main Menu:
o Show a menu with options:
1. Insert at Start
2. Insert at End
3. Insert After
4. Delete at Start
5. Delete at End
6. Delete After
7. Display List
8. Exit
o Execute the selected operation based on user input.
11. Exit:
o Terminate the program when the user selects the exit option.
Circular Queue
#include<iostream>
using namespace std;
class CircularQueue {
private:
int Front, Rear;
int Queue[10];
public:
CircularQueue() {
Front = Rear = -1;
}
void EnQueue(int x) {
if (Front == -1 && Rear == -1) {
Front = Rear = 0;
Queue[Rear] = x;
cout << "Enqueued " << x << " successfully." << endl;
} else if ((Rear + 1) % 10 == Front) {
cout << "Queue is full" << endl;
} else {
Rear = (Rear + 1) % 10;
Queue[Rear] = x;
cout << "Enqueued " << x << " successfully." << endl;
}
}
void DeQueue() {
if (Front == -1 && Rear == -1) {
cout << "Queue is empty" << endl;
} else if (Front == Rear) {
cout << "Dequeued " << Queue[Front] << endl;
Front = Rear = -1;
} else {
cout << "Dequeued " << Queue[Front] << endl;
Front = (Front + 1) % 10;
}
}
void Display() {
if (Front == -1 && Rear == -1) {
cout << "Queue is empty" << endl;
return;
}
cout << "Queue elements: ";
int i = Front;
while (i != Rear) {
cout << Queue[i] << " ";
i = (i + 1) % 10;
}
cout << Queue[Rear] << endl;
}
};
int main() {
CircularQueue queue;
int choice, value;
do {
cout << "\nCircular Queue Menu:" << endl;
cout << "1. EnQueue" << endl;
cout << "2. DeQueue" << endl;
cout << "3. Display Queue" << endl;
cout << "4. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
if (choice == 1) {
cout << "Enter value to EnQueue: ";
cin >> value;
queue.EnQueue(value);
} else if (choice == 2) {
queue.DeQueue();
} else if (choice == 3) {
queue.Display();
} else if (choice == 4) {
cout << "Exiting program." << endl;
} else {
cout << "Invalid choice! Please try again." << endl;
}
} while (choice != 4);
return 0;
}