0% found this document useful (0 votes)
3 views

DSA Practical File

Uploaded by

bhada6877
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

DSA Practical File

Uploaded by

bhada6877
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

Institute of Technology and Management Gwalior

Department of CSE-Data Science

Practical File

Subject Name: Data Structure

Subject Code: CD 303

Year/Semester: 2nd Year/ 3rd Semester

Submitted to: Submitted by:


Dr. Jitendra Singh Kushwah Raunak bhadauriya
Sujal Jain (0905CD231066)
Associate Professor, ITM Gwalior
Institute of Technology and Management Gwalior
Department of CSE-Data Science
Continuous Assessment Sheet (CAS)

Academic Year: 2024-25 Subject and Code: Data Structure CD-303


Raunak
Name of Student: Sujal bhadauriya
Jain Enrollment No.: 0905CD231066
0905CD231059

Oral and Report Writing (10)


Concept Understanding (10)

Program Execution (10)

Date of Submission
Total (30)
Exp. Date Title of Experiment Sign. of
No. Faculty

1 11.09.2024 Write a program (WAP) to demon-


strate the working of Traversing. Inser-
tion at specified Location and deletion
in an Array
2 13.09.2024 WAP to search an element in the array
using Linear Search
3 23.09.2024 WAP to search an element in the array
using Binary Search
4 25.09.2024 WAP to demonstrate the working of
Bubble Sort
5 09.10.2024 WAP to demonstrate the working of In-
sertion Sort
6 16.10.2024 WAP to demonstrate the working of Se-
lection Sort
7 21.10.2024 WAP to demonstrate the working of
Matrix Multiplication
Oral and Report Writing (10)
Concept Understanding (10)

Program Execution (10)

Date of Submission
Total (30)
Exp. Date Title of Experiment Sign. of
No. Faculty

8 13.11.2024 Write a menu driven program


to implement following opera-
tions on the singly linked list
ˆ Insert a node at the front
ˆ Insert a node at the end
ˆ Insert a node such that linked
list is in ascending order
9 20.11.2024 write a menu driven pro-
gram to implement following
ˆ Delete first node of the linked list
ˆ Delete a node before specified position
ˆ Delete a node after specified position
10 27.11.2024 WAP to add two Polynomials using
Linked List
11 16.12.2024 WAP to demonstrate stack that per-
forms following operations using array
ˆPUSH
ˆPOP
12 27.12.2024 Write a C++ program that uses stack
operations to convert a given infix ex-
pression into its postfix Equivalent, Im-
plement the stack using an array
13 30.12.2024 WAP to implement QUEUE using ar-
rays that performs following operations
ˆINSERT
ˆDELETE
ˆDISPLAY
06.01.2025 WAP to implement Tree Traversals on
14
Binary Trees
15 08.01.2025 Write a program that perform opera-
tions on Doubly Linked List
1 Write a program (WAP) to demonstrate the working of Travers-
ing. Insertion at specified Location and deletion in an Array

#include <iostream.h>
#include <conio.h>
void main()
{
int LA[100], item, k, i, n, j;
clrscr();
cout << "Enter the size of the array: ";
cin >> n;
cout << "Enter the elements of the array:" << endl;
for (i = 0; i < n; i++)
{
cin >> LA[i];
}
cout << "The original array elements are:" << endl;
for (i = 0; i < n; i++)
{
cout << "LA[" << i << "] = " << LA[i] << endl;
}
cout << "Enter the location where the element is to be inserted: "
cin >> k;
cout << "Enter the element to be inserted: ";
cin >> item;
for (j = n - 1; j >= k; j--)
{
LA[j + 1] = LA[j];
}
LA[k] = item;
n++;
cout << "Array elements after insertion:" << endl;
for (i = 0; i < n; i++)
{
cout << "LA[" << i << "] = " << LA[i] << endl;
}
cout << "Enter the location of the element to be deleted: ";
cin >> k;
for (i = k; i < n - 1; i++) {
LA[i] = LA[i + 1];
}
n--;
cout << "Array elements after deletion:" << endl;
for (i = 0; i < n; i++) {
cout << "LA[" << i << "] = " << LA[i] << endl;
}
getch();
}

