DS - BCA I (All Program)
DS - BCA I (All Program)
Practical Record
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
Date:
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
#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;
// 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
#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
}
// 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.
#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]);
// Selection Sort
selectionSort(arr2, size);
cout << "Sorted using Selection Sort: ";
printArray(arr2, size);
// 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
#include <iostream>
using namespace std;
void mergeArrays(int arr1[], int size1, int arr2[], int size2, int mergedArr[]) {
int i = 0, j = 0, k = 0;
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]);
return 0;
}
Output:
Merged Array: 1 3 5 7 2 4 6 8
#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];
}
}
}
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];
// 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:
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
#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];
// 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
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
if (position == 1) {
newNode->next = head;
head = newNode;
return;
}
newNode->next = temp->next;
temp->next = newNode;
}
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);
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
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
if (!head->next) {
delete head;
head = NULL;
return;
}
delete temp->next;
temp->next = NULL;
}
if (position == 1) {
deleteAtBeginning(head);
if (!temp || !temp->next) {
cout << "Position out of range!\n";
return;
}
if (!head) {
head = newNode;
return;
}
int main() {
Node* head = NULL;
insertAtEnd(head, 10);
insertAtEnd(head, 20);
insertAtEnd(head, 30);
insertAtEnd(head, 40);
cout << "Original List:\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
#include <iostream>
using namespace std;
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--;
}
int main() {
Stack s;
s.push(10);
s.push(20);
s.push(30);
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
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
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;
}
int main() {
s.push(10);
s.push(20);
s.push(30);
s.display();
s.pop();
s.display();
return 0;
}
Output:
#include <iostream>
using namespace std;
struct Node {
int coeff, exp;
Node* next;
};
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
#include <iostream>
#include <stack>
#include <cctype>
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;
}
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;
}
}
}
return s.top();
}
int main() {
if (postfix.empty()) {
cout << "Error: Empty input provided!" << endl;
return -1;
}
return 0;
}
Output:
Enter postfix expression: 53+82-*
Evaluated Result: 48
#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;
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
#include <iostream>
using namespace std;
class Queue {
int front, rear;
int arr[MAX]; // Array to store queue elements
public:
Queue() { front = rear = -1; } // Constructor initializes front and rear
int main() {
Queue q;
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
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
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
public:
CircularQueue() {
front = rear = NULL; // Changed nullptr to NULL for compatibility
}
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;
}
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.