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

DSA lab file copy

This document outlines a lab file for data structures course ENCS 251, detailing various programming tasks to be implemented in C/C++. It includes operations on arrays, stacks, queues, linked lists, sorting algorithms, searching algorithms, binary search trees, and graphs. Each task requires designing and developing menu-driven programs to perform specific data structure operations.

Uploaded by

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

DSA lab file copy

This document outlines a lab file for data structures course ENCS 251, detailing various programming tasks to be implemented in C/C++. It includes operations on arrays, stacks, queues, linked lists, sorting algorithms, searching algorithms, binary search trees, and graphs. Each task requires designing and developing menu-driven programs to perform specific data structure operations.

Uploaded by

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

LAB FILE

DATA STRUCTURES
(ENCS 251)

SUBMIITED BY: SUBMITTED TO:


SONU BHARTI MS. SUMAN
ROLL NUMBER: 2301010323
COURSE: BTECH CSE CORE
SECTION: E

School of Engineering & Technology


K. R. MANGALAM UNIVERSITY
Sohna, Haryana 122103, India
Page |1

INDEX

S.NO PROGRAM DATE SIGNATURE


1. Design, Develop and Implement a menu driven Program in C++
for the following Array operations
a. Creating an Array of N Integer Elements
b. Display of Array Elements
c. Inserting an Element at a given valid Position (POS)
d. Deleting an Element at a given valid Position (POS)
e. Exit.

2. Design, Develop and Implement a menu driven Program in C++


for the following operations on STACK of Integers (Array
Implementation of Stack with maximum size MAX)
a. Push an Element on to Stack
b. Pop an Element from Stack
c. Demonstrate how Stack can be used to check Palindrome
d. Demonstrate Overflow and Underflow situations on Stack
e. Display the status of Stack
f. Exit

3. Design, Develop and Implement a Program in C/C++ for


converting an Infix Expression to Postfix Expression. Program
should support for both parenthesized and free parenthesized
expressions with the operators: +, -, *, /, % (Remainder), ^
(Power) and alphanumeric operands.

4. Design, Develop and Implement a menu driven Program in C/C++


for the following operations on QUEUE of Characters
a. Insert an Element on to Linear QUEUE
b. Delete an Element from Linear QUEUE

5. Design, Develop and Implement a menu driven Program in C/C++


for the following operations on Circular QUEUE of Characters
a. Insert an Element on to Circular QUEUE
b. Delete an Element from Circular QUEUE
c. Demonstrate Overflow and Underflow situations on Circular
QUEUE
d. Display

6. Design, Develop and Implement a menu driven Program in C/C++


for the following operations on Singly Linked List (SLL)
a. Create a SLL.
b. Insert at Beginning
c. Insert at Last
d. Insert at any random location
e. Delete from Beginning
f. Delete from Last
g. Delete node after specified location
h. Search for an element
i. Show
j. Exit
Page |2

7. Design, Develop and Implement the following menu driven


Programs in C/C++ using Array
operations
a. Write a program for Bubble Sort algorithm
b. Write a program for Merge Sort algorithm
c. Write a program for Heap Sort algorithm
b. Write a program for Quick Sort algorithm
d. Write a program for Insertion Sort algorithm
e. Write a program for Selection Sort algorithm

8. Design, Develop and Implement the following menu driven


Programs in C/C++ for
implementing
a. Write a program for linear search algorithm
b. Write a program for displaying a sparse matrix

9. Design, Develop and Implement a menu driven Program in C/C++


for the following
operations on Binary Search Tree (BST) of Integers
a. Create a BST of N Integers: 8, 10, 3, 1, 6, 14, 7
b. Traverse the BST in Inorder
c. Traverse the BST in Preorder
d. Traverse the BST in and Post Order

10. Design, Develop and Implement a Program in C/C++ for the


following operations on
Graph(G)
a. Create a Graph N using Adjacency Matrix.
b. Print all the nodes reachable from a given starting node in a
digraph using BFS method
c. Print all the nodes reachable from a given starting node in a
digraph using DFS method
Page |3

Ques 1: Design, Develop and Implement a menu driven Program in C++ for the following Array
operations
a. Creating an Array of N Integer Elements
b. Display of Array Elements
c. Inserting an Element at a given valid Position (POS)
d. Deleting an Element at a given valid Position (POS)
e. Exit.
Code:
#include <iostream>
using namespace std;
class ArrayOperations {
private:
int arr[100]; // You can define maximum size of the array
int n; // Number of elements in the array
public:
ArrayOperations() {
n = 0; // Initialize number of elements to 0
}
// Function to create an array with N elements
void createArray() {
cout << "Enter the number of elements (max 100): ";
cin >> n;
if (n > 100 || n <= 0) {
cout << "Invalid size! Please enter a value between 1 and 100.\n";
return;
}
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cout << "Array created successfully!\n";
}
// Function to display the array
void displayArray() {
if (n == 0) {
cout << "Array is empty!\n";
return;
}
cout << "Array elements: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
Page |4

cout << endl;


}
// Function to insert an element at a given position
void insertElement() {
if (n >= 100) {
cout << "Array is full, cannot insert more elements.\n";
return;
}
int pos, element;
cout << "Enter the position (1 to " << n + 1 << ") to insert an element: ";
cin >> pos;
if (pos < 1 || pos > n + 1) {
cout << "Invalid position!\n";
return;
}
cout << "Enter the element to insert: ";
cin >> element;

// Shift elements to the right


for (int i = n; i >= pos; i--) {
arr[i] = arr[i - 1];
}

arr[pos - 1] = element;
n++; // Increment the size of the array

cout << "Element inserted successfully!\n";


}
// Function to delete an element from a given position
void deleteElement() {
if (n == 0) {
cout << "Array is empty, nothing to delete!\n";
return;
}
int pos;
cout << "Enter the position (1 to " << n << ") to delete an element: ";
cin >> pos;

if (pos < 1 || pos > n) {


cout << "Invalid position!\n";
return;
}
Page |5

// Shift elements to the left


for (int i = pos - 1; i < n - 1; i++) {
arr[i] = arr[i + 1];
}

n--; // Decrement the size of the array


cout << "Element deleted successfully!\n";
}

// Function to exit the program


void exitProgram() {
cout << "Exiting the program. Goodbye!\n";
}
};
int main() {
ArrayOperations ao;
int choice;
do {
cout << "\n--- Array Operations Menu ---\n";
cout << "1. Create Array\n";
cout << "2. Display Array\n";
cout << "3. Insert Element\n";
cout << "4. Delete Element\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
ao.createArray();
break;
case 2:
ao.displayArray();
break;
case 3:
ao.insertElement();
break;
case 4:
ao.deleteElement();
break;
case 5:
ao.exitProgram();
Page |6

break;
default:
cout << "Invalid choice! Please select a valid option.\n";
}
} while (choice != 5);
return 0;
}
Output:
--- Array Operations Menu ---
1. Create Array
2. Display Array
3. Insert Element
4. Delete Element
5. Exit
Enter your choice: 1
Enter the number of elements (max 100): 5
Enter 5 elements: 10 20 30 40 50
Array created successfully!