Output:
2 WAP to search an element in the array using Linear Search.

#include<iostream.h>
#include<conio.h>

void main() {
clrscr();
int arr[10], n, key, i, found = 0;

cout << "Enter the number of elements in the array (max 10): ";
cin >> n;

cout << "Enter " << n << " elements:" << endl;
for(i = 0; i < n; i++) {
cin >> arr[i];
}

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


cin >> key;

for(i = 0; i < n; i++) {


if(arr[i] == key) {
cout << "Element " << key << " found at index " << i << endl;
found = 1;
break;
}
}

if(!found) {
cout << "Element " << key << " not found in the array." << endl;
}
getch();
}
Output:
3 WAP to search an element in the array using Binary Search.

#include <iostream>
using namespace std;
int main()
{
int a[5], i, n,item,mid,beg,end;
cout << "How many values you want to enter:";
cin >> n;
cout <<"enter" <<n<<" values:";
for (i=0; i<5;i++)
{
cin>> a[i];
}
cout <<"array is";
for (i=0; i<5; i++)
{
cout<<a[i]<<" ";
}
cout<<" enter item=";
cin>> item;
{
int beg=0, end=n-1, mid;
mid = int((beg + end)/2);
while ((beg <= end) && a[mid]!= item)
{
if (a[mid] <item)
beg = mid + 1;
else end = mid-1;
mid = int ((beg+mid)/2);
}
if (a[mid] = item)
cout << " element found at" << mid;
else
cout<<" element not found!";
return 0;
}
}

Output:
4 WAP to demonstrate the working of Bubble Sort.

#include<iostream.h>
#include<conio.h>

void main() {
clrscr();
int arr[10], n, i, j, temp;

cout << "Enter the number of elements: ";


cin >> n;

cout << "Enter " << n << " elements:" << endl;
for(i = 0; i < n; i++) {
cin >> arr[i];
}

for(i = 0; i < n-1; i++) {


for(j = 0; j < n-i-1; j++) {
if(arr[j] > arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}

cout << "Sorted array: ";


for(i = 0; i < n; i++) {
cout << arr[i] << " ";
}

getch();
}
Output:
5 WAP to demonstrate the working of Insertion Sort.

#include <iostream.h>
#include <conio.h>

void insertionSort(int arr[], int n) {


for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

void main() {
clrscr();
int n;
cout << "Enter number of elements: ";
cin >> n;
int arr[n];
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
insertionSort(arr, n);
cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
getch();
}
Output:
6 WAP to demonstrate the working of Selection Sort.

#include <iostream.h>
#include <conio.h>

void selectionSort(int arr[], int n) {


for (int i = 0; i < n - 1; i++) {
int minIdx = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIdx]) {
minIdx = j;
}
}
int temp = arr[i];
arr[i] = arr[minIdx];
arr[minIdx] = temp;
}
}
void main() {
clrscr();
int n;
cout << "Enter number of elements: ";
cin >> n;
int arr[n];
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
selectionSort(arr, n);
cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
getch(); }
Output:
7 WAP to demonstrate the working of Matrix Multiplication.

#include <iostream.h>

#include <conio.h>

