Write a program to store the elements in 1-D array and perform the
opera ons like searching, sor ng and reversing the elements. [Menu
Driven]
#include <iostream>
using namespace std;
int main() {
int a[50], n, ch, i, j, temp, x;
cout << "Enter number of elements: ";
cin >> n;
cout << "Enter elements:\n";
for(i = 0; i < n; i++) {
cin >> a[i];
do {
cout << "\n MENU \n1. Display Array\n2. Search Elements\n3. Sort Array\n4. Reverse
Array\n5. Exit\n";
cout << "Enter your choice: ";
cin >> ch;
if(ch == 1) {
for(i = 0; i < n; i++) {
cout << a[i] << " ";
cout << "\n";
else if(ch == 2) {
cout << "Enter number to search: ";
cin >> x;
for(i = 0; i < n; i++) {
if(a[i] == x) {
cout << "Found at posi on " << i + 1 << "\n";
break;
}
if(i == n)
cout << "Not found!\n";
else if(ch == 3) {
for(i = 0; i < n - 1; i++) {
for(j = i + 1; j < n; j++) {
if(a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
cout << "Array sorted.\n";
else if(ch == 4) {
for(i = 0; i < n / 2; i++) {
temp = a[i];
a[i] = a[n - 1 - i];
a[n - 1 - i] = temp;
cout << "Array reversed.\n";
else if(ch == 5) {
cout << "Exi ng...\n";
else {
cout << "Invalid choice!\n";
}
} while(ch != 5);
return 0;
Read the two arrays from the user and merge them and display the
elements in sorted order. [Menu Driven]
#include <iostream>
using namespace std;
int main() {
int a[50], b[50], c[100];
int n1, n2, n, i, j, temp;
cout << "Enter size of first array: ";
cin >> n1;
cout << "Enter " << n1 << " elements:\n";
for(i = 0; i < n1; i++)
cin >> a[i];
cout << "Enter size of second array: ";
cin >> n2;
cout << "Enter " << n2 << " elements:\n";
for(i = 0; i < n2; i++)
cin >> b[i];
for(i = 0; i < n1; i++)
c[i] = a[i];
for(j = 0; j < n2; j++)
c[n1 + j] = b[j];
n = n1 + n2;
for(i = 0; i < n-1; i++) {
for(j = 0; j < n-i-1; j++) {
if(c[j] > c[j+1]) {
temp = c[j];
c[j] = c[j+1];
c[j+1] = temp;
}}}
cout << "Sorted merged array:\n";
for(i = 0; i < n; i++)
cout << c[i] << " ";
return 0;
Write a program to create a single linked list and display the node
elements in reverse order.
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
int main() {
Node* head = NULL;
Node* temp, *newNode;
int n, val;
cout << "enter no of nodes: ";
cin >> n;
for(int i = 0; i < n; i++) {
cout << "Enter value: ";
cin >> val;
newNode = new Node();
newNode->data = val;
newNode->next = NULL;
if(head == NULL)
head = newNode;
else {
temp = head;
while(temp->next != NULL)
temp = temp->next;
temp->next = newNode;
} }
int arr[100], count = 0;
temp = head;
while(temp != NULL) {
arr[count] = temp->data;
count++;
temp = temp->next;
cout << "Linked list in reverse:\n";
for(int i = count - 1; i >= 0; i--) {
cout << arr[i] << " ";
return 0;
Write a program to search the elements in the linked list and display the
same.
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
int main() {
Node *first, *second, *third;
int key;
first = new Node();
second = new Node();
third = new Node();
cout << "Enter 3 values:\n";
cin >> first->data;
cin >> second->data;
cin >> third->data;
first->next = second;
second->next = third;
third->next = NULL;
cout << "Enter element to search: ";
cin >> key;
if(first->data == key || second->data == key || third->data == key)
cout << "Element found.\n";
else
cout << "Element not found.\n";
return 0;
Write a program to create double linked list and sort the elements in the
linked list.
#include <iostream>
using namespace std;
struct Node {
int data;
Node* prev;
Node* next;
};
int main() {
Node *head = NULL, *tail = NULL, *temp, *newNode;
int n, val;
cout << "no of nodes ";
cin >> n;
for (int i = 0; i < n; i++) {
cout << "Enter value: ";
cin >> val;
newNode = new Node();
newNode->data = val;
newNode->prev = NULL;
newNode->next = NULL;
if (head == NULL) {
head = tail = newNode;
} else {
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}}
for (Node* i = head; i != NULL; i = i->next) {
for (Node* j = i->next; j != NULL; j = j->next) {
if (i->data > j->data) {
int tempData = i->data;
i->data = j->data;
j->data = tempData;
} } }
cout << "Sorted list:\n";
temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
return 0;
Write a program to implement the concept of Stack with Push, Pop,
Display and Exit opera ons.
#include <iostream>
using namespace std;
int stack[50], top = -1;
int main() {
int choice, value;
while (true) {
cout << "\n1. Push\n2. Pop\n3. Display\n4. Exit\nEnter choice: ";
cin >> choice;
if (choice == 1) {
cout << "Enter value: ";
cin >> value;
if (top == 49)
cout << "Stack Full\n";
else
stack[++top] = value;
else if (choice == 2) {
if (top == -1)
cout << "Stack Empty\n";
else
cout << "Popped: " << stack[top--] << "\n";
else if (choice == 3) {
if (top == -1)
cout << "Stack Empty\n";
else {
cout << "Stack: ";
for (int i = top; i >= 0; i--)
cout << stack[i] << " ";
cout << "\n";
}}
else if (choice == 4)
break;
else
cout << "Invalid choice\n";
return 0;
Write a program to convert an infix expression to pos ix and prefix
conversion.
#include <iostream>
#include <stack>
#include <algorithm>
#include <cctype> // for isalnum
using namespace std;
int precedence(char op) {
if(op == '+' || op == '-') return 1;
if(op == '*' || op == '/') return 2;
return 0;
bool isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/');
string infixToPos ix(string infix) {
stack<char> s;
string pos ix = "";
int i;
for(i = 0; i < infi[Link](); i++) {
char c = infix[i];
if(isalnum(c)) {
pos ix += c;
else if(c == '(') {
[Link](c);
else if(c == ')') {
while(![Link]() && [Link]() != '(') {
pos ix += [Link]();
[Link]();
if(![Link]()) [Link](); // pop '('
else if(isOperator(c)) {
while(![Link]() && precedence([Link]()) >= precedence(c)) {
pos ix += [Link]();
[Link]();
[Link](c);
}}
while(![Link]()) {
pos ix += [Link]();
[Link]();
return pos ix;
}
string infixToPrefix(string infix) {
reverse(infi[Link](), infi[Link]());
for(int i = 0; i < infi[Link](); i++) {
if(infix[i] == '(') infix[i] = ')';
else if(infix[i] == ')') infix[i] = '(';
string revPos ix = infixToPos ix(infix);
reverse(revPos [Link](), revPos [Link]());
return revPos ix;
int main() {
string infix;
cout << "Enter infix expression (e.g., (A+B)*C): ";
cin >> infix;
string pos ix = infixToPos ix(infix);
string prefix = infixToPrefix(infix);
cout << "Pos ix: " << pos ix << endl;
cout << "Prefix : " << prefix << endl;
return 0;
Write a program to perform the Matrix addi on, Mul plica on and
Transpose Opera on. [Menu Driven]
#include <iostream>
using namespace std;
const int MAX = 10;
void inputMatrix(int mat[][MAX], int &rows, int &cols, char name) {
cout << "Enter rows and columns for Matrix " << name << ": ";
cin >> rows >> cols;
cout << "Enter elements:\n";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << name << "[" << i << "][" << j << "]: ";
cin >> mat[i][j];
}}}
void displayMatrix(int mat[][MAX], int rows, int cols, string tle) {
cout << "\n" << tle << ":\n";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << mat[i][j] << "\t";
cout << endl;
}}
void addMatrices(int A[][MAX], int B[][MAX], int C[][MAX], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
C[i][j] = A[i][j] + B[i][j];
}}}
void mul plyMatrices(int A[][MAX], int B[][MAX], int C[][MAX], int r1, int c1, int c2) {
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
C[i][j] = 0;
for (int k = 0; k < c1; k++) {
C[i][j] += A[i][k] * B[k][j];
}}}}
void transposeMatrix(int A[][MAX], int T[][MAX], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
T[j][i] = A[i][j];
}}}
int main() {
int A[MAX][MAX], B[MAX][MAX], C[MAX][MAX];
int rowsA, colsA, rowsB, colsB;
int choice;
do {
cout << "\n=== MATRIX OPERATIONS ===";
cout << "\n1. Input Matrices";
cout << "\n2. Matrix Addi on";
cout << "\n3. Matrix Mul plica on";
cout << "\n4. Matrix Transpose";
cout << "\n5. Exit";
cout << "\nEnter choice: ";
cin >> choice;
switch(choice) {
case 1:
inputMatrix(A, rowsA, colsA, 'A');
inputMatrix(B, rowsB, colsB, 'B');
break;
case 2:
if (rowsA != rowsB || colsA != colsB) {
cout << "Error: Matrices must have same size!\n";
} else {
addMatrices(A, B, C, rowsA, colsA);
displayMatrix(C, rowsA, colsA, "A + B");
break;
case 3:
if (colsA != rowsB) {
cout << "Error: Columns of A must equal rows of B!\n";
} else {
mul plyMatrices(A, B, C, rowsA, colsA, colsB);
displayMatrix(C, rowsA, colsB, "A x B");
}
break;
case 4:
cout << "Transpose of A:\n";
transposeMatrix(A, C, rowsA, colsA);
displayMatrix(C, colsA, rowsA, "A Transpose");
cout << "Transpose of B:\n";
transposeMatrix(B, C, rowsB, colsB);
displayMatrix(C, colsB, rowsB, "B Transpose");
break;
case 5:
cout << "Goodbye!\n";
break;
default:
cout << "Invalid choice!\n";
} while (choice != 5);
return 0;
Write a program to implement Tower of Hanoi problem.
#include <iostream>
using namespace std;
void towerOfHanoi(int n, char source, char helper, char des na on) {
if (n == 1) {
cout << "Move disk 1 from " << source << " to " << des na on << endl;
return;
towerOfHanoi(n - 1, source, des na on, helper);
cout << "Move disk " << n << " from " << source << " to " << des na on << endl;
towerOfHanoi(n - 1, helper, source, des na on);
}
int main() {
int n;
cout << "Enter number of disks: ";
cin >> n;
towerOfHanoi(n, 'A', 'B', 'C'); // A = source, B = helper, C = des na on
return 0;
Write a program to implement the concept of Queue with Insert, Delete,
Display and Exit opera ons.
#include <iostream>
using namespace std;
#define SIZE 5
int queue[SIZE], front = -1, rear = -1;
void insert(int value) {
if (rear == SIZE - 1) {
cout << "Queue Overflow!" << endl;
} else {
if (front == -1) front = 0;
queue[++rear] = value;
cout << value << " inserted." << endl;
}}
void remove() {
if (front == -1 || front > rear) {
cout << "Queue Underflow!" << endl;
} else {
cout << queue[front] << " deleted." << endl;
front++;
}}
void display() {
if (front == -1 || front > rear) {
cout << "Queue is empty!" << endl;
} else {
cout << "Queue: ";
for (int i = front; i <= rear; i++) {
cout << queue[i] << " ";
cout << endl;
}}
int main() {
int choice, value;
while (true) {
cout << "\n--- Queue Menu ---\n";
cout << "1. Insert\n";
cout << "2. Delete\n";
cout << "3. Display\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value: ";
cin >> value;
insert(value);
break;
case 2:
remove();
break;
case 3:
display();
break;
case 4:
return 0;
default:
cout << "Invalid choice!" << endl;
}}}
Write a program to implement the concept of Circular Queue.
#include <iostream>
using namespace std;
const int MAX = 5;
int q[MAX], front = -1, rear = -1;
void insert() {
int val;
cout << "Enter value: ";
cin >> val;
if ((rear + 1) % MAX == front) {
cout << "Queue is full!\n";
} else if (front == -1) {
front = rear = 0;
q[rear] = val;
cout << val << " inserted.\n";
} else {
rear = (rear + 1) % MAX;
q[rear] = val;
cout << val << " inserted.\n";
}}
void deleteq() {
if (front == -1) {
cout << "Queue is empty!\n";
} else if (front == rear) {
cout << q[front] << " deleted.\n";
front = rear = -1;
} else {
cout << q[front] << " deleted.\n";
front = (front + 1) % MAX;
}}
void display() {
if (front == -1) {
cout << "Queue is empty!\n";
} else {
cout << "Queue: ";
int i = front;
while (true) {
cout << q[i] << " ";
if (i == rear) break;
i = (i + 1) % MAX;
cout << "\n";
}}
int main() {
int choice;
do {
cout << "\n*** Circular Queue Menu ***\n";
cout << "1. Insert\n2. Delete\n3. Display\n4. Exit\n";
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case 1: insert(); break;
case 2: deleteq(); break;
case 3: display(); break;
case 4: cout << "Exi ng...\n"; break;
default: cout << "Invalid choice!\n";
} while (choice != 4);
return 0;
Write a program to implement the concept of Deque.
#include <iostream>
using namespace std;
const int MAX = 5;
int q[MAX];
int front = -1, rear = -1;
bool isFull() {
return (front == (rear + 1) % MAX);
bool isEmpty() {
return (front == -1);
void insertRear() {
int val;
cout << "Enter value: ";
cin >> val;
if (isFull()) {
cout << "Queue is full.\n";
return;
if (isEmpty()) {
front = rear = 0;
} else {
rear = (rear + 1) % MAX;
}
q[rear] = val;
cout << "Inserted at rear.\n";
void insertFront() {
int val;
cout << "Enter value: ";
cin >> val;
if (isFull()) {
cout << "Queue is full.\n";
return;
if (isEmpty()) {
front = rear = 0;
} else {
front = (front - 1 + MAX) % MAX;
q[front] = val;
cout << "Inserted at front.\n";
void deleteFront() {
if (isEmpty()) {
cout << "Queue is empty.\n";
return;
cout << "Deleted from front: " << q[front] << "\n";
if (front == rear) {
front = rear = -1;
} else {
front = (front + 1) % MAX;
}
}
void deleteRear() {
if (isEmpty()) {
cout << "Queue is empty.\n";
return;
cout << "Deleted from rear: " << q[rear] << "\n";
if (front == rear) {
front = rear = -1;
} else {
rear = (rear - 1 + MAX) % MAX;
}}
void display() {
if (isEmpty()) {
cout << "Queue is empty.\n";
return;
cout << "Queue: ";
int i = front;
while (true) {
cout << q[i] << " ";
if (i == rear) break;
i = (i + 1) % MAX;
cout << "\n";
int main() {
int choice;
do {
cout << "\n*** Deque Menu ***\n"
<< "1. Insert Rear\n"
<< "2. Insert Front\n"
<< "3. Delete Front\n"
<< "4. Delete Rear\n"
<< "5. Display\n"
<< "6. Exit\n"
<< "Enter choice: ";
cin >> choice;
switch (choice) {
case 1: insertRear(); break;
case 2: insertFront(); break;
case 3: deleteFront(); break;
case 4: deleteRear(); break;
case 5: display(); break;
case 6: cout << "Exi ng...\n"; break;
default: cout << "Invalid choice!\n";
} while (choice != 6);
return 0;
Write a program to implement bubble sort.
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter number of elements: ";
cin >> n;
int arr[n];
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++)
cin >> arr[i];
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}}}
cout << "Sorted array: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
return 0;
Write a program to implement selec on sort.
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter number of elements: ";
cin >> n;
int arr[n];
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++)
cin >> arr[i];
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;
}
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
cout << "Sorted array: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
return 0;
Write a program to implement inser on sort.
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter number of elements: ";
cin >> n;
int arr[n];
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++)
cin >> arr[i];
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
arr[j + 1] = key;
}
cout << "Sorted array: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
return 0;
Write a program to implement merge sort.
#include <iostream>
using namespace std;
void merge(int arr[], int le , int mid, int right) {
int n1 = mid - le + 1;
int n2 = right - mid;
int L[n1], R[n2];
for (int i = 0; i < n1; i++) L[i] = arr[le + i];
for (int j = 0; j < n2; j++) R[j] = arr[mid + 1 + j];
int i = 0, j = 0, k = le ;
while (i < n1 && j < n2) {
if (L[i] <= R[j])
arr[k++] = L[i++];
else
arr[k++] = R[j++];
while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];
void mergeSort(int arr[], int le , int right) {
if (le < right) {
int mid = (le + right) / 2;
mergeSort(arr, le , mid);
mergeSort(arr, mid + 1, right);
merge(arr, le , mid, right);
}}
int main() {
int n;
cout << "Enter number of elements: ";
cin >> n;
int arr[n];
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++)
cin >> arr[i];
mergeSort(arr, 0, n - 1);
cout << "Sorted array: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
return 0;
Write a program to search the element using sequen al search.
#include <iostream>
using namespace std;
int main() {
int n, key;
cout << "Enter number of elements: ";
cin >> n;
int arr[n];
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++)
cin >> arr[i];
cout << "Enter element to search: ";
cin >> key;
int pos = -1;
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
pos = i;
break;
}}
if (pos == -1)
cout << key << " not found in the array.\n";
else
cout << key << " found at posi on " << pos + 1 << ".\n";
return 0;
Write a program to search the element using binary search.
#include <iostream>
using namespace std;
int main() {
int n, key;
cout << "Enter number of elements: ";
cin >> n;
int arr[n];
cout << "Enter " << n << " sorted elements: ";
for (int i = 0; i < n; i++)
cin >> arr[i];
cout << "Enter element to search: ";
cin >> key;
int low = 0, high = n - 1, mid, pos = -1;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] == key) {
pos = mid;
break;
} else if (arr[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}}
if (pos == -1)
cout << key << " not found in the array.\n";
else
cout << key << " found at posi on " << pos + 1 << ".\n";
return 0;
Write a program to create the tree and display the elements.
#include <iostream>
using namespace std;
struct Node {
int data;
Node* le ;
Node* right;
};
Node* createNode(int value) {
Node* newNode = new Node;
newNode->data = value;
newNode->le = NULL;
newNode->right = NULL;
return newNode;
}
void display(Node* root) {
if (root == NULL) return;
cout << root->data << " ";
display(root->le );
display(root->right);
int main() {
Node* root = createNode(1);
root->le = createNode(2);
root->right = createNode(3);
root->le ->le = createNode(4);
root->le ->right = createNode(5);
root->right->le = createNode(6);
root->right->right = createNode(7);
cout << "Tree Elements (Preorder Traversal): ";
display(root);
return 0;
Write a program to construct the binary tree.
#include <iostream>
using namespace std;
struct Node {
int data;
Node* le ;
Node* right;
Node(int value) {
data = value;
le = right = NULL;
}
};
Node* createTree() {
Node* root = new Node(1);
root->le = new Node(2);
root->right = new Node(3);
root->le ->le = new Node(4);
root->le ->right = new Node(5);
root->right->le = new Node(6);
return root;
void inorder(Node* root) {
if (root == NULL) return;
inorder(root->le );
cout << root->data << " ";
inorder(root->right);
void preorder(Node* root) {
if (root == NULL) return;
cout << root->data << " ";
preorder(root->le );
preorder(root->right);
void postorder(Node* root) {
if (root == NULL) return;
postorder(root->le );
postorder(root->right);
cout << root->data << " ";
int main() {
Node* root = createTree();
cout << "Inorder Traversal: ";
inorder(root);
cout << endl;
cout << "Preorder Traversal: ";
preorder(root);
cout << endl;
cout << "Postorder Traversal: ";
postorder(root);
cout << endl;
return 0;
Write a program for inorder, postorder and preorder traversal of tree.
#include <iostream>
using namespace std;
struct Node {
int data;
Node* le ;
Node* right;
Node(int value) {
data = value;
le = NULL;
right = NULL;
};
void preorder(Node* root) {
if (!root) return;
cout << root->data << " ";
preorder(root->le );
preorder(root->right);
}
void inorder(Node* root) {
if (!root) return;
inorder(root->le );
cout << root->data << " ";
inorder(root->right);
void postorder(Node* root) {
if (!root) return;
postorder(root->le );
postorder(root->right);
cout << root->data << " ";
int main() {
Node* root = new Node(1);
root->le = new Node(2);
root->right = new Node(3);
root->le ->le = new Node(4);
root->le ->right = new Node(5);
root->right->le = new Node(6);
root->right->right = new Node(7);
cout << "Preorder: "; preorder(root); cout << endl;
cout << "Inorder: "; inorder(root); cout << endl;
cout << "Postorder: "; postorder(root); cout << endl;
return 0;
Write a program to insert the element into maximum heap.
#include <iostream>
using namespace std;
void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
void maxHeapify(int arr[], int n, int i) {
int largest = i;
int le = 2 * i + 1;
int right = 2 * i + 2;
if (le < n && arr[le ] > arr[largest])
largest = le ;
if (right < n && arr[right] > arr[largest])
largest = right;
if (largest != i) {
swap(arr[i], arr[largest]);
maxHeapify(arr, n, largest);
}}
void insertMaxHeap(int arr[], int &n, int element) {
n++;
int i = n - 1;
arr[i] = element;
while (i > 0 && arr[(i - 1) / 2] < arr[i]) {
swap(arr[i], arr[(i - 1) / 2]);
i = (i - 1) / 2;
}}
int main() {
int arr[100];
int n = 0;
int num_elements;
cout << "Enter the number of elements to insert into the max heap: ";
cin >> num_elements;
cout << "Enter " << num_elements << " elements:\n";
for (int i = 0; i < num_elements; i++) {
int element;
cin >> element;
insertMaxHeap(arr, n, element);
cout << "Max Heap a er inser ons: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
cout << endl;
return 0;
Write a program to insert the element into minimum heap.
#include <iostream>
using namespace std;
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
void insertMinHeap(int arr[], int *n, int value) {
(*n)++; // increase heap size
int i = (*n) - 1; // insert at last posi on
arr[i] = value;
while (i > 0 && arr[(i - 1) / 2] > arr[i]) {
swap(&arr[i], &arr[(i - 1) / 2]);
i = (i - 1) / 2;
}
}
int main() {
int arr[100];
int n = 0;
int num;
cout << "Enter number of elements: ";
cin >> num;
cout << "Enter " << num << " elements:\n";
for (int i = 0; i < num; i++) {
int x;
cin >> x;
insertMinHeap(arr, &n, x);
cout << "Min Heap: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
cout << endl;
return 0;
Write a program to implement the collision technique.
Write a program to implement the concept of linear probing.
#include <iostream>
using namespace std;
#define SIZE 10
void insert(int hashTable[], int key) {
int index = key % SIZE;
int i = 0;
while (hashTable[(index + i) % SIZE] != -1) { // find empty slot
i++;
hashTable[(index + i) % SIZE] = key;
void display(int hashTable[]) {
cout << "Hash Table:\n";
for (int i = 0; i < SIZE; i++) {
if (hashTable[i] != -1)
cout << i << " --> " << hashTable[i] << endl;
else
cout << i << " --> " << "empty" << endl;
}}
int main() {
int hashTable[SIZE];
for (int i = 0; i < SIZE; i++) hashTable[i] = -1; // ini alize table
int n;
cout << "Enter number of keys: ";
cin >> n;
cout << "Enter " << n << " keys:\n";
for (int i = 0; i < n; i++) {
int key;
cin >> key;
insert(hashTable, key);
display(hashTable);
return 0;
Write a program to generate the adjacency matrix.
#include <iostream>
using namespace std;
int main() {
int n, e;
cout << "Enter number of ver ces: ";
cin >> n;
cout << "Enter number of edges: ";
cin >> e;
int adj[20][20] = {0};
cout << "Enter edges (u v):\n";
for (int i = 0; i < e; i++) {
int u, v;
cin >> u >> v;
adj[u][v] = 1;
adj[v][u] = 1;
cout << "\nAdjacency Matrix:\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << adj[i][j] << " ";
cout << endl;
return 0;