0% found this document useful (0 votes)
6 views17 pages

Ds Practicals

Practical Programs of Data structures and Algorithms

Uploaded by

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

Ds Practicals

Practical Programs of Data structures and Algorithms

Uploaded by

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

PRACTICAL NO: 01 - Insertion and Deletion in Array

#include <iostream>
using namespace std;

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


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

int main() {
int arr[10] = {1, 2, 3, 4, 5};
int n = 5;

cout << "Original array: ";


display(arr, n);

// Insertion at position 2 (0-based index)


int pos = 2, value = 10;
for(int i=n; i>pos; i--) {
arr[i] = arr[i-1];
}
arr[pos] = value;
n++;
cout << "After inserting " << value << " at position " << pos << ": ";
display(arr, n);

// Deletion at position 3
pos = 3;
for(int i=pos; i<n-1; i++) {
arr[i] = arr[i+1];
}
n--;
cout << "After deleting element at position " << pos << ": ";
display(arr, n);

return 0;
}

Output:

Original array: 1 2 3 4 5
After inserting 10 at position 2: 1 2 10 3 4 5
After deleting element at position 3: 1 2 10 4 5
PRACTICAL NO: 02 - Linear Search

#include <iostream>
using namespace std;

int linearSearch(int arr[], int n, int key) {


for(int i=0; i<n; i++) {
if(arr[i] == key) {
return i;
}
}
return -1;
}

int main() {
int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr)/sizeof(arr[0]);
int key = 30;

int result = linearSearch(arr, n, key);

if(result == -1) {
cout << "Element " << key << " not found in array" << endl;
} else {
cout << "Element " << key << " found at index " << result << endl;
}

return 0;
}

Output;

Element 30 found at index 2


PRACTICAL NO: 03 - Binary Search
#include <iostream>
using namespace std;

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;
}

if(arr[mid] < key) {


left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
int main() {
int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr)/sizeof(arr[0]);
int key = 40;
int result = binarySearch(arr, 0, n-1, key);
if(result == -1) {
cout << "Element " << key << " not found in array" << endl;
} else {
cout << "Element " << key << " found at index " << result << endl;
}
return 0;
}

Output:
Element 40 found at index 3
PRACTICAL NO: 04 - Bubble Sort
#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]);
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);

cout << "Original array: ";


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

bubbleSort(arr, n);
cout << "Sorted array: ";
for(int i=0; i<n; i++) {
cout << arr[i] << " ";
}
cout << endl;

return 0;
}

Output:
Original array: 64 34 25 12 22 11 90
Sorted array: 11 12 22 25 34 64 90
PRACTICAL NO: 05 - Merge Two Arrays
#include <iostream>
using namespace std;
int main() {
int arr1[] = {1, 3, 5, 7};
int arr2[] = {2, 4, 6, 8};
int n1 = sizeof(arr1)/sizeof(arr1[0]);
int n2 = sizeof(arr2)/sizeof(arr2[0]);
int merged[n1 + n2];
int i = 0, j = 0, k = 0;
while(i < n1 && j < n2) {
if(arr1[i] < arr2[j]) {
merged[k++] = arr1[i++];
} else {
merged[k++] = arr2[j++];
}
}
while(i < n1) {
merged[k++] = arr1[i++];
}
while(j < n2) {
merged[k++] = arr2[j++];
}
cout << "Merged array: ";
for(int i=0; i<k; i++) {
cout << merged[i] << " ";
}
cout << endl;

return 0;
}
**Output:**
Merged array: 1 2 3 4 5 6 7 8
PRACTICAL NO: 06 - Matrix Addition

#include <iostream>
using namespace std;
#define ROWS 2
#define COLS 2

void addMatrices(int mat1[ROWS][COLS], int mat2[ROWS][COLS], int result[ROWS]


[COLS]) {
for(int i=0; i<ROWS; i++) {
for(int j=0; j<COLS; j++) {
result[i][j] = mat1[i][j] + mat2[i][j];
}
}
}

void printMatrix(int mat[ROWS][COLS]) {


for(int i=0; i<ROWS; i++) {
for(int j=0; j<COLS; j++) {
cout << mat[i][j] << " ";
}
cout << endl;
}
}

int main() {
int mat1[ROWS][COLS] = {{1, 2}, {3, 4}};
int mat2[ROWS][COLS] = {{5, 6}, {7, 8}};
int result[ROWS][COLS];

cout << "Matrix 1:" << endl;


printMatrix(mat1);

cout << "Matrix 2:" << endl;


printMatrix(mat2);
addMatrices(mat1, mat2, result);

cout << "Resultant Matrix:" << endl;


printMatrix(result);

return 0;
}

Output:
Matrix 1:
12
34
Matrix 2:
56
78
Resultant Matrix:
68
10 12
PRACTICAL NO: 07 - Sum of Diagonal Elements

