DSA lab file copy
DSA lab file copy
DATA STRUCTURES
(ENCS 251)
INDEX
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
arr[pos - 1] = element;
n++; // Increment the size of the array
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!
3. Insert Element
4. Delete Element
5. Exit
Enter your choice: 2
Array elements: 10 20 25 30 40 50
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;
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) {}
if (isPalindrome) {
cout << str << " is a Palindrome.\n";
} else {
cout << str << " is not a Palindrome.\n";
}
}
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
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
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)
return postfix;
}
P a g e | 16
int main() {
string infixExpression;
// Example usage:
cout << "Enter the infix expression: ";
cin >> infixExpression;
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;
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
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;
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) {}
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";
}
}
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
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
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;
public:
// Constructor
SinglyLinkedList() : head(nullptr) {}
P a g e | 24
// 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";
}
if (position <= 0) {
cout << "Invalid position.\n";
return;
}
if (position == 1) {
newNode->next = head;
head = newNode;
cout << "Inserted " << value << " at position " << position << ".\n";
return;
}
if (temp == nullptr) {
cout << "Position out of bounds.\n";
return;
}
newNode->next = temp->next;
temp->next = newNode;
cout << "Inserted " << value << " at position " << position << ".\n";
}
if (head->next == nullptr) {
delete head;
head = nullptr;
cout << "Deleted the last node.\n";
return;
}
temp = temp->next;
}
do {
cout << "\nMenu:\n";
cout << "1. Create a Singly Linked List\n";
cout << "2. Insert at Beginning\n";
P a g e | 27
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
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;
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++;
}
if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}
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";
}
int main() {
int arr[50], n, choice;
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:
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;
int value;
};
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
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(int value) {
data = value;
left = nullptr;
right = nullptr;
}
};
postorder(root->right);
cout << root->data << " ";
}
public:
// Constructor to initialize root as nullptr
BinarySearchTree() {
root = nullptr;
}
int main() {
BinarySearchTree bst;
int choice;
do {
cout << "\nMenu:\n";
cout << "1. Traverse Inorder\n";
cout << "2. Traverse Preorder\n";
P a g e | 41
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
}
visited[start] = true;
q.push(start);
cout << "BFS starting from node " << start << ": ";
while (!q.empty()) {
int node = q.front();
q.pop();
cout << node << " ";
q.push(i);
}
}
}
cout << endl;
}
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;
}
int main() {
int N, choice, u, v;
Graph g(N);
do {
cout << "\nMenu:\n";
P a g e | 44
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:
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