--- Array Operations Menu ---


1. Create Array
2. Display Array
3. Insert Element
4. Delete Element
5. Exit
Enter your choice: 2
Array elements: 10 20 30 40 50

--- Array Operations Menu ---


1. Create Array
2. Display Array
3. Insert Element
4. Delete Element
5. Exit
Enter your choice: 3
Enter the position (1 to 6) to insert an element: 3
Enter the element to insert: 25
Element inserted successfully!

--- Array Operations Menu ---


1. Create Array
2. Display Array
Page |7

3. Insert Element
4. Delete Element
5. Exit
Enter your choice: 2
Array elements: 10 20 25 30 40 50

--- Array Operations Menu ---


1. Create Array
2. Display Array
3. Insert Element
4. Delete Element
5. Exit
Enter your choice: 4
Enter the position (1 to 6) to delete an element: 4
Element deleted successfully!

--- Array Operations Menu ---


1. Create Array
2. Display Array
3. Insert Element
4. Delete Element
5. Exit
Enter your choice: 2
Array elements: 10 20 25 40 50

--- Array Operations Menu ---


1. Create Array
2. Display Array
3. Insert Element
4. Delete Element
5. Exit
Enter your choice: 5
Exiting the program. Goodbye!

Ques 2: Design, Develop and Implement a menu driven Program in C++ for the following operations on
STACK of Integers (Array Implementation of Stack with maximum size MAX)
a. Push an Element on to Stack
b. Pop an Element from Stack
c. Demonstrate how Stack can be used to check Palindrome
d. Demonstrate Overflow and Underflow situations on Stack
e. Display the status of Stack
f. Exit
Page |8

Code:
#include <iostream>
using namespace std;

#define MAX 5 // Maximum size of the stack