#include <iostream>
using namespace std;
#define SIZE 3
int sumDiagonal(int mat[SIZE][SIZE]) {
int sum = 0;
for(int i=0; i<SIZE; i++) {
sum += mat[i][i];
}
return sum;
}
int main() {
int mat[SIZE][SIZE] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
cout << "Matrix:" << endl;
for(int i=0; i<SIZE; i++) {
for(int j=0; j<SIZE; j++) {
cout << mat[i][j] << " ";
}
cout << endl;
}
int diagonalSum = sumDiagonal(mat);
cout << "Sum of diagonal elements: " << diagonalSum << endl;
return 0;
}
Output:
Matrix:
123
456
789
Sum of diagonal elements: 15
PRACTICAL NO: 08 - Matrix Multiplication

#include <iostream>
using namespace std;
#define ROWS1 2
#define COLS1 3
#define ROWS2 3
#define COLS2 2

void multiplyMatrices(int mat1[ROWS1][COLS1], int mat2[ROWS2][COLS2], int


result[ROWS1][COLS2]) {
for(int i=0; i<ROWS1; i++) {
for(int j=0; j<COLS2; j++) {
result[i][j] = 0;
for(int k=0; k<COLS1; k++) {
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
}

void printMatrix(int mat[][COLS2], int rows, int cols) {


for(int i=0; i<rows; i++) {
for(int j=0; j<cols; j++) {
cout << mat[i][j] << " ";
}
cout << endl;
}
}

int main() {
int mat1[ROWS1][COLS1] = {{1, 2, 3}, {4, 5, 6}};
int mat2[ROWS2][COLS2] = {{7, 8}, {9, 10}, {11, 12}};
int result[ROWS1][COLS2];
cout << "Matrix 1:" << endl;
printMatrix(mat1, ROWS1, COLS1);

cout << "Matrix 2:" << endl;


printMatrix(mat2, ROWS2, COLS2);

multiplyMatrices(mat1, mat2, result);

cout << "Resultant Matrix:" << endl;


printMatrix(result, ROWS1, COLS2);

return 0;
}

Output:
Matrix 1:
123
456
Matrix 2:
78
9 10
11 12
Resultant Matrix:
58 64
139 154
PRACTICAL NO: 09 - Factorial using Recursion

#include <iostream>
using namespace std;

int factorial(int n) {
if(n == 0 || n == 1) {
return 1;
}
return n * factorial(n-1);
}

int main() {
int num = 5;
cout << "Factorial of " << num << " is " << factorial(num) << endl;
return 0;
}

Output:
Factorial of 5 is 120
PRACTICAL NO: 10 - GCD using Recursion

#include <iostream>
using namespace std;
int gcd(int a, int b) {
if(b == 0) {
return a;
}
return gcd(b, a % b);
}
int main() {
int num1 = 48, num2 = 18;
cout << "GCD of " << num1 << " and " << num2 << " is " << gcd(num1, num2) << endl;
return 0;
}

Output:
GCD of 48 and 18 is 6
PRACTICAL NO: 11 - Stack Operations

#include <iostream>
using namespace std;
#define MAX 5

int stack[MAX];
int top = -1;

void push(int item) {


if(top == MAX-1) {
cout << "Stack Overflow" << endl;
return;
}
stack[++top] = item;
cout << item << " pushed to stack" << endl;
}

int pop() {
if(top == -1) {
cout << "Stack Underflow" << endl;
return -1;
}
return stack[top--];
}

void display() {
if(top == -1) {
cout << "Stack is empty" << endl;
return;
}
cout << "Stack elements: ";
for(int i=0; i<=top; i++) {
cout << stack[i] << " ";
}
cout << endl;
}

int main() {
push(10);
push(20);
push(30);
display();

cout << pop() << " popped from stack" << endl;
display();

push(40);
push(50);
push(60);
push(70); // This will cause stack overflow
display();

return 0;
}
Output:
10 pushed to stack
20 pushed to stack
30 pushed to stack
Stack elements: 10 20 30
30 popped from stack
Stack elements: 10 20
40 pushed to stack
50 pushed to stack
60 pushed to stack
Stack Overflow
Stack elements: 10 20 40 50 60
PRACTICAL NO: 12 - Queue Operations

#include <iostream>
using namespace std;
#define MAX 5

int queue[MAX];
int front = -1, rear = -1;

void enqueue(int item) {


if(rear == MAX-1) {
cout << "Queue Overflow" << endl;
return;
}
if(front == -1) {
front = 0;
}
queue[++rear] = item;
cout << item << " enqueued to queue" << endl;
}

int dequeue() {
if(front == -1 || front > rear) {
cout << "Queue Underflow" << endl;
return -1;
}
return queue[front++];
}

void display() {
if(front == -1 || front > rear) {
cout << "Queue is empty" << endl;
return;
}
cout << "Queue elements: ";
for(int i=front; i<=rear; i++) {
cout << queue[i] << " ";
}
cout << endl;
}

int main() {
enqueue(10);
enqueue(20);
enqueue(30);
display();

cout << dequeue() << " dequeued from queue" << endl;
display();

enqueue(40);
enqueue(50);
enqueue(60); // This will cause queue overflow
display();

return 0;
}

Output:
10 enqueued to queue
20 enqueued to queue
30 enqueued to queue
Queue elements: 10 20 30
10 dequeued from queue
Queue elements: 20 30
40 enqueued to queue
50 enqueued to queue
Queue Overflow
Queue elements: 20 30 40 50

You might also like