Shri.
Dadasaheb Gawai Charitable Trust Amravati’s
Takshashila Mahavidhyalaya, Amravati
Department of Computer Application
Session: 2024-25
Practical Record
Name of Student: Minaz Nazmi Raju Shaha
Subject: Data Structure (LAB: l)
Class: BCA 1st Year
Semester: lI
Department of Computer Application
CERTIFICATE
This is to certify that the Practical Entitled “Data Structure (LAB: l) is the bonafide work of “ Minaz
Nazmi Raju Shaha ” submitted in partial fulfilment for award of the degree “Bachelor of Computer
Application (B.C.A.)”, at “BCA I (SEM-lI)” Takshashila Mahavidyalaya, Affiliated to
Sant.Gadge.Baba.Amravati University, Amravati, during the Academic Year 2024-2025.
Practical In-Charge: Head of Department
Prof. Lalita Darshimbe
Date:
Department of Computer Application
Department of Computer Applications
(ACADEMY YEAR: 2024-25)
Subject: Data Structure (LAB: l) Class: BCA-I Sem-II
Sr. No. Name of Practical Date Remark
1 Write a program for insertion and deletion operations in an array. 10-03-2025
2 Write a program to search for an element in an array using linear search and 13-03-2025
binary search.
3 Write a program to sort an array using bubble, selection sort and insertion 17-03-2025
sort.
4 Write a program to merge two arrays. 20-03-2025
5 Write a program to add and subtract two matrices. 24-03-2025
6 Write a program to multiply two matrices. 27-03-2025
7 write a program to insert an element in to a singly linked list: 27-03-2025
a) At the beginning
b) At the end
c) At a specified position
8 write a program to delete an element in to a singly linked list: 03-04-2025
a) At the beginning
b) At the end
c) At a specified position
9 Write a program to implement stack operations using an array. 03-04-2025
10 Write a program to implement stack operations using a linked list. 07-04-2025
11 Write a program to add two polynomials using a linked lists. 07-04-2025
12 Write a program to evaluate a postfix expression using a stack. 10-04-2025
13 write a program to perform to perform the following using recursion: 10-04-2025
a) Find the factorial of a number
b) Find the GCD of two numbers
c) Solve towers of Hanoi problem
14 Write a program to implement simple queue operations using an array. 17-04-2025
15 Write a program to implement circular queue operations using a linked list. 17-04-2025
Signature of Teachers who taught examinee:
Prof. Lalita G. Darshimbe Mam …………..
Department of Computer Application
Program 1: Write a program for insertion and deletion operations in an array.
#include <iostream>
using namespace std;
void insertElement(int arr[], int& size, int element, int position) {
if (position > size || position < 0) {
cout << "Invalid position!" << endl;
return;
}
for (int i = size; i > position; i--) {
arr[i] = arr[i - 1];
}
arr[position] = element;
size++;
}
void deleteElement(int arr[], int& size, int position) {
if (position >= size || position < 0) {
cout << "Invalid position!" << endl;
return;
}
for (int i = position; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
size--;
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
int arr[100] = {1, 2, 3, 4, 5}; // Initial array
int size = 5;
cout << "Original array: ";
printArray(arr, size);
// Insertion
insertElement(arr, size, 10, 2);
cout << "After insertion: ";
printArray(arr, size);
// Deletion
deleteElement(arr, size, 3);
cout << "After deletion: ";
printArray(arr, size);
return 0;
}
Output:
Original array: 1 2 3 4 5
After insertion: 1 2 10 3 4 5
After deletion: 1 2 10 4 5
Department of Computer Application
Program 2: Write a program to search for an element in an array using linear search and
binary search.
#include <iostream>
using namespace std;
// Linear Search
int linearSearch(int arr[], int size, int key) {
for (int i = 0; i < size; i++) {
if (arr[i] == key) {
return i; // Found at index i
}
}
return -1; // Not found
}
// Binary Search (Assumes the array is sorted)
int binarySearch(int arr[], int left, int right, int key) {
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == key) return mid;
else if (arr[mid] < key) left = mid + 1;
else right = mid - 1;
}
return -1; // Not found
}
int main() {
int arr[] = {2, 4, 6, 8, 10, 12, 14, 16}; // Sorted array for binary search
int size = sizeof(arr) / sizeof(arr[0]);
int key;
cout << "Enter the element to search: ";
cin >> key;
// Linear Search
int linResult = linearSearch(arr, size, key);
if (linResult != -1) cout << "Element found at index " << linResult << " using Linear Search.\n";
else cout << "Element not found using Linear Search.\n";
// Binary Search
int binResult = binarySearch(arr, 0, size - 1, key);
if (binResult != -1) cout << "Element found at index " << binResult << " using Binary Search.\n";
else cout << "Element not found using Binary Search.\n";
return 0;
}
Output:
Enter the element to search: 3
Element not found using Linear Search.
Element not found using Binary Search.
Enter the element to search: 4
Element found at index 1 using Linear Search.
Element found at index 1 using Binary Search.
Department of Computer Application
Program 3: write a program to sort an array using bubble, selection sort and insertion sort.
#include <iostream>
using namespace std;
// Bubble Sort
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]);
}
}
}
}
// Selection Sort
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
swap(arr[i], arr[minIndex]);
}
}
// Insertion Sort
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i], j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
// Print array
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int size = sizeof(arr) / sizeof(arr[0]);
cout << "Original array: ";
printArray(arr, size);
Department of Computer Application
// Bubble Sort
bubbleSort(arr, size);
cout << "Sorted using Bubble Sort: ";
printArray(arr, size);
// Resetting the array
int arr2[] = {64, 25, 12, 22, 11};
// Selection Sort
selectionSort(arr2, size);
cout << "Sorted using Selection Sort: ";
printArray(arr2, size);
// Resetting the array again
int arr3[] = {64, 25, 12, 22, 11};
// Insertion Sort
insertionSort(arr3, size);
cout << "Sorted using Insertion Sort: ";
printArray(arr3, size);
return 0;
}
Output:
Original array: 64 25 12 22 11
Sorted using Bubble Sort: 11 12 22 25 64
Sorted using Selection Sort: 11 12 22 25 64
Sorted using Insertion Sort: 11 12 22 25 64
Department of Computer Application
Program 4: write a program to merge two arrays.
#include <iostream>
using namespace std;
void mergeArrays(int arr1[], int size1, int arr2[], int size2, int mergedArr[]) {
int i = 0, j = 0, k = 0;
while (i < size1) {
mergedArr[k++] = arr1[i++];
}
while (j < size2) {
mergedArr[k++] = arr2[j++];
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
int arr1[] = {1, 3, 5, 7};
int arr2[] = {2, 4, 6, 8};
int size1 = sizeof(arr1) / sizeof(arr1[0]);
int size2 = sizeof(arr2) / sizeof(arr2[0]);
int mergedSize = size1 + size2;
int mergedArr[mergedSize];
mergeArrays(arr1, size1, arr2, size2, mergedArr);
cout << "Merged Array: ";
printArray(mergedArr, mergedSize);
return 0;
}
Output:
Merged Array: 1 3 5 7 2 4 6 8
Department of Computer Application
Program 5: write a program to add and subtract two matrices.
#include <iostream>
using namespace std;
void addMatrices(int A[][3], int B[][3], int result[][3], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = A[i][j] + B[i][j];
}
}
}
void subtractMatrices(int A[][3], int B[][3], int result[][3], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = A[i][j] - B[i][j];
}
}
}
void printMatrix(int matrix[][3], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
int main() {
int A[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int B[3][3] = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
int result[3][3];
cout << "Matrix A:\n";
printMatrix(A, 3, 3);
cout << "\nMatrix B:\n";
printMatrix(B, 3, 3);
// Matrix Addition
addMatrices(A, B, result, 3, 3);
cout << "\nResult of Addition:\n";
printMatrix(result, 3, 3);
// Matrix Subtraction
subtractMatrices(A, B, result, 3, 3);
cout << "\nResult of Subtraction:\n";
printMatrix(result, 3, 3);
return 0;
}
Output:
Department of Computer Application
Matrix A:
123
456
789
Matrix B:
987
654
321
Result of Addition:
10 10 10
10 10 10
10 10 10
Result of Subtraction:
-8 -6 -4
-2 0 2
468
Department of Computer Application
Program 6: write a program to multiply two matrices.
#include <iostream>
using namespace std;
void multiplyMatrices(int A[][3], int B[][3], int result[][3], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = 0;
for (int k = 0; k < cols; k++) {
result[i][j] += A[i][k] * B[k][j];
}
}
}
}
void printMatrix(int matrix[][3], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
int main() {
int A[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int B[3][3] = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
int result[3][3];
cout << "Matrix A:\n";
printMatrix(A, 3, 3);
cout << "\nMatrix B:\n";
printMatrix(B, 3, 3);
// Matrix Multiplication
multiplyMatrices(A, B, result, 3, 3);
cout << "\nResult of Multiplication:\n";
printMatrix(result, 3, 3);
return 0;
}
Output:
Matrix A:
123
456
789
Matrix B:
987
654
321
Result of Multiplication:
30 24 18
84 69 54
138 114 90
Department of Computer Application
Program 7: write a program to insert an element in to a singly linked list:
a) At the beginning
b) At the end
c) At a specified position
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
// Function to insert at the beginning
void insertAtBeginning(Node*& head, int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = head;
head = newNode;
}
// Function to insert at the end
void insertAtEnd(Node*& head, int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = NULL; // Changed nullptr to NULL
if (head == NULL) { // Changed nullptr to NULL
head = newNode;
return;
}
Node* temp = head;
while (temp->next != NULL) { // Changed nullptr to NULL
temp = temp->next;
}
temp->next = newNode;
}
// Function to insert at a specified position
void insertAtPosition(Node*& head, int value, int position) {
if (position <= 0) {
cout << "Invalid position!" << endl;
return;
}
Node* newNode = new Node();
newNode->data = value;
if (position == 1) {
newNode->next = head;
head = newNode;
return;
}
Department of Computer Application
Node* temp = head;
for (int i = 1; i < position - 1; i++) {
if (temp == NULL || temp->next == NULL) { // Changed nullptr to NULL
cout << "Position out of range!" << endl;
delete newNode;
return;
}
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
}
// Function to print the linked list
void printList(Node* head) {
Node* temp = head;
while (temp != NULL) { // Changed nullptr to NULL
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}
int main() {
Node* head = NULL; // Changed nullptr to NULL
insertAtBeginning(head, 10);
insertAtBeginning(head, 5);
cout << "After inserting at beginning: ";
printList(head);
insertAtEnd(head, 20);
insertAtEnd(head, 25);
cout << "After inserting at end: ";
printList(head);
insertAtPosition(head, 15, 3);
cout << "After inserting at position 3: ";
printList(head);
return 0;
}
Output:
After inserting at beginning: 5 -> 10 -> NULL
After inserting at end: 5 -> 10 -> 20 -> 25 -> NULL
After inserting at position 3: 5 -> 10 -> 15 -> 20 -> 25 -> NULL
Department of Computer Application
Program 8: write a program to delete an element in to a singly linked list:
a) At the beginning
b) At the end
c) At a specified position
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
// Function to delete from the beginning
void deleteAtBeginning(Node*& head) {
if (!head) {
cout << "List is empty! Cannot delete.\n";
return;
}
Node* temp = head;
head = head->next;
delete temp;
}
// Function to delete from the end
void deleteAtEnd(Node*& head) {
if (!head) {
cout << "List is empty! Cannot delete.\n";
return;
}
if (!head->next) {
delete head;
head = NULL;
return;
}
Node* temp = head;
while (temp->next->next) {
temp = temp->next;
}
delete temp->next;
temp->next = NULL;
}
// Function to delete from a specified position
void deleteAtPosition(Node*& head, int position) {
if (!head || position <= 0) {
cout << "Invalid position or empty list! Cannot delete.\n";
return;
}
if (position == 1) {
deleteAtBeginning(head);
Department of Computer Application
return;
}
Node* temp = head;
for (int i = 1; temp && i < position - 1; i++) {
temp = temp->next;
}
if (!temp || !temp->next) {
cout << "Position out of range!\n";
return;
}
Node* deleteNode = temp->next;
temp->next = deleteNode->next;
delete deleteNode;
}
// Function to insert an element at the end
void insertAtEnd(Node*& head, int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = NULL;
if (!head) {
head = newNode;
return;
}
Node* temp = head;
while (temp->next) {
temp = temp->next;
}
temp->next = newNode;
}
// Function to print the linked list
void printList(Node* head) {
Node* temp = head;
while (temp) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL\n";
}
int main() {
Node* head = NULL;
insertAtEnd(head, 10);
insertAtEnd(head, 20);
insertAtEnd(head, 30);
insertAtEnd(head, 40);
cout << "Original List:\n";
printList(head);
Department of Computer Application
// Delete at beginning
deleteAtBeginning(head);
cout << "After deleting at beginning:\n";
printList(head);
// Delete at end
deleteAtEnd(head);
cout << "After deleting at end:\n";
printList(head);
// Delete at position 2
deleteAtPosition(head, 2);
cout << "After deleting at position 2:\n";
printList(head);
return 0;
}
Output:
Original List:
10 -> 20 -> 30 -> 40 -> NULL
After deleting at beginning:
20 -> 30 -> 40 -> NULL
After deleting at end:
20 -> 30 -> NULL
After deleting at position 2:
20 -> NULL
Department of Computer Application
Program 9: write a program to implement stack operations using an array.
#include <iostream>
using namespace std;
#define MAX 100 // Maximum size of the stack
class Stack {
int top;
int arr[MAX]; // Stack array
public:
Stack() { top = -1; } // Constructor initializes top
// Push operation
void push(int value) {
if (top >= MAX - 1) {
cout << "Stack Overflow!" << endl;
return;
}
arr[++top] = value;
cout << value << " pushed into stack." << endl;
}
// Pop operation
void pop() {
if (top < 0) {
cout << "Stack Underflow!" << endl;
return;
}
cout << arr[top] << " popped from stack." << endl;
top--;
}
// Display stack elements
void display() {
if (top < 0) {
cout << "Stack is empty!" << endl;
return;
}
cout << "Stack elements: ";
for (int i = top; i >= 0; i--) {
cout << arr[i] << " ";
}
cout << endl;
}
};
int main() {
Stack s;
s.push(10);
s.push(20);
s.push(30);
Department of Computer Application
s.display();
s.pop();
s.display();
return 0;
}
Output:
10 pushed into stack.
20 pushed into stack.
30 pushed into stack.
Stack elements: 30 20 10
30 popped from stack.
Stack elements: 20 10
Department of Computer Application
Program 10: write a program to implement stack operations using a linked list.
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
// Class to implement stack using linked list
class Stack {
Node* top;
public:
Stack() { top = NULL; } // Changed nullptr to NULL
// Push operation
void push(int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = top;
top = newNode;
cout << value << " pushed into stack." << endl;
}
// Pop operation
void pop() {
if (top == NULL) { // Changed nullptr to NULL
cout << "Stack Underflow! Cannot pop from an empty stack." << endl;
return;
}
Node* temp = top;
cout << temp->data << " popped from stack." << endl;
top = top->next;
delete temp;
}
// Display stack elements
void display() {
if (top == NULL) { // Changed nullptr to NULL
cout << "Stack is empty!" << endl;
return;
}
cout << "Stack elements: ";
Node* temp = top;
while (temp != NULL) { // Changed nullptr to NULL
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};
int main() {
Department of Computer Application
Stack s;
s.push(10);
s.push(20);
s.push(30);
s.display();
s.pop();
s.display();
return 0;
}
Output:
10 pushed into stack.
20 pushed into stack.
30 pushed into stack.
Stack elements: 30 20 10
30 popped from stack.
Stack elements: 20 10
Department of Computer Application
Program 11: write a program to add two polynomials using a linked lists.
#include <iostream>
using namespace std;
struct Node {
int coeff, exp;
Node* next;
};
// Function to insert a term into a polynomial
void insertTerm(Node*& head, int coeff, int exp) {
Node* newNode = new Node();
newNode->coeff = coeff;
newNode->exp = exp;
newNode->next = NULL;
if (!head || head->exp < exp) {
newNode->next = head;
head = newNode;
return;
}
Node* temp = head;
Node* prev = NULL;
while (temp && temp->exp >= exp) {
if (temp->exp == exp) {
temp->coeff += coeff; // If exponent already exists, just add coefficients
delete newNode; // Prevent memory leak
return;
}
prev = temp;
temp = temp->next;
}
newNode->next = temp;
if (prev) {
prev->next = newNode;
} else {
head = newNode;
}
}
// Function to add two polynomials
Node* addPolynomials(Node* poly1, Node* poly2) {
Node* result = NULL;
while (poly1 || poly2) {
if (!poly2 || (poly1 && poly1->exp > poly2->exp)) {
insertTerm(result, poly1->coeff, poly1->exp);
poly1 = poly1->next;
} else if (!poly1 || (poly2 && poly2->exp > poly1->exp)) {
insertTerm(result, poly2->coeff, poly2->exp);
poly2 = poly2->next;
} else {
insertTerm(result, poly1->coeff + poly2->coeff, poly1->exp);
poly1 = poly1->next;
Department of Computer Application
poly2 = poly2->next;
}
}
return result;
}
// Function to print a polynomial
void printPolynomial(Node* poly) {
while (poly) {
cout << poly->coeff << "x^" << poly->exp;
if (poly->next) cout << " + ";
poly = poly->next;
}
cout << endl;
}
// Function to delete the linked list and free memory
void deletePolynomial(Node*& head) {
while (head) {
Node* temp = head;
head = head->next;
delete temp;
}
}
int main() {
Node* poly1 = NULL;
Node* poly2 = NULL;
// First polynomial: 5x^2 + 3x^1 + 7x^0
insertTerm(poly1, 5, 2);
insertTerm(poly1, 3, 1);
insertTerm(poly1, 7, 0);
// Second polynomial: 4x^2 + 2x^1 + 6x^0
insertTerm(poly2, 4, 2);
insertTerm(poly2, 2, 1);
insertTerm(poly2, 6, 0);
cout << "Polynomial 1: ";
printPolynomial(poly1);
cout << "Polynomial 2: ";
printPolynomial(poly2);
Node* result = addPolynomials(poly1, poly2);
cout << "Resultant Polynomial: ";
printPolynomial(result);
// Free memory
deletePolynomial(poly1);
deletePolynomial(poly2);
deletePolynomial(result);
return 0;
}
Output:
Polynomial 1: 5x^2 + 3x^1 + 7x^0
Polynomial 2: 4x^2 + 2x^1 + 6x^0
Resultant Polynomial: 9x^2 + 5x^1 + 13x^0
Department of Computer Application
Program 12: Write a program to evaluate a postfix expression using a stack.
#include <iostream>
#include <stack>
#include <cctype>
using namespace std;
// Function to evaluate a postfix expression
int evaluatePostfix(const string& expression) {
stack<int> s;
// Iterate through the expression using traditional indexing for compatibility
for (size_t i = 0; i < expression.length(); i++) {
char ch = expression[i];
if (isdigit(ch)) {
s.push(ch - '0'); // Convert char to int
} else {
if (s.size() < 2) {
cout << "Error: Invalid postfix expression (not enough operands)." << endl;
return -1;
}
int operand2 = s.top(); s.pop();
int operand1 = s.top(); s.pop();
switch (ch) {
case '+': s.push(operand1 + operand2); break;
case '-': s.push(operand1 - operand2); break;
case '*': s.push(operand1 * operand2); break;
case '/':
if (operand2 == 0) {
cout << "Error: Division by zero!" << endl;
return -1;
}
s.push(operand1 / operand2);
break;
default:
cout << "Error: Unsupported operator '" << ch << "' encountered." << endl;
return -1;
}
}
}
// Final validation check
if (s.size() != 1) {
cout << "Error: Invalid postfix expression (remaining elements in stack)." << endl;
return -1;
}
return s.top();
}
int main() {
Department of Computer Application
string postfix;
cout << "Enter postfix expression: ";
cin >> postfix;
if (postfix.empty()) {
cout << "Error: Empty input provided!" << endl;
return -1;
}
int result = evaluatePostfix(postfix);
if (result != -1) {
cout << "Evaluated Result: " << result << endl;
}
return 0;
}
Output:
Enter postfix expression: 53+82-*
Evaluated Result: 48
Department of Computer Application
Program 13: write a program to perform to perform the following using recursion:
a) Find the factorial of a number
b) Find the GCD of two numbers
c) Solve towers of Hanoi problem
#include <iostream>
using namespace std;
// Function to find factorial using recursion
long long factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
// Function to find GCD using recursion
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
// Function to solve the Towers of Hanoi problem using recursion
void hanoi(int n, char from_rod, char to_rod, char aux_rod) {
if (n == 1) {
cout << "Move disk 1 from " << from_rod << " to " << to_rod << endl;
return;
}
hanoi(n - 1, from_rod, aux_rod, to_rod);
cout << "Move disk " << n << " from " << from_rod << " to " << to_rod << endl;
hanoi(n - 1, aux_rod, to_rod, from_rod);
}
int main() {
// Factorial Calculation
int num = 5;
cout << "Factorial of " << num << " is " << factorial(num) << endl;
// GCD Calculation
int a = 48, b = 18;
cout << "GCD of " << a << " and " << b << " is " << gcd(a, b) << endl;
// Towers of Hanoi Problem
int disks = 3;
cout << "Towers of Hanoi solution for " << disks << " disks:" << endl;
hanoi(disks, 'A', 'C', 'B');
return 0;
}
Output:
Factorial of 5 is 120
GCD of 48 and 18 is 6
Towers of Hanoi solution for 3 disks:
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C
Department of Computer Application
Program 14: Write a program to implement simple queue operations using an array.
#include <iostream>
using namespace std;
#define MAX 100 // Maximum size of the queue
class Queue {
int front, rear;
int arr[MAX]; // Array to store queue elements
public:
Queue() { front = rear = -1; } // Constructor initializes front and rear
// Enqueue operation (Insert element)
void enqueue(int value) {
if (rear == MAX - 1) {
cout << "Queue Overflow! Cannot enqueue more elements." << endl;
return;
}
if (front == -1) front = 0; // First element inserted
arr[++rear] = value;
cout << value << " enqueued into the queue." << endl;
}
// Dequeue operation (Remove element)
void dequeue() {
if (front == -1 || front > rear) {
cout << "Queue Underflow! Cannot dequeue from an empty queue." << endl;
return;
}
cout << arr[front] << " dequeued from the queue." << endl;
front++;
}
// Display queue elements
void display() {
if (front == -1 || front > rear) {
cout << "Queue is empty!" << endl;
return;
}
cout << "Queue elements: ";
for (int i = front; i <= rear; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
};
int main() {
Queue q;
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
Department of Computer Application
q.display();
q.dequeue();
q.display();
return 0;
}
Output:
10 enqueued into the queue.
20 enqueued into the queue.
30 enqueued into the queue.
Queue elements: 10 20 30
10 dequeued from the queue.
Queue elements: 20 30
Department of Computer Application
Program 15: write a program to implement circular queue operations using a linked list.
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
// Class to implement circular queue using linked list
class CircularQueue {
Node* front;
Node* rear;
public:
CircularQueue() {
front = rear = NULL; // Changed nullptr to NULL for compatibility
}
// Enqueue operation (Insert element)
void enqueue(int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = NULL;
if (!front) {
front = rear = newNode;
rear->next = front; // Circular link
} else {
rear->next = newNode;
rear = newNode;
rear->next = front;
}
cout << value << " enqueued into the circular queue." << endl;
}
// Dequeue operation (Remove element)
void dequeue() {
if (!front) {
cout << "Circular Queue Underflow! Cannot dequeue from an empty queue." << endl;
return;
}
if (front == rear) { // Single element case
cout << front->data << " dequeued from the queue." << endl;
delete front;
front = rear = NULL;
} else {
Node* temp = front;
front = front->next;
rear->next = front; // Maintain circular structure
cout << temp->data << " dequeued from the queue." << endl;
delete temp;
Department of Computer Application
}
}
// Display queue elements
void display() {
if (!front) {
cout << "Circular Queue is empty!" << endl;
return;
}
cout << "Circular Queue elements: ";
Node* temp = front;
do {
cout << temp->data << " ";
temp = temp->next;
} while (temp != front);
cout << endl;
}
// Destructor to free memory when object is destroyed
~CircularQueue() {
while (front) {
dequeue();
}
}
};
int main() {
CircularQueue cq;
cq.enqueue(10);
cq.enqueue(20);
cq.enqueue(30);
cq.display();
cq.dequeue();
cq.display();
return 0;
}
Output:
10 enqueued into the circular queue.
20 enqueued into the circular queue.
30 enqueued into the circular queue.
Circular Queue elements: 10 20 30
10 dequeued from the queue.
Circular Queue elements: 20 30
20 dequeued from the queue.
30 dequeued from the queue.
Department of Computer Application