class Stack {
private:
int arr[MAX]; // Array to hold the stack elements
int top; // Variable to track the top element

public:
// Constructor to initialize stack
Stack() : top(-1) {}

// Function to push an element onto the stack


void push(int element) {
if (top >= MAX - 1) {
cout << "Stack Overflow. Cannot push " << element << ".\n";
} else {
arr[++top] = element;
cout << "Pushed " << element << " onto the stack.\n";
}
}

// Function to pop an element from the stack


int pop() {
if (top < 0) {
cout << "Stack Underflow. No element to pop.\n";
return -1;
} else {
int poppedElement = arr[top--];
cout << "Popped " << poppedElement << " from the stack.\n";
return poppedElement;
}
}

// Function to display the stack status


void display() {
if (top < 0) {
cout << "Stack is empty.\n";
} else {
Page |9

cout << "Stack elements are: ";


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

// Function to check if a string is a palindrome using a stack


void checkPalindrome(string str) {
int len = str.length();
Stack tempStack; // Temporary stack for palindrome check

// Push all characters onto the stack


for (int i = 0; i < len; i++) {
tempStack.push(str[i]);
}

// Pop characters and compare with original string


bool isPalindrome = true;
for (int i = 0; i < len; i++) {
if (str[i] != tempStack.pop()) {
isPalindrome = false;
break;
}
}

if (isPalindrome) {
cout << str << " is a Palindrome.\n";
} else {
cout << str << " is not a Palindrome.\n";
}
}

// Function to check for overflow


void demonstrateOverflow() {
cout << "Attempting to push " << (MAX + 1) << " elements to demonstrate overflow...\n";
for (int i = 0; i < MAX + 1; i++) {
push(i + 1);
}
}
P a g e | 10

// Function to check for underflow


void demonstrateUnderflow() {
cout << "Attempting to pop " << (MAX + 1) << " elements to demonstrate underflow...\n";
for (int i = 0; i < MAX + 1; i++) {
pop();
}
}
};

// Main function with menu


int main() {
Stack stack;
int choice, element;
string str;

do {
cout << "\nMenu:\n";
cout << "1. Push an Element onto Stack\n";
cout << "2. Pop an Element from Stack\n";
cout << "3. Check if a String is a Palindrome\n";
cout << "4. Demonstrate Overflow\n";
cout << "5. Demonstrate Underflow\n";
cout << "6. Display the Stack\n";
cout << "7. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter an element to push: ";
cin >> element;
stack.push(element);
break;

case 2:
stack.pop();
break;

case 3:
cout << "Enter a string to check palindrome: ";
cin >> str;
P a g e | 11

stack.checkPalindrome(str);
break;

case 4:
stack.demonstrateOverflow();
break;

case 5:
stack.demonstrateUnderflow();
break;

case 6:
stack.display();
break;

case 7:
cout << "Exiting...\n";
break;

default:
cout << "Invalid choice. Please try again.\n";
break;
}
} while (choice != 7);

return 0;
}
Output:
Menu:
1. Push an Element onto Stack
2. Pop an Element from Stack
3. Check if a String is a Palindrome
4. Demonstrate Overflow
5. Demonstrate Underflow
6. Display the Stack
7. Exit
Enter your choice:
Enter your choice: 1
Enter an element to push: 10
Pushed 10 onto the stack.

Menu:
P a g e | 12

1. Push an Element onto Stack


2. Pop an Element from Stack
3. Check if a String is a Palindrome
4. Demonstrate Overflow
5. Demonstrate Underflow
6. Display the Stack
7. Exit
Enter your choice:
Enter your choice: 1
Enter an element to push: 20
Pushed 20 onto the stack.

Menu:
1. Push an Element onto Stack
2. Pop an Element from Stack
3. Check if a String is a Palindrome
4. Demonstrate Overflow
5. Demonstrate Underflow
6. Display the Stack
7. Exit
Enter your choice:
Enter your choice: 6
Stack elements are: 10 20

Menu:
1. Push an Element onto Stack
2. Pop an Element from Stack
3. Check if a String is a Palindrome
4. Demonstrate Overflow
5. Demonstrate Underflow
6. Display the Stack
7. Exit
Enter your choice:
Enter your choice: 2
Popped 20 from the stack.

Menu:
1. Push an Element onto Stack
2. Pop an Element from Stack
3. Check if a String is a Palindrome
4. Demonstrate Overflow
5. Demonstrate Underflow
P a g e | 13

6. Display the Stack


7. Exit
Enter your choice:
Enter your choice: 6
Stack elements are: 10

Menu:
1. Push an Element onto Stack
2. Pop an Element from Stack
3. Check if a String is a Palindrome
4. Demonstrate Overflow
5. Demonstrate Underflow
6. Display the Stack
7. Exit
Enter your choice:
Enter your choice: 3
Enter a string to check palindrome: madam
madam is a Palindrome.

Menu:
1. Push an Element onto Stack
2. Pop an Element from Stack
3. Check if a String is a Palindrome
4. Demonstrate Overflow
5. Demonstrate Underflow
6. Display the Stack
7. Exit
Enter your choice:
Enter your choice: 4
Attempting to push 6 elements to demonstrate overflow...
Pushed 1 onto the stack.
Pushed 2 onto the stack.
Pushed 3 onto the stack.
Pushed 4 onto the stack.
Pushed 5 onto the stack.
Stack Overflow. Cannot push 6.

Menu:
1. Push an Element onto Stack
2. Pop an Element from Stack
3. Check if a String is a Palindrome
4. Demonstrate Overflow
P a g e | 14

5. Demonstrate Underflow
6. Display the Stack
7. Exit
Enter your choice:
Enter your choice: 7
Exiting…

3. Design, Develop and Implement a Program in C/C++ for converting an Infix Expression to
Postfix Expression. Program should support for both parenthesized and free parenthesized
expressions with the operators: +, -, *, /, % (Remainder), ^ (Power) and alphanumeric
operands.

Code:
#include <iostream>
#include <stack>
#include <cctype> // For isalnum function
#include <cmath> // For pow function (if needed in evaluation)

using namespace std;

// Function to determine operator precedence


int precedence(char op) {
if (op == '^') return 3;
if (op == '*' || op == '/' || op == '%') return 2;
if (op == '+' || op == '-') return 1;
return 0;
}

// Function to check if the operator is right associative


bool isRightAssociative(char op) {
return (op == '^');
}

// Function to check if the given character is an operator


bool isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/' || c == '%' || c == '^');
}

// Function to convert infix expression to postfix


string infixToPostfix(string infix) {
stack<char> st; // Stack to hold operators
string postfix; // Resulting postfix expression
P a g e | 15

for (int i = 0; i < infix.length(); i++) {


char c = infix[i];

// If the character is an operand, add it to the output


if (isalnum(c)) {
postfix += c;
}
// If the character is '(', push it to the stack
else if (c == '(') {
st.push(c);
}
// If the character is ')', pop until '(' is encountered
else if (c == ')') {
while (!st.empty() && st.top() != '(') {
postfix += st.top();
st.pop();
}
if (!st.empty()) st.pop(); // Pop the '(' from the stack
}
// If the character is an operator
else if (isOperator(c)) {
while (!st.empty() && precedence(st.top()) > precedence(c)) {
postfix += st.top();
st.pop();
}
// If the operator is right-associative and of same precedence
while (!st.empty() && precedence(st.top()) == precedence(c) && !isRightAssociative(c)) {
postfix += st.top();
st.pop();
}
st.push(c); // Push the current operator to the stack
}
}

// Pop all the operators remaining in the stack


while (!st.empty()) {
postfix += st.top();
st.pop();
}

return postfix;
}
P a g e | 16

int main() {
string infixExpression;

// Example usage:
cout << "Enter the infix expression: ";
cin >> infixExpression;

string postfixExpression = infixToPostfix(infixExpression);

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

return 0;
}
Output:
Enter the infix expression: a+b*(c^d-e)^(f+g*h)-i
Postfix expression: abcd^e-fgh*+^*+i-

4. Design, Develop and Implement a menu driven Program in C/C++ for the following operations on
QUEUE of Characters
a. Insert an Element on to Linear QUEUE
b. Delete an Element from Linear QUEUE

Code:
#include <iostream>
using namespace std;

#define MAX 5 // Maximum size of the queue

