DD
DD
#include <iostream>
struct Node {
string name;
int idNo;
double salary;
Node* prev;
Node* next;
};
class DoublyLinkedList {
private:
Node* head;
public:
DoublyLinkedList() {
head = nullptr;
if (head == nullptr) {
head = newNode;
} else {
newNode->next = head;
head->prev = newNode;
head = newNode;
if (head == nullptr) {
head = newNode;
} else {
current = current->next;
current->next = newNode;
newNode->prev = current;
}
cout << "Inserted at end: " << name << endl;
return;
if (position == 1) {
return;
int currentPosition = 1;
current = current->next;
currentPosition++;
return;
newNode->next = current->next;
newNode->prev = current;
if (current->next != nullptr) {
current->next->prev = newNode;
current->next = newNode;
cout << "Inserted at position " << position << ": " << name << endl;
void display() {
if (head == nullptr) {
return;
cout << "Name: " << current->name << ", IdNo: " << current->idNo << ", Salary: " << current->salary
<< endl;
current = current->next;
}
void deleteAtBeginning() {
if (head == nullptr) {
cout << "Doubly linked list is empty. Cannot delete element." << endl;
return;
head = head->next;
if (head != nullptr) {
head->prev = nullptr;
delete temp;
void deleteAtEnd() {
if (head == nullptr) {
cout << "Doubly linked list is empty. Cannot delete element." << endl;
return;
}
if (head->next == nullptr) {
delete head;
head = nullptr;
return;
current = current->next;
current->next = nullptr;
delete temp;
return;
}
if (position == 1) {
deleteAtBeginning();
return;
int currentPosition = 1;
current = current->next;
currentPosition++;
return;
current->prev->next = current->next;
if (current->next != nullptr) {
current->next->prev = current->prev;
delete temp;
if (head == nullptr) {
cout << "Doubly linked list is empty. Cannot update element." << endl;
return;
if (current->idNo == idNo) {
current->salary = newSalary;
cout << "Updated element with IdNo " << idNo << endl;
return;
current = current->next;
cout << "Element with IdNo " << idNo << " not found." << endl;
cout << "Doubly linked list is empty. Cannot search element." << endl;
return;
if (current->idNo == idNo) {
cout << "Element with IdNo " << idNo << " found." << endl;
cout << "Name: " << current->name << ", Salary: " << current->salary << endl;
return;
current = current->next;
cout << "Element with IdNo " << idNo << " not found." << endl;
newNode->name = name;
newNode->idNo = idNo;
newNode->salary = salary;
newNode->prev = nullptr;
newNode->next = nullptr;
return newNode;
};
int main() {
DoublyLinkedList doublyLinkedList;
char choice;
string name;
int idNo;
double salary;
int position;
while (true) {
switch (toupper(choice)) {
case 'A':
break;
case 'B':
break;
case 'C':
break;
case 'D':
doublyLinkedList.display();
break;
case 'E':
doublyLinkedList.deleteAtBeginning();
break;
case 'F':
doublyLinkedList.deleteAtEnd();
break;
case 'G':
cout << "Enter the position: ";
doublyLinkedList.deleteAtLocation(position);
break;
case 'H':
doublyLinkedList.updateElement(idNo, salary);
break;
case 'I':
doublyLinkedList.searchElement(idNo);
break;
case 'J':
exit(0);
default:
cout << "Invalid choice. Please enter a valid option." << endl;
break;
return 0;
2.
#include <iostream>
struct Node {
int data;
Node* next;
};
class Stack {
private:
Node* top;
public:
Stack() {
top = nullptr;
}
void push(int element) {
newNode->data = element;
newNode->next = top;
top = newNode;
void pop() {
if (isEmpty()) {
return;
top = top->next;
delete temp;
void display() {
if (isEmpty()) {
return;
}
current = current->next;
void peek() {
if (isEmpty()) {
return;
bool isEmpty() {
};
int main() {
Stack stack;
char choice;
int element;
while (true) {
switch (toupper(choice)) {
case 'A':
stack.push(element);
break;
case 'B':
stack.pop();
break;
case 'C':
stack.display();
break;
case 'D':
stack.peek();
break;
case 'E':
exit(0);
default:
cout << "Invalid choice. Please enter a valid option." << endl;
break;
return 0;
3.
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
class Queue {
private:
Node* front;
Node* rear;
public:
Queue() {
front = nullptr;
rear = nullptr;
newNode->data = element;
newNode->next = nullptr;
if (rear == nullptr) {
rear->next = newNode;
rear = newNode;
void dequeue() {
if (isEmpty()) {
return;
front = front->next;
delete temp;
if (front == nullptr) {
rear = nullptr;
}
void display() {
if (isEmpty()) {
return;
current = current->next;
void peek() {
if (isEmpty()) {
return;
bool isEmpty() {
return front == nullptr;
};
int main() {
Queue queue;
char choice;
int element;
while (true) {
switch (toupper(choice)) {
case 'A':
break;
case 'B':
queue.dequeue();
break;
case 'C':
queue.display();
break;
case 'D':
queue.peek();
break;
case 'E':
exit(0);
default:
cout << "Invalid choice. Please enter a valid option." << endl;
break;
}
return 0;