void multiplyMatrices(int A[10][10], int B[10][10], int result[10


for (int i = 0; i < rowA; i++) {
for (int j = 0; j < colB; j++) {
result[i][j] = 0;
for (int k = 0; k < colA; k++) {
result[i][j] += A[i][k] * B[k][j];
}
}
}
}

void main() {
clrscr();
int A[10][10], B[10][10], result[10][10];
int rowA, colA, rowB, colB;

cout << "Enter number of rows and columns of matrix A: ";


cin >> rowA >> colA;

cout << "Enter number of rows and columns of matrix B: ";


cin >> rowB >> colB;

if (colA != rowB) {
cout << "Matrix multiplication is not possible. Number of colum
getch();
return;
}
cout << "Enter elements of matrix A:\n";
for (int i = 0; i < rowA; i++) {
for (int j = 0; j < colA; j++) {
cin >> A[i][j];
}
}

cout << "Enter elements of matrix B:\n";


for (int i = 0; i < rowB; i++) {
for (int j = 0; j < colB; j++) {
cin >> B[i][j];
}
}

multiplyMatrices(A, B, result, rowA, colA, rowB, colB);

cout << "Result of matrix multiplication:\n";


for (int i = 0; i < rowA; i++) {
for (int j = 0; j < colB; j++) {
cout << result[i][j] << " ";
}
cout << endl;
}

getch();
}
Output:
8 Write a menu driven program to implement following operations
on the singly linked list.
ˆ Insert a node at the front
ˆ Insert a node at the end
ˆ Insert a node such that linked list is in ascending order

#include <iostream.h>
#include <conio.h>

struct Node {
int data;
Node* next;
};

Node* head = NULL;

void insertAtFront(int value) {


Node* newNode = new Node();
newNode->data = value;
newNode->next = head;
head = newNode;
}

void insertAtEnd(int value) {


Node* newNode = new Node();
newNode->data = value;
newNode->next = NULL;

if (head == NULL) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}

void insertInAscendingOrder(int value) {


Node* newNode = new Node();
newNode->data = value;

if (head == NULL || head->data >= value) {


newNode->next = head;
head = newNode;
} else {
Node* temp = head;
while (temp->next != NULL && temp->next->data < value) {
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
}
}

void displayList() {
Node* temp = head;
if (temp == NULL) {
cout << "List is empty." << endl;
return;
}
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
void main() {
clrscr();
int choice, value;

while (1) {
cout << "\nMenu: ";
cout << "\n1. Insert a node at the front";
cout << "\n2. Insert a node at the end";
cout << "\n3. Insert a node in ascending order";
cout << "\n4. Display the list";
cout << "\n5. Exit";
cout << "\nEnter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter the value to insert at the front: ";
cin >> value;
insertAtFront(value);
break;
case 2:
cout << "Enter the value to insert at the end: ";
cin >> value;
insertAtEnd(value);
break;
case 3:
cout << "Enter the value to insert in ascending order: ";
cin >> value;
insertInAscendingOrder(value);
break;
case 4:
displayList();
break;
case 5:
cout << "Exiting..." << endl;
getch();
return;
default:
cout << "Invalid choice, please try again." << endl;
}
}
}
Output:
9 Write a menu driven program to implement following.
ˆDelete first node of the linked list
ˆDelete a node before specified position
ˆDelete a node after specified position.

#include <iostream.h>
#include <conio.h>

struct Node {
int data;
Node* next;
};

Node* head = NULL;

void insertAtEnd(int value) {


Node* newNode = new Node();
newNode->data = value;
newNode->next = NULL;

if (head == NULL) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}

void deleteFirstNode() {
if (head == NULL) {
cout << "List is empty!" << endl;
return;
}
Node* temp = head;
head = head->next;
delete temp;
cout << "First node deleted successfully." << endl;
}

void deleteBeforeNode(int position) {


if (head == NULL || position <= 1) {
cout << "Cannot delete node before specified position." << endl
return;
}

Node* temp = head;


Node* prev = NULL;

// Traverse to the node just before the given position


for (int i = 1; i < position - 1 && temp != NULL; i++) {
prev = temp;
temp = temp->next;
}

if (temp == NULL || temp->next == NULL) {


cout << "Invalid position!" << endl;
return;
}

Node* nodeToDelete = temp;


prev->next = temp->next;
delete nodeToDelete;
cout << "Node before position " << position << " deleted success
}
void deleteAfterNode(int position) {
if (head == NULL) {
cout << "List is empty!" << endl;
return;
}

Node* temp = head;

// Traverse to the node at the specified position


for (int i = 1; i < position && temp != NULL; i++) {
temp = temp->next;
}

if (temp == NULL || temp->next == NULL) {


cout << "Invalid position or no node after the specified positi
return;
}

Node* nodeToDelete = temp->next;


temp->next = nodeToDelete->next;
delete nodeToDelete;
cout << "Node after position " << position << " deleted successf
}

void displayList() {
Node* temp = head;
if (temp == NULL) {
cout << "List is empty." << endl;
return;
}

while (temp != NULL) {


cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}

void main() {
clrscr();
int choice, value, position;

// Example of inserting nodes to start with


insertAtEnd(10);
insertAtEnd(20);
insertAtEnd(30);
insertAtEnd(40);

while (1) {
cout << "\nMenu: ";
cout << "\n1. Delete the first node";
cout << "\n2. Delete a node before a specified position";
cout << "\n3. Delete a node after a specified position";
cout << "\n4. Display the list";
cout << "\n5. Exit";
cout << "\nEnter your choice: ";
cin >> choice;

switch (choice) {
case 1:
deleteFirstNode();
break;
case 2:
cout << "Enter the position before which you want to delete a
cin >> position;
deleteBeforeNode(position);
break;
case 3:
cout << "Enter the position after which you want to delete a n
cin >> position;
deleteAfterNode(position);
break;
case 4:
displayList();
break;
case 5:
cout << "Exiting..." << endl;
getch();
return;
default:
cout << "Invalid choice, please try again." << endl;
}
}
}
Output:
10 WAP to add two Polynomials using Linked List.

#include<iostream.h>
#include<conio.h>

struct Node {
int coefficient;
int exponent;
Node* next;
};

Node* createNode(int coeff, int exp) {


Node* newNode = new Node;
newNode->coefficient = coeff;
newNode->exponent = exp;
newNode->next = NULL;
return newNode;
}

void insertNode(Node*& head, int coeff, int exp) {


Node* newNode = createNode(coeff, exp);
if (head == NULL) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}

void displayPolynomial(Node* head) {


Node* temp = head;
while (temp != NULL) {
if (temp->coefficient >= 0 && temp != head) {
cout << "+";
}
cout << temp->coefficient << "x^" << temp->exponent << " ";
temp = temp->next;
}
cout << endl;
}

Node* addPolynomials(Node* poly1, Node* poly2) {


Node* result = NULL;
Node *ptr1 = poly1, *ptr2 = poly2;
while (ptr1 != NULL && ptr2 != NULL) {
if (ptr1->exponent > ptr2->exponent) {
insertNode(result, ptr1->coefficient, ptr1->exponent);
ptr1 = ptr1->next;
} else if (ptr1->exponent < ptr2->exponent) {
insertNode(result, ptr2->coefficient, ptr2->exponent);
ptr2 = ptr2->next;
} else {
int sumCoeff = ptr1->coefficient + ptr2->coefficient;
if (sumCoeff != 0) {
insertNode(result, sumCoeff, ptr1->exponent);
}
ptr1 = ptr1->next;
ptr2 = ptr2->next;
}
}
while (ptr1 != NULL) {
insertNode(result, ptr1->coefficient, ptr1->exponent);
ptr1 = ptr1->next;
}
while (ptr2 != NULL) {
insertNode(result, ptr2->coefficient, ptr2->exponent);
ptr2 = ptr2->next;
}
return result;
}

void main() {
clrscr();
Node *poly1 = NULL, *poly2 = NULL;
insertNode(poly1, 3, 3);
insertNode(poly1, 4, 2);
insertNode(poly1, 2, 1);
insertNode(poly1, 1, 0);
insertNode(poly2, 5, 3);
insertNode(poly2, 2, 2);
insertNode(poly2, 6, 0);
cout << "Polynomial 1: ";
displayPolynomial(poly1);
cout << "Polynomial 2: ";
displayPolynomial(poly2);

Node* result = addPolynomials(poly1, poly2);


cout << "Sum of Polynomials: ";
displayPolynomial(result);

getch();
}

Output:
11 WAP to demonstrate stack that performs following operations
using array.
ˆPUSH
ˆPOP

#include<iostream.h>
#include<conio.h>

#define MAX 5

class Stack {
int arr[MAX];
int top;
public:
Stack() { top = -1; }
void push(int value) {
if (top == MAX - 1) cout << "Stack Overflow! Unable to push " << value << end
else { arr[++top] = value; cout << "Pushed " << value << " onto the stack." <
}
void pop() {
if (top == -1) cout << "Stack Underflow! No elements to pop." << endl;
else { cout << "Popped " << arr[top--] << " from the stack." << endl; }
}
void display() {
if (top == -1) cout << "Stack is empty." << endl;
else { cout << "Stack elements: "; for (int i = top; i >= 0; i--) cout << arr
}
};

void main() {
clrscr();
Stack s;
s.push(10); s.push(20); s.push(30);
s.display();
s.pop();
s.display();
s.push(40); s.push(50); s.push(60);
s.display();
s.pop();
s.display();
getch();
}

Output:
12 Write a C++ program that uses stack operations to convert a
given infix expression into its postfix Equivalent, Implement the
stack using an array.

#include <iostream.h>
#include <conio.h>
#include <ctype.h>

#define MAX 50

class Stack {
char arr[MAX];
int top;
public:
Stack() { top = -1; }

void push(char c) {
if (top == MAX - 1) {
cout << "Stack Overflow!" << endl;
} else {
top++;
arr[top] = c;
}
}

char pop() {
if (top == -1) {
return ’\0’;
} else {
char temp = arr[top];
top--;
return temp;
}
}
char peek() {
if (top == -1) return ’\0’;
return arr[top];
}

bool isEmpty() { return top == -1; }


};

int getPrecedence(char c) {
if (c == ’+’ || c == ’-’) return 1;
if (c == ’*’ || c == ’/’) return 2;
if (c == ’^’) return 3;
return 0;
}

bool isOperand(char c) {
return isalpha(c) || isdigit(c);
}

void infixToPostfix(char* infix, char* postfix) {


Stack s;
int k = 0;

for (int i = 0; infix[i] != ’\0’; i++) {


char current = infix[i];

if (isOperand(current)) {
postfix[k++] = current;
}

else if (current == ’(’) {


s.push(current);
}
else if (current == ’)’) {
while (!s.isEmpty() && s.peek() != ’(’) {
postfix[k++] = s.pop();
}
s.pop();
}
else {
while (!s.isEmpty() && getPrecedence(s.peek()) >= getPrecedence
postfix[k++] = s.pop();
}
s.push(current);
}
}
while (!s.isEmpty()) {
postfix[k++] = s.pop();
}

postfix[k] = ’\0’;
}

void main() {
clrscr();

char infix[MAX], postfix[MAX];


cout << "Enter infix expression: ";
cin >> infix;

infixToPostfix(infix, postfix);

cout << "Postfix expression: " << postfix << endl;

getch();
}
Output:
13 WAP to implement QUEUE using arrays that performs follow-
ing operations.
ˆINSERT
ˆDELETE
ˆDISPLAY

#include <iostream.h>
#include <conio.h>

#define MAX 5

class Queue {
int arr[MAX];
int front, rear;
public:
Queue() {
front = -1;
rear = -1;
}

void insert(int value) {


if (rear == MAX - 1) {
cout << "Queue is full!" << endl;
} else {
if (front == -1) front = 0;
rear++;
arr[rear] = value;
}
}

void deleteQueue() {
if (front == -1) {
cout << "Queue is empty!" << endl;
} else {
cout << "Deleted element: " << arr[front] << endl;
front++;
if (front > rear) {
front = rear = -1;
}
}
}

void display() {
if (front == -1) {
cout << "Queue is empty!" << endl;
} else {
cout << "Queue elements are: ";
for (int i = front; i <= rear; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
}
};

void main() {
clrscr();
Queue q;
int choice, value;

do {
cout << "1. Insert" << endl;
cout << "2. Delete" << endl;
cout << "3. Display" << endl;
cout << "4. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value to insert: ";
cin >> value;
q.insert(value);
break;
case 2:
q.deleteQueue();
break;
case 3:
q.display();
break;
case 4:
break;
default:
cout << "Invalid choice!" << endl;
}
} while (choice != 4);

getch();
}
Output:
14 WAP to implement Tree Traversals on Binary Trees.

#include <iostream.h>
#include <conio.h>

struct Node {
int data;
Node* left;
Node* right;

Node(int val) {
data = val;
left = right = NULL;
}
};

class BinaryTree {
public:
Node* root;

BinaryTree() {
root = NULL;
}

void inorder(Node* node) {


if (node == NULL)
return;
inorder(node->left);
cout << node->data << " ";
inorder(node->right);
}

void preorder(Node* node) {


if (node == NULL)
return;
cout << node->data << " ";
preorder(node->left);
preorder(node->right);
}

void postorder(Node* node) {


if (node == NULL)
return;
postorder(node->left);
postorder(node->right);
cout << node->data << " ";
}

void inorderTraversal() {
inorder(root);
}

void preorderTraversal() {
preorder(root);
}

void postorderTraversal() {
postorder(root);
}

Node* insertNode(Node* node, int value) {


if (node == NULL) {
return new Node(value);
}

char choice;
cout << "Insert left or right of " << node->data << "? (l/r): ";
cin >> choice;
if (choice == ’l’ || choice == ’L’) {
node->left = insertNode(node->left, value);
} else if (choice == ’r’ || choice == ’R’) {
node->right = insertNode(node->right, value);
} else {
cout << "Invalid choice!" << endl;
}

return node;
}
};

void main() {
clrscr();

BinaryTree tree;
int value;
char more;

cout << "Enter the root node value: ";


cin >> value;
tree.root = new Node(value);

do {
cout << "Enter value to insert: ";
cin >> value;
tree.insertNode(tree.root, value);
cout << "Do you want to insert more nodes? (y/n): ";
cin >> more;
} while (more == ’y’ || more == ’Y’);

cout << "\nInorder Traversal: ";


tree.inorderTraversal();
cout << endl;

cout << "Preorder Traversal: ";


tree.preorderTraversal();
cout << endl;

cout << "Postorder Traversal: ";


tree.postorderTraversal();
cout << endl;

getch();
}
Output:
15 Write a program that perform operations on Doubly Linked List.

#include <iostream.h>
#include <conio.h>

struct Node {
int data;
Node* prev;
Node* next;

Node(int val) {
data = val;
prev = next = NULL;
}
};

class DoublyLinkedList {
public:
Node* head;

DoublyLinkedList() {
head = NULL;
}

void insertAtBeginning(int value) {


Node* newNode = new Node(value);
if (head == NULL) {
head = newNode;
} else {
newNode->next = head;
head->prev = newNode;
head = newNode;
}
}
void insertAtEnd(int value) {
Node* newNode = new Node(value);
if (head == NULL) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
}

void deleteFromBeginning() {
if (head == NULL) {
cout << "List is empty!" << endl;
return;
} else {
Node* temp = head;
head = head->next;
if (head != NULL) {
head->prev = NULL;
}
delete temp;
}
}

void deleteFromEnd() {
if (head == NULL) {
cout << "List is empty!" << endl;
return;
} else {
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
if (temp->prev != NULL) {
temp->prev->next = NULL;
}
delete temp;
}
}

void display() {
if (head == NULL) {
cout << "List is empty!" << endl;
return;
} else {
Node* temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
}

void displayReverse() {
if (head == NULL) {
cout << "List is empty!" << endl;
return;
} else {
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->prev;
}
cout << endl;
}
}
};

void main() {
clrscr();

DoublyLinkedList dll;
int choice, value;

do {
cout << "Menu: \n";
cout << "1. Insert at the beginning\n";
cout << "2. Insert at the end\n";
cout << "3. Delete from the beginning\n";
cout << "4. Delete from the end\n";
cout << "5. Display the list\n";
cout << "6. Display the list in reverse\n";
cout << "7. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter value to insert at the beginning: ";
cin >> value;
dll.insertAtBeginning(value);
break;
case 2:
cout << "Enter value to insert at the end: ";
cin >> value;
dll.insertAtEnd(value);
break;
case 3:
dll.deleteFromBeginning();
break;
case 4:
dll.deleteFromEnd();
break;
case 5:
dll.display();
break;
case 6:
dll.displayReverse();
break;
case 7:
break;
default:
cout << "Invalid choice! Try again." << endl;
}
} while (choice != 7);

getch();
}
Output:

You might also like