class Queue {
private:
char arr[MAX]; // Array to store the queue elements
int front, rear; // Pointers to track the front and rear of the queue

public:
// Constructor to initialize the queue
Queue() {
front =rear = -1;
}
// Function to insert an element into the queue (Enqueue)
void enqueue(char element) {
if (rear == MAX - 1) {
cout << "Queue Overflow. Cannot insert " << element << ".\n";
} else {
if (front == -1) front = 0; // If inserting the first element
arr[++rear] = element;
cout << "Inserted " << element << " into the queue.\n";
}
P a g e | 17

// Function to delete an element from the queue (Dequeue)


char dequeue() {
if (front == -1 || front > rear) {
cout << "Queue Underflow. No element to delete.\n";
return '\0'; // Return null character if queue is empty
} else {
char element = arr[front];
cout << "Deleted " << element << " from the queue.\n";
front++;
if (front > rear) { // If the queue becomes empty after deletion
front = rear = -1;
}
return element;
}
}

// Function to display the queue status


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

// Main function with menu


int main() {
Queue queue;
int choice;
char element;

do {
cout << "\nMenu:\n";
cout << "1. Insert an Element into Queue (Enqueue)\n";
cout << "2. Delete an Element from Queue (Dequeue)\n";
cout << "3. Display the Queue\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter a character to insert: ";
cin >> element;
queue.enqueue(element);
break;

case 2:
P a g e | 18

queue.dequeue();
break;

case 3:
queue.display();
break;

case 4:
cout << "Exiting...\n";
break;

default:
cout << "Invalid choice. Please try again.\n";
break;
}
} while (choice != 4);

return 0;
}

Output:
Menu:
1. Insert an Element into Queue (Enqueue)
2. Delete an Element from Queue (Dequeue)
3. Display the Queue
4. Exit
Enter your choice: 1
Enter a character to insert: A
Inserted A into the queue.

Menu:
1. Insert an Element into Queue (Enqueue)
2. Delete an Element from Queue (Dequeue)
3. Display the Queue
4. Exit
Enter your choice: 1
Enter a character to insert: B
Inserted B into the queue.

Menu:
1. Insert an Element into Queue (Enqueue)
2. Delete an Element from Queue (Dequeue)
3. Display the Queue
4. Exit
Enter your choice: 3
Queue elements are: A B

Menu:
1. Insert an Element into Queue (Enqueue)
2. Delete an Element from Queue (Dequeue)
3. Display the Queue
4. Exit
Enter your choice: 2
Deleted A from the queue.
P a g e | 19

Menu:
1. Insert an Element into Queue (Enqueue)
2. Delete an Element from Queue (Dequeue)
3. Display the Queue
4. Exit
Enter your choice: 3
Queue elements are: B

Menu:
1. Insert an Element into Queue (Enqueue)
2. Delete an Element from Queue (Dequeue)
3. Display the Queue
4. Exit
Enter your choice: 4
Exiting...

5. Design, Develop and Implement a menu driven Program in C/C++ for the following operations on
Circular QUEUE of Characters
a. Insert an Element on to Circular QUEUE
b. Delete an Element from Circular QUEUE
c. Demonstrate Overflow and Underflow situations on Circular QUEUE
d. Display

Code:
#include <iostream>
using namespace std;

#define MAX 5 // Maximum size of the Circular Queue

class CircularQueue {
private:
char queue[MAX]; // Array to store elements
int front; // Index of the front element
int rear; // Index of the rear element

public:
// Constructor to initialize the circular queue
CircularQueue() : front(-1), rear(-1) {}

// Function to check if the circular queue is full


bool isFull() {
return (front == (rear + 1) % MAX);
}

// Function to check if the circular queue is empty


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

// Function to insert an element into the circular queue


void insert(char element) {
if (isFull()) {
P a g e | 20

cout << "Queue Overflow. Cannot insert " << element << ".\n";
} else {
if (front == -1) { // First element being inserted
front = 0;
}
rear = (rear + 1) % MAX;
queue[rear] = element;
cout << "Inserted " << element << " into the queue.\n";
}
}

// Function to delete an element from the circular queue


char remove() {
if (isEmpty()) {
cout << "Queue Underflow. No element to remove.\n";
return '\0'; // Return null character to indicate underflow
} else {
char removedElement = queue[front];
if (front == rear) { // Queue has only one element
front = rear = -1; // Reset queue after removing the last element
} else {
front = (front + 1) % MAX;
}
cout << "Removed " << removedElement << " from the queue.\n";
return removedElement;
}
}

// Function to display the elements of the circular queue


void display() {
if (isEmpty()) {
cout << "Queue is empty.\n";
} else {
cout << "Queue elements are: ";
int i = front;
while (true) {
cout << queue[i] << " ";
if (i == rear) break; // End when the rear is reached
i = (i + 1) % MAX; // Move circularly
}
cout << "\n";
}
}
};

int main() {
CircularQueue cq;
int choice;
char element;

do {
cout << "\nMenu:\n";
cout << "1. Insert an Element onto Circular Queue\n";
cout << "2. Delete an Element from Circular Queue\n";
cout << "3. Demonstrate Overflow\n";
P a g e | 21

cout << "4. Demonstrate Underflow\n";


cout << "5. Display the Queue\n";
cout << "6. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter a character to insert: ";
cin >> element;
cq.insert(element);
break;

case 2:
cq.remove();
break;

case 3:
cout << "Demonstrating Overflow:\n";
for (int i = 0; i < MAX + 1; i++) {
cq.insert('A' + i); // Attempt to insert more elements than the size
}
break;

case 4:
cout << "Demonstrating Underflow:\n";
while (!cq.isEmpty()) {
cq.remove(); // Removing all elements
}
cq.remove(); // Try to remove from an empty queue to show underflow
break;

case 5:
cq.display();
break;

case 6:
cout << "Exiting...\n";
break;

default:
cout << "Invalid choice. Please try again.\n";
break;
}
} while (choice != 6);

return 0;
}

Output:

Menu:
1. Insert an Element onto Circular Queue
2. Delete an Element from Circular Queue
3. Demonstrate Overflow
P a g e | 22

4. Demonstrate Underflow
5. Display the Queue
6. Exit
Enter your choice: 1
Enter a character to insert: B
Inserted B into the queue.

Menu:
1. Insert an Element onto Circular Queue
2. Delete an Element from Circular Queue
3. Demonstrate Overflow
4. Demonstrate Underflow
5. Display the Queue
6. Exit
Enter your choice: 1
Enter a character to insert: C
Inserted C into the queue.

