0% found this document useful (0 votes)
21 views24 pages

DD

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views24 pages

DD

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

1.

#include <iostream>

using namespace std;

struct Node {

string name;

int idNo;

double salary;

Node* prev;

Node* next;

};

class DoublyLinkedList {

private:

Node* head;

public:

DoublyLinkedList() {

head = nullptr;

void insertAtBeginning(string name, int idNo, double salary) {

Node* newNode = createNode(name, idNo, salary);

if (head == nullptr) {
head = newNode;

} else {

newNode->next = head;

head->prev = newNode;

head = newNode;

cout << "Inserted at beginning: " << name << endl;

void insertAtEnd(string name, int idNo, double salary) {

Node* newNode = createNode(name, idNo, salary);

if (head == nullptr) {

head = newNode;

} else {

Node* current = head;

while (current->next != nullptr) {

current = current->next;

current->next = newNode;

newNode->prev = current;

}
cout << "Inserted at end: " << name << endl;

void insertAtLocation(string name, int idNo, double salary, int position) {

if (position < 1 || (position > 1 && head == nullptr)) {

cout << "Invalid position. Cannot insert element." << endl;

return;

if (position == 1) {

insertAtBeginning(name, idNo, salary);

return;

Node* newNode = createNode(name, idNo, salary);

Node* current = head;

int currentPosition = 1;

while (currentPosition < position - 1 && current->next != nullptr) {

current = current->next;

currentPosition++;

if (currentPosition < position - 1) {

cout << "Invalid position. Cannot insert element." << endl;


delete newNode;

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) {

cout << "Doubly linked list is empty." << endl;

return;

cout << "Elements of the doubly linked list:" << endl;

Node* current = head;

while (current != nullptr) {

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;

Node* temp = head;

head = head->next;

if (head != nullptr) {

head->prev = nullptr;

cout << "Deleted at beginning: " << temp->name << endl;

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;

cout << "Deleted at end." << endl;

return;

Node* current = head;

while (current->next->next != nullptr) {

current = current->next;

Node* temp = current->next;

current->next = nullptr;

cout << "Deleted at end: " << temp->name << endl;

delete temp;

void deleteAtLocation(int position) {

if (position < 1 || head == nullptr) {

cout << "Invalid position. Cannot delete element." << endl;

return;
}

if (position == 1) {

deleteAtBeginning();

return;

Node* current = head;

int currentPosition = 1;

while (currentPosition < position && current->next != nullptr) {

current = current->next;

currentPosition++;

if (currentPosition < position) {

cout << "Invalid position. Cannot delete element." << endl;

return;

Node* temp = current;

current->prev->next = current->next;

if (current->next != nullptr) {

current->next->prev = current->prev;
delete temp;

cout << "Deleted at position " << position << endl;

void updateElement(int idNo, double newSalary) {

if (head == nullptr) {

cout << "Doubly linked list is empty. Cannot update element." << endl;

return;

Node* current = head;

while (current != nullptr) {

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;

void searchElement(int idNo) {


if (head == nullptr) {

cout << "Doubly linked list is empty. Cannot search element." << endl;

return;

Node* current = head;

while (current != nullptr) {

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;

Node* createNode(string name, int idNo, double salary) {

Node* newNode = new Node;

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) {

cout << "\nMenu:" << endl;

cout << "A. Insert at beginning" << endl;

cout << "B. Insert at last (end)" << endl;

cout << "C. Insert at any location" << endl;

cout << "D. Display all elements" << endl;

cout << "E. Delete at beginning" << endl;

cout << "F. Delete at last (end)" << endl;

cout << "G. Delete at any location" << endl;

cout << "H. Update element" << endl;

cout << "I. Search element" << endl;

cout << "J. Exit" << endl;


cout << "Enter your choice (A/B/C/D/E/F/G/H/I/J): ";

cin >> choice;

switch (toupper(choice)) {

case 'A':

cout << "Enter the name: ";

cin >> name;

cout << "Enter the IdNo: ";

cin >> idNo;

cout << "Enter the salary: ";

cin >> salary;

doublyLinkedList.insertAtBeginning(name, idNo, salary);

break;

case 'B':

cout << "Enter the name: ";

cin >> name;

cout << "Enter the IdNo: ";

cin >> idNo;

cout << "Enter the salary: ";

cin >> salary;

doublyLinkedList.insertAtEnd(name, idNo, salary);

break;
case 'C':

cout << "Enter the name: ";

cin >> name;

cout << "Enter the IdNo: ";

cin >> idNo;

cout << "Enter the salary: ";

cin >> salary;

cout << "Enter the position: ";

cin >> position;

doublyLinkedList.insertAtLocation(name, idNo, salary, position);

break;

case 'D':

doublyLinkedList.display();

break;

case 'E':

doublyLinkedList.deleteAtBeginning();

break;

case 'F':

doublyLinkedList.deleteAtEnd();

break;

case 'G':
cout << "Enter the position: ";

cin >> position;

doublyLinkedList.deleteAtLocation(position);

break;

case 'H':

cout << "Enter the IdNo of the element to update: ";

cin >> idNo;

cout << "Enter the new salary: ";

cin >> salary;

doublyLinkedList.updateElement(idNo, salary);

break;

case 'I':

cout << "Enter the IdNo of the element to search: ";

cin >> idNo;

doublyLinkedList.searchElement(idNo);

break;

case 'J':

cout << "Exiting the system..." << endl;

exit(0);

default:

cout << "Invalid choice. Please enter a valid option." << endl;
break;

return 0;

2.

#include <iostream>

using namespace std;

struct Node {

int data;

Node* next;

};

class Stack {

private:

Node* top;

public:

Stack() {

top = nullptr;

}
void push(int element) {

Node* newNode = new Node;

newNode->data = element;

newNode->next = top;

top = newNode;

cout << "Pushed element: " << element << endl;

void pop() {

if (isEmpty()) {

cout << "Stack is empty. Cannot pop element." << endl;

return;

Node* temp = top;

int poppedElement = temp->data;

top = top->next;

delete temp;

cout << "Popped element: " << poppedElement << endl;

void display() {

if (isEmpty()) {

cout << "Stack is empty." << endl;

return;
}

cout << "Elements of the stack:" << endl;

Node* current = top;

while (current != nullptr) {

cout << current->data << endl;

current = current->next;

void peek() {

if (isEmpty()) {

cout << "Stack is empty." << endl;

return;

cout << "Top element: " << top->data << endl;

bool isEmpty() {

return top == nullptr;

};

int main() {
Stack stack;

char choice;

int element;

while (true) {

cout << "\nMenu:" << endl;

cout << "A. Push element" << endl;

cout << "B. Pop element" << endl;

cout << "C. Display all elements of stack" << endl;

cout << "D. Peek top element" << endl;

cout << "E. Exit" << endl;

cout << "Enter your choice (A/B/C/D/E): ";

cin >> choice;

switch (toupper(choice)) {

case 'A':

cout << "Enter the element to push: ";

cin >> element;

stack.push(element);

break;

case 'B':

stack.pop();
break;

case 'C':

stack.display();

break;

case 'D':

stack.peek();

break;

case 'E':

cout << "Exiting the system..." << endl;

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;

void enqueue(int element) {

Node* newNode = new Node;

newNode->data = element;

newNode->next = nullptr;

if (rear == nullptr) {

front = rear = newNode;


} else {

rear->next = newNode;

rear = newNode;

cout << "Enqueued element: " << element << endl;

void dequeue() {

if (isEmpty()) {

cout << "Queue is empty. Cannot dequeue element." << endl;

return;

Node* temp = front;

int dequeuedElement = temp->data;

front = front->next;

delete temp;

if (front == nullptr) {

rear = nullptr;

cout << "Dequeued element: " << dequeuedElement << endl;

}
void display() {

if (isEmpty()) {

cout << "Queue is empty." << endl;

return;

cout << "Elements of the queue:" << endl;

Node* current = front;

while (current != nullptr) {

cout << current->data << endl;

current = current->next;

void peek() {

if (isEmpty()) {

cout << "Queue is empty." << endl;

return;

cout << "Front element: " << front->data << endl;

bool isEmpty() {
return front == nullptr;

};

int main() {

Queue queue;

char choice;

int element;

while (true) {

cout << "\nMenu:" << endl;

cout << "A. Enqueue element" << endl;

cout << "B. Dequeue element" << endl;

cout << "C. Display all elements of queue" << endl;

cout << "D. Peek front element" << endl;

cout << "E. Exit" << endl;

cout << "Enter your choice (A/B/C/D/E): ";

cin >> choice;

switch (toupper(choice)) {

case 'A':

cout << "Enter the element to enqueue: ";

cin >> element;


queue.enqueue(element);

break;

case 'B':

queue.dequeue();

break;

case 'C':

queue.display();

break;

case 'D':

queue.peek();

break;

case 'E':

cout << "Exiting the system..." << endl;

exit(0);

default:

cout << "Invalid choice. Please enter a valid option." << endl;

break;

}
return 0;

You might also like