LAB MANUAL DSA (C++)
Program 1: Linear Search in Array (C++)
#include <iostream>
using namespace std;
int main() {
int a[10], item, flag = 0;
cout << "Enter the data in the array: ";
for(int i = 0; i < 10; i++) {
cin >> a[i];
}
cout << "Enter the element to be searched: ";
cin >> item;
for(int i = 0; i < 10; i++) {
if(item == a[i]) {
flag = 1;
cout << "Element Found at Position = " << i << endl;
break;
}
}
if(flag == 0)
cout << "Element Not Found" << endl;
return 0;
}
Program 2: Linear Search in 2D Array (C++)
#include <iostream>
using namespace std;
int main() {
int a[3][3], item, flag = 0;
cout << "Enter the data in the array:" << endl;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
cin >> a[i][j];
}
}
cout << "Enter the element to be searched: ";
cin >> item;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
if(item == a[i][j]) {
flag = 1;
cout << "Element found at position = " << i << ", " << j << endl;
}
LAB MANUAL DSA (C++) 1
}
}
if(flag == 0)
cout << "Element Not Found" << endl;
return 0;
}
Program 3: Merge Two Sorted Arrays (C++)
#include <iostream>
using namespace std;
int main() {
int a[10], b[10], c[20], i, j, k = 0, n, m;
cout << "Enter size of Array A: ";
cin >> n;
cout << "Enter the data in Array A:" << endl;
for(i = 0; i < n; i++) {
cin >> a[i];
}
cout << "Enter size of Array B: ";
cin >> m;
cout << "Enter the data in Array B:" << endl;
for(j = 0; j < m; j++) {
cin >> b[j];
}
i = j = 0;
while(i < n && j < m) {
if(a[i] < b[j])
c[k++] = a[i++];
else
c[k++] = b[j++];
}
while(i < n)
c[k++] = a[i++];
while(j < m)
c[k++] = b[j++];
cout << "Merged array is: ";
for(i = 0; i < m + n; i++)
cout << c[i] << " ";
cout << endl;
return 0;
}
Program 4: Matrix Operations (C++)
#include <iostream>
using namespace std;
LAB MANUAL DSA (C++) 2
int main() {
int a[3][3], b[3][3], c[3][3], d[3][3];
cout << "Enter the data in Matrix A:" << endl;
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
cin >> a[i][j];
cout << "Enter the data in Matrix B:" << endl;
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
cin >> b[i][j];
// Addition
cout << "Addition of two Matrix A and B is:" << endl;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
c[i][j] = a[i][j] + b[i][j];
cout << c[i][j] << "\t";
}
cout << endl;
}
// Subtraction
cout << "Subtraction of two Matrix A and B is:" << endl;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
c[i][j] = a[i][j] - b[i][j];
cout << c[i][j] << "\t";
}
cout << endl;
}
// Transpose of the last result matrix
cout << "Transpose of Matrix C is:" << endl;
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
d[j][i] = c[i][j];
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
cout << d[i][j] << "\t";
}
cout << endl;
}
// Multiplication
cout << "Multiplication of Matrix A and B is:" << endl;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
c[i][j] = 0;
for(int k = 0; k < 3; k++) {
c[i][j] += a[i][k] * b[k][j];
}
cout << c[i][j] << "\t";
}
cout << endl;
}
LAB MANUAL DSA (C++) 3
return 0;
}
Program 5: Swapping Using Call by Value and Call by Reference (C++)
#include <iostream>
using namespace std;
void swapByValue(int x, int y) {
int temp = x;
x = y;
y = temp;
cout << "\nNumbers after swapping by value are:\n";
cout << "a = " << x << "\nb = " << y << endl;
}
void swapByRef(int &x, int &y) {
int temp = x;
x = y;
y = temp;
}
int main() {
int a, b;
cout << "Enter the two numbers: ";
cin >> a >> b;
swapByValue(a, b);
swapByRef(a, b);
cout << "\nNumber after swapping by Reference:\n";
cout << "a = " << a << "\nb = " << b << endl;
return 0;
}
✅ Program 6: String Operations (Concatenation, Length, Copy, Reverse)
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char a[100], b[100], c[100];
cout << "Enter the First String: ";
cin >> a;
cout << "Enter the Second String: ";
cin >> b;
strcat(a, b);
cout << "\nConcatenation of String a and b is: " << a;
int l = strlen(a);
cout << "\nLength of String is: " << l;
strcpy(c, a);
LAB MANUAL DSA (C++) 4
cout << "\nThe Copied String is: " << c;
strrev(a); // This is not available in standard C++, so we write manually
int len = strlen(a);
for (int i = 0; i < len / 2; i++) {
char temp = a[i];
a[i] = a[len - i - 1];
a[len - i - 1] = temp;
}
cout << "\nReverse of String is: " << a;
return 0;
}
✅ Program 7(a): Iterative Binary Search
#include <iostream>
using namespace std;
int main() {
int a[100], n, mid, beg, end, item, loc = -1;
cout << "Enter the number of elements: ";
cin >> n;
cout << "Enter the elements in ascending order:\n";
for(int i = 0; i < n; i++) {
cin >> a[i];
}
cout << "Enter the element to be searched: ";
cin >> item;
beg = 0;
end = n - 1;
while(beg <= end) {
mid = (beg + end) / 2;
if(item == a[mid]) {
loc = mid;
break;
} else if(a[mid] < item) {
beg = mid + 1;
} else {
end = mid - 1;
}
}
if(loc == -1)
cout << "Element Not Present";
else
cout << "Element found at index: " << loc;
return 0;
}
✅ Program 7(b): Recursive Binary Search (Fixed)
LAB MANUAL DSA (C++) 5
#include <iostream>
using namespace std;
int binarySearch(int a[], int beg, int end, int item) {
if(beg <= end) {
int mid = (beg + end) / 2;
if(a[mid] == item)
return mid;
else if(item > a[mid])
return binarySearch(a, mid + 1, end, item);
else
return binarySearch(a, beg, mid - 1, item);
}
return -1;
}
int main() {
int a[100], n, item;
cout << "Enter the number of items in the array: ";
cin >> n;
cout << "Enter the data in array:\n";
for(int i = 0; i < n; i++) {
cin >> a[i];
}
cout << "Enter the element to be searched: ";
cin >> item;
int loc = binarySearch(a, 0, n - 1, item);
if(loc == -1)
cout << "Element not Found";
else
cout << "Element Found at position: " << loc;
return 0;
}
✅ Program 8: Bubble Sort
#include <iostream>
using namespace std;
void bubble(int a[], int n) {
int temp;
for(int i = 1; i < n; i++) {
for(int p = 0; p < n - i; p++) {
if(a[p] > a[p + 1]) {
temp = a[p];
a[p] = a[p + 1];
a[p + 1] = temp;
}
}
}
cout << "\nData After Bubble Sort:\n";
LAB MANUAL DSA (C++) 6
for(int i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
}
int main() {
int a[100], n;
cout << "Enter the number of items in the array: ";
cin >> n;
cout << "Enter the data in the array:\n";
for(int i = 0; i < n; i++) {
cin >> a[i];
}
bubble(a, n);
return 0;
}
✅ Program 9: Selection Sort (Fixed)
#include <iostream>
using namespace std;
int minIndex(int a[], int lb, int ub) {
int m = lb;
for(int i = lb + 1; i < ub; i++) {
if(a[i] < a[m]) {
m = i;
}
}
return m;
}
void selectionSort(int a[], int n) {
for(int i = 0; i < n; i++) {
int loc = minIndex(a, i, n);
swap(a[i], a[loc]);
}
cout << "\nData After Selection Sort:\n";
for(int i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
}
int main() {
int a[100], n;
cout << "Enter the number of items in the array: ";
cin >> n;
cout << "Enter the data in the array:\n";
for(int i = 0; i < n; i++) {
cin >> a[i];
}
LAB MANUAL DSA (C++) 7
selectionSort(a, n);
return 0;
}
✅ Program 10: Insertion Sort
#include <iostream>
using namespace std;
void insertionSort(int a[], int n) {
int temp, j;
for(int i = 1; i < n; i++) {
temp = a[i];
j = i - 1;
while(j >= 0 && a[j] > temp) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = temp;
}
cout << "\nData After Insertion Sort:\n";
for(int i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
}
int main() {
int a[100], n;
cout << "Enter the number of items in the array: ";
cin >> n;
cout << "Enter the data in the array:\n";
for(int i = 0; i < n; i++) {
cin >> a[i];
}
insertionSort(a, n);
return 0;
}
✅ Program 11: Quick Sort in C++
#include <iostream>
using namespace std;
void quicksort(int[], int, int);
int partition(int[], int, int);
int main() {
int a[20], i, n;
cout << "Enter the size of array: ";
cin >> n;
cout << "Enter the elements in the array:\n";
LAB MANUAL DSA (C++) 8
for(i = 0; i < n; i++) {
cin >> a[i];
}
quicksort(a, 0, n - 1);
cout << "Sorted array using Quick Sort:\n";
for(i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << endl;
return 0;
}
void quicksort(int a[], int lb, int ub) {
int mid;
if(lb < ub) {
mid = partition(a, lb, ub);
quicksort(a, lb, mid - 1);
quicksort(a, mid + 1, ub);
}
}
int partition(int a[], int lb, int ub) {
int pivot = a[lb];
int start = lb + 1;
int end = ub;
int temp;
while(end >= start) {
while(a[start] < pivot && start <= ub)
start++;
while(a[end] > pivot && end > lb)
end--;
if(start < end) {
temp = a[start];
a[start] = a[end];
a[end] = temp;
}
}
temp = a[lb];
a[lb] = a[end];
a[end] = temp;
return end;
}
✅ Program 12:Merge Sort in C++
#include <iostream>
using namespace std;
void mergesort(int[], int, int);
void merge(int[], int, int, int);
int main() {
int a[20], i, n;
cout << "Enter the number of elements: ";
cin >> n;
LAB MANUAL DSA (C++) 9
cout << "Enter the elements:\n";
for(i = 0; i < n; i++) {
cin >> a[i];
}
mergesort(a, 0, n - 1);
cout << "Sorted array using Merge Sort:\n";
for(i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << endl;
return 0;
}
void mergesort(int a[], int lb, int ub) {
int mid;
if(lb < ub) {
mid = (lb + ub) / 2;
mergesort(a, lb, mid);
mergesort(a, mid + 1, ub);
merge(a, lb, mid + 1, ub);
}
}
void merge(int a[], int lb, int mid, int ub) {
int b[20];
int p1 = lb, p2 = mid, p3 = lb;
while((p1 < mid) && (p2 <= ub)) {
if(a[p1] <= a[p2])
b[p3++] = a[p1++];
else
b[p3++] = a[p2++];
}
while(p1 < mid) {
b[p3++] = a[p1++];
}
while(p2 <= ub) {
b[p3++] = a[p2++];
}
for(int k = lb; k < p3; k++) {
a[k] = b[k];
}
}
✅ Program 13: Stack Implementation Using Array
#include <iostream>
#include <cstdlib> // for exit()
using namespace std;
void push();
void pop();
void display();
LAB MANUAL DSA (C++) 10
int top;
int a[5];
int main() {
int choice;
char ch;
top = -1;
do {
cout << "\n\t1. PUSH";
cout << "\n\t2. POP";
cout << "\n\t3. DISPLAY";
cout << "\n\t4. EXIT";
cout << "\nEnter your choice: ";
cin >> choice;
switch(choice) {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
cout << "\nBAD CHOICE";
}
cout << "\nDo you want to continue (y/n)? ";
cin >> ch;
} while(ch == 'y' || ch == 'Y');
return 0;
}
void push() {
int item;
if(top == 4) {
cout << "STACK IS FULL";
} else {
cout << "Enter the item to be inserted: ";
cin >> item;
top = top + 1;
a[top] = item;
}
}
void pop() {
if(top == -1) {
cout << "STACK IS EMPTY";
} else {
int item = a[top];
top = top - 1;
cout << item << " is deleted";
}
LAB MANUAL DSA (C++) 11
}
void display() {
if(top == -1) {
cout << "STACK IS EMPTY";
} else {
cout << "\nStack elements are:\n";
for(int i = top; i >= 0; i--) {
cout << a[i] << "\n";
}
}
}
✅ Program 14: Queue Implementation Using Array
#include <iostream>
#include <cstdlib> // for exit()
using namespace std;
void insert();
void delet();
void display();
int front, rear;
int q[5];
int main() {
int choice;
char ch;
front = -1;
rear = -1;
do {
cout << "\n\t1. INSERT";
cout << "\n\t2. DELETE";
cout << "\n\t3. DISPLAY";
cout << "\n\t4. EXIT";
cout << "\nEnter your choice: ";
cin >> choice;
switch(choice) {
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
cout << "\nBAD CHOICE";
}
cout << "\nDo you want to continue (y/n)? ";
cin >> ch;
LAB MANUAL DSA (C++) 12
} while(ch == 'y' || ch == 'Y');
return 0;
}
void insert() {
int item;
if (((front == 0 && rear == 4) || (front == rear + 1))) {
cout << "QUEUE IS FULL\n";
} else {
cout << "Enter the element: ";
cin >> item;
if (front == -1) {
front = 0;
rear = 0;
} else if (rear == 4) {
rear = 0;
} else {
rear++;
}
q[rear] = item;
}
}
void delet() {
if (front == -1) {
cout << "QUEUE IS EMPTY\n";
} else {
int item = q[front];
if (front == rear) {
front = -1;
rear = -1;
} else if (front == 4) {
front = 0;
} else {
front++;
}
cout << item << " is deleted\n";
}
}
void display() {
if (front == -1) {
cout << "QUEUE IS EMPTY\n";
} else {
cout << "Queue elements:\n";
int i = front;
if (front <= rear) {
while (i <= rear) {
cout << q[i] << "\n";
i++;
}
} else {
while (i < 5) {
cout << q[i] << "\n";
LAB MANUAL DSA (C++) 13
i++;
}
i = 0;
while (i <= rear) {
cout << q[i] << "\n";
i++;
}
}
}
}
✅ Program 15: Linked List Implementation
#include <iostream>
#include <cstdlib>
using namespace std;
struct Node {
int info;
Node* next;
};
Node* start = nullptr;
// Function Prototypes
void insert();
void insertAtBeginning();
void insertAtMiddle();
void insertAtEnd();
void remove();
void deleteAtBeginning();
void deleteAtMiddle();
void deleteAtEnd();
void display();
int countNodes();
int main() {
int ch, cnt;
while (true) {
cout << "\n********** MENU **********";
cout << "\n1. Insert";
cout << "\n2. Delete";
cout << "\n3. Display";
cout << "\n4. Count";
cout << "\n5. Exit";
cout << "\nEnter your choice: ";
cin >> ch;
switch (ch) {
case 1: insert(); break;
case 2: remove(); break;
case 3: display(); break;
case 4:
cnt = countNodes();
cout << "\nThe number of nodes: " << cnt << endl;
break;
case 5: exit(0);
default: cout << "\nInvalid Choice!\n";
LAB MANUAL DSA (C++) 14
}
}
return 0;
}
void insert() {
int ch1;
cout << "\nInsert Options:";
cout << "\n1. Insert at the beginning";
cout << "\n2. Insert in the middle";
cout << "\n3. Insert at the end";
cout << "\nEnter your choice: ";
cin >> ch1;
switch (ch1) {
case 1: insertAtBeginning(); break;
case 2: insertAtMiddle(); break;
case 3: insertAtEnd(); break;
default: cout << "\nInvalid Choice!\n";
}
}
void insertAtBeginning() {
int data;
Node* newNode = new Node;
cout << "\nEnter information to insert at the beginning: ";
cin >> data;
newNode->info = data;
newNode->next = start;
start = newNode;
}
void insertAtMiddle() {
int pos, data;
cout << "\nEnter the position after which new node is to be inserted: ";
cin >> pos;
Node* temp = start;
for (int i = 1; i < pos && temp != nullptr; ++i) {
temp = temp->next;
}
if (temp == nullptr) {
cout << "\nPosition out of bounds!";
return;
}
Node* newNode = new Node;
cout << "\nEnter data for the new node: ";
cin >> data;
newNode->info = data;
newNode->next = temp->next;
temp->next = newNode;
}
void insertAtEnd() {
LAB MANUAL DSA (C++) 15
int data;
Node* newNode = new Node;
cout << "\nEnter information to insert at the end: ";
cin >> data;
newNode->info = data;
newNode->next = nullptr;
if (start == nullptr) {
start = newNode;
} else {
Node* temp = start;
while (temp->next != nullptr)
temp = temp->next;
temp->next = newNode;
}
}
void remove() {
int ch2;
cout << "\nDelete Options:";
cout << "\n1. Delete at the beginning";
cout << "\n2. Delete in the middle";
cout << "\n3. Delete at the end";
cout << "\nEnter your choice: ";
cin >> ch2;
switch (ch2) {
case 1: deleteAtBeginning(); break;
case 2: deleteAtMiddle(); break;
case 3: deleteAtEnd(); break;
default: cout << "\nInvalid Choice!\n";
}
}
void deleteAtBeginning() {
if (start == nullptr) {
cout << "\nList is empty!";
return;
}
Node* temp = start;
start = start->next;
delete temp;
cout << "\nNode deleted from beginning.";
}
void deleteAtMiddle() {
if (start == nullptr) {
cout << "\nList is empty!";
return;
}
int value;
cout << "\nEnter the value to be deleted: ";
cin >> value;
Node* current = start;
LAB MANUAL DSA (C++) 16
Node* previous = nullptr;
while (current != nullptr && current->info != value) {
previous = current;
current = current->next;
}
if (current == nullptr) {
cout << "\nValue not found!";
return;
}
if (previous == nullptr) {
// First node match
start = start->next;
} else {
previous->next = current->next;
}
delete current;
cout << "\nNode deleted.";
}
void deleteAtEnd() {
if (start == nullptr) {
cout << "\nList is empty!";
return;
}
Node* current = start;
Node* previous = nullptr;
while (current->next != nullptr) {
previous = current;
current = current->next;
}
if (previous == nullptr) {
// Only one node
delete start;
start = nullptr;
} else {
previous->next = nullptr;
delete current;
}
cout << "\nNode deleted from end.";
}
void display() {
if (start == nullptr) {
cout << "\nList is empty!";
return;
}
Node* temp = start;
cout << "\n\n*********** LINKED LIST ***********\n";
while (temp != nullptr) {
cout << temp->info << " -> ";
temp = temp->next;
LAB MANUAL DSA (C++) 17
}
cout << "NULL\n";
}
int countNodes() {
int count = 0;
Node* temp = start;
while (temp != nullptr) {
++count;
temp = temp->next;
}
return count;
}
LAB MANUAL DSA (C++) 18