Menu:
1. Insert an Element onto Circular Queue
2. Delete an Element from Circular Queue
3. Demonstrate Overflow
4. Demonstrate Underflow
5. Display the Queue
6. Exit
Enter your choice: 5
Queue elements are: B C

Menu:
1. Insert an Element onto Circular Queue
2. Delete an Element from Circular Queue
3. Demonstrate Overflow
4. Demonstrate Underflow
5. Display the Queue
6. Exit
Enter your choice: 2
Removed B from the queue.

Menu:
1. Insert an Element onto Circular Queue
2. Delete an Element from Circular Queue
3. Demonstrate Overflow
4. Demonstrate Underflow
5. Display the Queue
6. Exit
Enter your choice: 5
Queue elements are: C

Menu:
1. Insert an Element onto Circular Queue
2. Delete an Element from Circular Queue
3. Demonstrate Overflow
4. Demonstrate Underflow
5. Display the Queue
6. Exit
P a g e | 23

Enter your choice: 2


Removed C from the queue.

Menu:
1. Insert an Element onto Circular Queue
2. Delete an Element from Circular Queue
3. Demonstrate Overflow
4. Demonstrate Underflow
5. Display the Queue
6. Exit
Enter your choice: 4
Queue Underflow. No element to remove.

Menu:
1. Insert an Element onto Circular Queue
2. Delete an Element from Circular Queue
3. Demonstrate Overflow
4. Demonstrate Underflow
5. Display the Queue
6. Exit
Enter your choice: 6
Exiting…

6. Design, Develop and Implement a menu driven Program in C/C++ for the following operations on
Singly Linked List (SLL)
a. Create a SLL.
b. Insert at Beginning
c. Insert at Last
d. Insert at any random location
e. Delete from Beginning
f. Delete from Last
g. Delete node after specified location
h. Search for an element
i. Show
j. Exit

Code:
#include <iostream>
using namespace std;

// Node structure for a Singly Linked List


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

// Singly Linked List class


class SinglyLinkedList {
private:
Node* head;

public:
// Constructor
SinglyLinkedList() : head(nullptr) {}
P a g e | 24

// Create SLL (initializes an empty list)


void createList() {
head = nullptr;
cout << "Singly Linked List created.\n";
}

// Insert at Beginning
void insertAtBeginning(int value) {
Node* newNode = new Node;
newNode->data = value;
newNode->next = head;
head = newNode;
cout << "Inserted " << value << " at the beginning.\n";
}

// Insert at Last
void insertAtLast(int value) {
Node* newNode = new Node;
newNode->data = value;
newNode->next = nullptr;

if (head == nullptr) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}
cout << "Inserted " << value << " at the end.\n";
}

// Insert at any random location


void insertAtLocation(int value, int position) {
Node* newNode = new Node;
newNode->data = value;

if (position <= 0) {
cout << "Invalid position.\n";
return;
}

if (position == 1) {
newNode->next = head;
head = newNode;
cout << "Inserted " << value << " at position " << position << ".\n";
return;
}

Node* temp = head;


for (int i = 1; i < position - 1 && temp != nullptr; i++) {
temp = temp->next;
}
P a g e | 25

if (temp == nullptr) {
cout << "Position out of bounds.\n";
return;
}

newNode->next = temp->next;
temp->next = newNode;
cout << "Inserted " << value << " at position " << position << ".\n";
}

// Delete from Beginning


void deleteFromBeginning() {
if (head == nullptr) {
cout << "List is empty. Nothing to delete.\n";
return;
}
Node* temp = head;
head = head->next;
delete temp;
cout << "Deleted node from the beginning.\n";
}

// Delete from Last


void deleteFromLast() {
if (head == nullptr) {
cout << "List is empty. Nothing to delete.\n";
return;
}

if (head->next == nullptr) {
delete head;
head = nullptr;
cout << "Deleted the last node.\n";
return;
}

Node* temp = head;


while (temp->next != nullptr && temp->next->next != nullptr) {
temp = temp->next;
}
delete temp->next;
temp->next = nullptr;
cout << "Deleted the last node.\n";
}

// Delete node after specified location


void deleteAfterLocation(int position) {
if (head == nullptr || position < 1) {
cout << "Invalid position or list is empty.\n";
return;
}

Node* temp = head;


for (int i = 1; i < position && temp != nullptr; i++) {
P a g e | 26

temp = temp->next;
}

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


cout << "No node to delete after position " << position << ".\n";
return;
}

Node* toDelete = temp->next;


temp->next = temp->next->next;
delete toDelete;
cout << "Deleted node after position " << position << ".\n";
}

// Search for an element


void searchElement(int value) {
Node* temp = head;
int position = 1;
while (temp != nullptr) {
if (temp->data == value) {
cout << "Element " << value << " found at position " << position << ".\n";
return;
}
temp = temp->next;
position++;
}
cout << "Element " << value << " not found in the list.\n";
}

// Show the list


void showList() {
if (head == nullptr) {
cout << "The list is empty.\n";
return;
}

Node* temp = head;


cout << "The list elements are: ";
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};

// Main function with menu


int main() {
SinglyLinkedList sll;
int choice, value, position;

do {
cout << "\nMenu:\n";
cout << "1. Create a Singly Linked List\n";
cout << "2. Insert at Beginning\n";
P a g e | 27

cout << "3. Insert at Last\n";


cout << "4. Insert at any random location\n";
cout << "5. Delete from Beginning\n";
cout << "6. Delete from Last\n";
cout << "7. Delete node after specified location\n";
cout << "8. Search for an element\n";
cout << "9. Show List\n";
cout << "10. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
sll.createList();
break;
case 2:
cout << "Enter value to insert at beginning: ";
cin >> value;
sll.insertAtBeginning(value);
break;
case 3:
cout << "Enter value to insert at last: ";
cin >> value;
sll.insertAtLast(value);
break;
case 4:
cout << "Enter value to insert: ";
cin >> value;
cout << "Enter position to insert at: ";
cin >> position;
sll.insertAtLocation(value, position);
break;
case 5:
sll.deleteFromBeginning();
break;
case 6:
sll.deleteFromLast();
break;
case 7:
cout << "Enter position after which to delete: ";
cin >> position;
sll.deleteAfterLocation(position);
break;
case 8:
cout << "Enter value to search: ";
cin >> value;
sll.searchElement(value);
break;
case 9:
sll.showList();
break;
case 10:
cout << "Exiting...\n";
break;
default:
P a g e | 28

cout << "Invalid choice. Please try again.\n";


}
} while (choice != 10);

return 0;
}

Output:

Menu:
1. Create a Singly Linked List
2. Insert at Beginning
3. Insert at Last
4. Insert at any random location
5. Delete from Beginning
6. Delete from Last
7. Delete node after specified location
8. Search for an element
9. Show List
10. Exit
Enter your choice: 2
Enter value to insert at beginning: 10
Inserted 10 at the beginning.

Menu:
1. Create a Singly Linked List
2. Insert at Beginning
3. Insert at Last
4. Insert at any random location
5. Delete from Beginning
6. Delete from Last
7. Delete node after specified location
8. Search for an element
9. Show List
10. Exit
Enter your choice: 3
Enter value to insert at last: 20
Inserted 20 at the end.

Menu:
1. Create a Singly Linked List
2. Insert at Beginning
3. Insert at Last
4. Insert at any random location
5. Delete from Beginning
6. Delete from Last
7. Delete node after specified location
8. Search for an element
9. Show List
10. Exit
Enter your choice: 9
The list elements are: 10 20

Menu:
1. Create a Singly Linked List
P a g e | 29

2. Insert at Beginning
3. Insert at Last
4. Insert at any random location
5. Delete from Beginning
6. Delete from Last
7. Delete node after specified location
8. Search for an element
9. Show List
10. Exit
Enter your choice: 4
Enter value to insert: 15
Enter position to insert at: 2
Inserted 15 at position 2.

Menu:
1. Create a Singly Linked List
2. Insert at Beginning
3. Insert at Last
4. Insert at any random location
5. Delete from Beginning
6. Delete from Last
7. Delete node after specified location
8. Search for an element
9. Show List
10. Exit
Enter your choice: 9
The list elements are: 10 15 20

Menu:
1. Create a Singly Linked List
2. Insert at Beginning
3. Insert at Last
4. Insert at any random location
5. Delete from Beginning
6. Delete from Last
7. Delete node after specified location
8. Search for an element
9. Show List
10. Exit
Enter your choice: 5
Deleted node from the beginning.

Menu:
1. Create a Singly Linked List
2. Insert at Beginning
3. Insert at Last
4. Insert at any random location
5. Delete from Beginning
6. Delete from Last
7. Delete node after specified location
8. Search for an element
9. Show List
10. Exit
Enter your choice: 9
The list elements are: 15 20
P a g e | 30

Menu:
1. Create a Singly Linked List
2. Insert at Beginning
3. Insert at Last
4. Insert at any random location
5. Delete from Beginning
6. Delete from Last
7. Delete node after specified location
8. Search for an element
9. Show List
10. Exit
Enter your choice: 8
Enter value to search: 20
Element 20 found at position 2.

Menu:
1. Create a Singly Linked List
2. Insert at Beginning
3. Insert at Last
4. Insert at any random location
5. Delete from Beginning
6. Delete from Last
7. Delete node after specified location
8. Search for an element
9. Show List
10. Exit
Enter your choice: 10
Exiting…

7. Design, Develop and Implement the following menu driven Programs in C/C++ using Array
operations
a. Write a program for Bubble Sort algorithm
b. Write a program for Merge Sort algorithm
c. Write a program for Heap Sort algorithm
b. Write a program for Quick Sort algorithm
d. Write a program for Insertion Sort algorithm
e. Write a program for Selection Sort algorithm

Code:
#include <iostream>
using namespace std;

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


for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
swap(arr[j], arr[j+1]);
}
}
}
cout << "Array sorted using Bubble Sort.\n";
}
P a g e | 31

void merge(int arr[], int left, int mid, int right) {


int n1 = mid - left + 1;
int n2 = right - mid;
int L[n1], R[n2];

for (int i = 0; i < n1; i++)


L[i] = arr[left + i];
for (int i = 0; i < n2; i++)
R[i] = arr[mid + 1 + i];

int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}

while (i < n1) {


arr[k] = L[i];
i++;
k++;
}

while (j < n2) {


arr[k] = R[j];
j++;
k++;
}
}

void mergeSort(int arr[], int left, int right) {


if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
cout << "Array sorted using Merge Sort.\n";
}

void heapify(int arr[], int n, int i) {


int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;

if (left < n && arr[left] > arr[largest])


largest = left;

if (right < n && arr[right] > arr[largest])


largest = right;
P a g e | 32

if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}

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


for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);

for (int i = n - 1; i >= 0; i--) {


swap(arr[0], arr[i]);
heapify(arr, i, 0);
}
cout << "Array sorted using Heap Sort.\n";
}

int partition(int arr[], int low, int high) {


int pivot = arr[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[high]);
return (i + 1);
}

void quickSort(int arr[], int low, int high) {


if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
cout << "Array sorted using Quick Sort.\n";
}

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--;
}
arr[j + 1] = key;
}
cout << "Array sorted using Insertion Sort.\n";
}

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


for (int i = 0; i < n-1; i++) {
P a g e | 33

int min_idx = i;
for (int j = i+1; j < n; j++) {
if (arr[j] < arr[min_idx])
min_idx = j;
}
swap(arr[min_idx], arr[i]);
}
cout << "Array sorted using Selection Sort.\n";
}

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


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

int main() {
int arr[50], n, choice;

cout << "Enter number of elements: ";


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

do {
cout << "\nMenu:\n";
cout << "1. Bubble Sort\n";
cout << "2. Merge Sort\n";
cout << "3. Heap Sort\n";
cout << "4. Quick Sort\n";
cout << "5. Insertion Sort\n";
cout << "6. Selection Sort\n";
cout << "7. Display the Array\n";
cout << "8. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
bubbleSort(arr, n);
display(arr, n);
break;
case 2:
mergeSort(arr, 0, n - 1);
display(arr, n);
break;
case 3:
heapSort(arr, n);
display(arr, n);
break;
case 4:
quickSort(arr, 0, n - 1);
P a g e | 34

display(arr, n);
break;
case 5:
insertionSort(arr, n);
display(arr, n);
break;
case 6:
selectionSort(arr, n);
display(arr, n);
break;
case 7:
display(arr, n);
break;
case 8:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
break;
}
} while (choice != 8);

return 0;
}

Output:

Enter number of elements: 5


Enter the elements: 12 11 13 5 6

Menu:
1. Bubble Sort
2. Merge Sort
3. Heap Sort
4. Quick Sort
5. Insertion Sort
6. Selection Sort
7. Display the Array
8. Exit
Enter your choice: 1
Array sorted using Bubble Sort.
5 6 11 12 13

Menu:
1. Bubble Sort
2. Merge Sort
3. Heap Sort
4. Quick Sort
P a g e | 35

5. Insertion Sort
6. Selection Sort
7. Display the Array
8. Exit
Enter your choice: 7
The current array is: 5 6 11 12 13

Menu:
1. Bubble Sort
2. Merge Sort
3. Heap Sort
4. Quick Sort
5. Insertion Sort
6. Selection Sort
7. Display the Array
8. Exit
Enter your choice: 8
Exiting...

8. Design, Develop and Implement the following menu driven Programs in C/C++ for
implementing
a. Write a program for linear search algorithm
b. Write a program for displaying a sparse matrix

Code:
#include <iostream>
using namespace std;

// Linear Search Algorithm


int linearSearch(int arr[], int size, int target) {
for (int i = 0; i < size; i++) {
if (arr[i] == target) {
return i; // Return index if target is found
}
}
return -1; // Return -1 if target is not found
}

// Display Sparse Matrix


struct SparseMatrix {
int row;
int col;
P a g e | 36

int value;
};

void displaySparseMatrix(SparseMatrix sparse[], int nonZeroCount) {


cout << "Row | Column | Value\n";
cout << "----------------------\n";
for (int i = 0; i < nonZeroCount; i++) {
cout << sparse[i].row << " | " << sparse[i].col << " | " << sparse[i].value << endl;
}
}

int main() {
int choice;
do {
cout << "\nMenu:\n";
cout << "1. Linear Search\n";
cout << "2. Display Sparse Matrix\n";
cout << "3. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch(choice) {
case 1: {
int n, target;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cout << "Enter the element to search for: ";
cin >> target;
int result = linearSearch(arr, n, target);
if (result != -1) {
cout << "Element found at index " << result << endl;
} else {
cout << "Element not found in the array.\n";
}
break;
}
case 2: {
P a g e | 37

int rows, cols, nonZeroCount = 0;


cout << "Enter the number of rows: ";
cin >> rows;
cout << "Enter the number of columns: ";
cin >> cols;
SparseMatrix sparse[rows * cols];
cout << "Enter the matrix elements (row-wise):\n";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int value;
cin >> value;
if (value != 0) {
sparse[nonZeroCount].row = i;
sparse[nonZeroCount].col = j;
sparse[nonZeroCount].value = value;
nonZeroCount++;
}
}
}
if (nonZeroCount == 0) {
cout << "The matrix is a zero matrix.\n";
} else {
cout << "Sparse Matrix Representation:\n";
displaySparseMatrix(sparse, nonZeroCount);
}
break;
}
case 3:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice, please try again.\n";
}
} while (choice != 3);

return 0;
}
P a g e | 38

Output:

Menu:
1. Linear Search
2. Display Sparse Matrix
3. Exit
Enter your choice: 2
Enter the number of rows: 3
Enter the number of columns: 3
Enter the matrix elements (row-wise):
100
003
040
Sparse Matrix Representation:
Row | Column | Value
----------------------
0 |0 |1
1 |2 |3
2 |1 |4

Menu:
1. Linear Search
2. Display Sparse Matrix
3. Exit
Enter your choice: 1
Enter the number of elements: 5
Enter the elements: 10 20 30 40 50
Enter the element to search for: 30
Element found at index 2

Menu:
1. Linear Search
2. Display Sparse Matrix
3. Exit
Enter your choice: 3
Exiting…

9. Design, Develop and Implement a menu driven Program in C/C++ for the following
operations on Binary Search Tree (BST) of Integers
a. Create a BST of N Integers: 8, 10, 3, 1, 6, 14, 7
b. Traverse the BST in Inorder
c. Traverse the BST in Preorder
d. Traverse the BST in and Post Order
P a g e | 39

Code:
#include <iostream>
using namespace std;

// Node structure for Binary Search Tree


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

Node(int value) {
data = value;
left = nullptr;
right = nullptr;
}
};

// Class for Binary Search Tree


class BinarySearchTree {
private:
Node* root;

// Function to insert a node in the BST


Node* insert(Node* root, int value) {
if (root == nullptr) {
return new Node(value);
}
if (value < root->data) {
root->left = insert(root->left, value);
} else {
root->right = insert(root->right, value);
}
return root;
}

// Function for Inorder Traversal


void inorder(Node* root) {
if (root == nullptr) return;
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}

// Function for Preorder Traversal


void preorder(Node* root) {
if (root == nullptr) return;
cout << root->data << " ";
preorder(root->left);
preorder(root->right);
}

// Function for Postorder Traversal


void postorder(Node* root) {
if (root == nullptr) return;
postorder(root->left);
P a g e | 40

postorder(root->right);
cout << root->data << " ";
}

public:
// Constructor to initialize root as nullptr
BinarySearchTree() {
root = nullptr;
}

// Function to insert a node in the BST


void insert(int value) {
root = insert(root, value);
}

// Function to traverse in Inorder


void inorderTraversal() {
cout << "Inorder Traversal: ";
inorder(root);
cout << endl;
}

// Function to traverse in Preorder


void preorderTraversal() {
cout << "Preorder Traversal: ";
preorder(root);
cout << endl;
}

// Function to traverse in Postorder


void postorderTraversal() {
cout << "Postorder Traversal: ";
postorder(root);
cout << endl;
}
};

int main() {
BinarySearchTree bst;

// Inserting the elements into the BST: 8, 10, 3, 1, 6, 14, 7


bst.insert(8);
bst.insert(10);
bst.insert(3);
bst.insert(1);
bst.insert(6);
bst.insert(14);
bst.insert(7);

int choice;

do {
cout << "\nMenu:\n";
cout << "1. Traverse Inorder\n";
cout << "2. Traverse Preorder\n";
P a g e | 41

cout << "3. Traverse Postorder\n";


cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
bst.inorderTraversal();
break;
case 2:
bst.preorderTraversal();
break;
case 3:
bst.postorderTraversal();
break;
case 4:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 4);

return 0;
}

Output:

Menu:
1. Traverse Inorder
2. Traverse Preorder
3. Traverse Postorder
4. Exit
Enter your choice: 1
Inorder Traversal: 1 3 6 7 8 10 14

Menu:
1. Traverse Inorder
2. Traverse Preorder
3. Traverse Postorder
4. Exit
Enter your choice: 2
Preorder Traversal: 8 3 1 6 7 10 14

Menu:
1. Traverse Inorder
2. Traverse Preorder
3. Traverse Postorder
4. Exit
Enter your choice: 3
Postorder Traversal: 1 7 6 3 14 10 8

Menu:
1. Traverse Inorder
2. Traverse Preorder
P a g e | 42

3. Traverse Postorder
4. Exit
Enter your choice: 4
Exiting…

10. Design, Develop and Implement a Program in C/C++ for the following operations on
Graph(G)
a. Create a Graph N using Adjacency Matrix.
b. Print all the nodes reachable from a given starting node in a digraph using BFS method
c. Print all the nodes reachable from a given starting node in a digraph using DFS method

Code:
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
using namespace std;

class Graph {
private:
int N; // Number of nodes
vector<vector<int>> adjMatrix; // Adjacency Matrix

public:
// Constructor to initialize the graph
Graph(int n) {
N = n;
adjMatrix.resize(N, vector<int>(N, 0)); // Initialize the matrix to 0
}

// Function to add an edge to the graph (directed)


void addEdge(int u, int v) {
adjMatrix[u][v] = 1;
}

// Function to perform BFS and print reachable nodes


void BFS(int start) {
vector<bool> visited(N, false);
queue<int> q;

visited[start] = true;
q.push(start);

cout << "BFS starting from node " << start << ": ";

while (!q.empty()) {
int node = q.front();
q.pop();
cout << node << " ";

// Visit all the adjacent nodes


for (int i = 0; i < N; i++) {
if (adjMatrix[node][i] == 1 && !visited[i]) {
visited[i] = true;
P a g e | 43

q.push(i);
}
}
}
cout << endl;
}

// Function to perform DFS and print reachable nodes


void DFS(int start) {
vector<bool> visited(N, false);
stack<int> s;

visited[start] = true;
s.push(start);

cout << "DFS starting from node " << start << ": ";

while (!s.empty()) {
int node = s.top();
s.pop();
cout << node << " ";

// Visit all the adjacent nodes in reverse order (to simulate recursion)
for (int i = N - 1; i >= 0; i--) {
if (adjMatrix[node][i] == 1 && !visited[i]) {
visited[i] = true;
s.push(i);
}
}
}
cout << endl;
}

// Function to print the graph's adjacency matrix


void printGraph() {
cout << "Graph's Adjacency Matrix:" << endl;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout << adjMatrix[i][j] << " ";
}
cout << endl;
}
}
};

int main() {
int N, choice, u, v;

cout << "Enter the number of nodes in the graph: ";


cin >> N;

Graph g(N);

do {
cout << "\nMenu:\n";
P a g e | 44

cout << "1. Add Edge\n";


cout << "2. Print Graph (Adjacency Matrix)\n";
cout << "3. BFS Traversal\n";
cout << "4. DFS Traversal\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter the source and destination nodes for the edge (u, v): ";
cin >> u >> v;
g.addEdge(u, v);
break;
case 2:
g.printGraph();
break;
case 3:
cout << "Enter the starting node for BFS: ";
cin >> u;
g.BFS(u);
break;
case 4:
cout << "Enter the starting node for DFS: ";
cin >> u;
g.DFS(u);
break;
case 5:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 5);

return 0;
}

Output:

Enter the number of nodes in the graph: 5

Menu:
1. Add Edge
2. Print Graph (Adjacency Matrix)
3. BFS Traversal
4. DFS Traversal
5. Exit
Enter your choice: 1
Enter the source and destination nodes for the edge (u, v): 0 1

Menu:
1. Add Edge
2. Print Graph (Adjacency Matrix)
3. BFS Traversal
P a g e | 45

4. DFS Traversal
5. Exit
Enter your choice: 1
Enter the source and destination nodes for the edge (u, v): 1 2

Menu:
1. Add Edge
2. Print Graph (Adjacency Matrix)
3. BFS Traversal
4. DFS Traversal
5. Exit
Enter your choice: 2
Graph's Adjacency Matrix:
01000
00100
00000
00000
00000

Menu:
1. Add Edge
2. Print Graph (Adjacency Matrix)
3. BFS Traversal
4. DFS Traversal
5. Exit
Enter your choice: 3
Enter the starting node for BFS: 0
BFS starting from node 0: 0 1 2

Menu:
1. Add Edge
2. Print Graph (Adjacency Matrix)
3. BFS Traversal
4. DFS Traversal
5. Exit
Enter your choice: 4
Enter the starting node for DFS: 0
DFS starting from node 0: 0 1 2

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy