dsaAssignment[1]
dsaAssignment[1]
#include <cstdlib>
int main() {
int n, new_n;
cin >> n;
if (arr == nullptr) {
return 1;
if (c_arr == nullptr) {
1 | Page
GGNIMT
free(arr);
return 1;
if (arr == nullptr) {
free(c_arr);
return 1;
if (new_n > n) {
cout << "Enter " << new_n - n << " additional elements: ";
2 | Page
GGNIMT
free(arr);
free(c_arr);
return 0;
Output:
3 | Page
GGNIMT
return;
arr[pos] = value;
n++;
return;
n--;
int main() {
int arr[100], n;
cin >> n;
4 | Page
GGNIMT
traverse(arr, n);
traverse(arr, n);
deleteElement(arr, n, pos);
traverse(arr, n);
return 0;
Output:
5 | Page
GGNIMT
int main() {
cout << "Enter the number of elements in the first array: ";
int arr1[n1];
cout << "Enter the number of elements in the second array: ";
int arr2[n2];
int k = 0;
merged[k++] = arr1[i];
6 | Page
GGNIMT
merged[k++] = arr2[i];
return 0;
Output:
#include <iostream>
void addMatrices(int rows, int cols, int mat1[][10], int mat2[][10], int
result[][10]) {
}
7 | Page
GGNIMT
void subtractMatrices(int rows, int cols, int mat1[][10], int mat2[][10], int
result[][10]) {
int main() {
cout << "Enter the number of rows and columns of the matrices: ";
8 | Page
GGNIMT
return 0;
Output:
9 | Page
GGNIMT
#include <iostream>
result[i][j] = 0;
}}}}
int main() {
cout << "Enter the number of rows and columns of the first matrix: ";
int mat1[10][10];
}}
cout << "Enter the number of rows and columns of the second matrix: ";
if(cols1 != rows2) {
return 0;
10 | P a g e
GGNIMT
int mat2[10][10];
}}
int result[10][10];
11 | P a g e
GGNIMT
return 0;
Output:
if (arr[i] == target) {
12 | P a g e
GGNIMT
if (arr[i][j] == target) {
int main() {
int n, target;
cin >> n;
int arr1[n];
13 | P a g e
GGNIMT
if (result1D != -1) {
cout << "Element found at index " << result1D << " in 1D array.\n";
} else {
if (result2D != -1) {
cout << "Element found at position (" << row << ", " << col << ")
in 2D array.\n";
} else {
14 | P a g e
GGNIMT
return 0;
Output:
int minIndex = i;
minIndex = j;
15 | P a g e
GGNIMT
if (minIndex != i) {
swap(arr[i], arr[minIndex]);
int main() {
int n;
cin >> n;
int arr[n];
return 0;
Output:
16 | P a g e
GGNIMT
int j = i - 1;
arr[j + 1] = arr[j];
j--;
arr[j + 1] = key;
int main() {
int n;
17 | P a g e
GGNIMT
cin >> n;
int arr[n];
insertionSort(arr, n);
printArray(arr, n);
return 0;
Output:
swap(arr[i], arr[j]);
18 | P a g e
GGNIMT
quickSort(arr, pi + 1, high);
int main() {
int n;
cin >> n;
int arr[n];
19 | P a g e
GGNIMT
quickSort(arr, 0, n - 1);
printArray(arr, n);
return 0;
Output:
temp[k++] = arr[i++];
} else {
temp[k++] = arr[j++];
20 | P a g e
GGNIMT
temp[k++] = arr[i++];
temp[k++] = arr[j++];
arr[i] = temp[k];
21 | P a g e
GGNIMT
int main() {
int n;
cin >> n;
int arr[n];
mergeSort(arr, 0, n - 1);
printArray(arr, n);
return 0;
Output:
22 | P a g e
GGNIMT
int length = 0;
length++;
return length;
int main() {
char str[100];
cin.getline(str, 100);
cout << "The length of the string is: " << length << endl;
return 0;
Output:
#include <string>
if (str1 == str2) {
// Lexicographical comparison
else {
int main() {
getline(cin, str1);
getline(cin, str2);
if (result == 0) {
24 | P a g e
GGNIMT
else{
return 0;
Output:
If one string is greater than other:
int main() {
25 | P a g e
GGNIMT
cout << "First character: " << *p << endl; // Dereference the pointer to
get the value
return 0;
Output:
26 | P a g e
GGNIMT
#include <string>
// Base case: if start is greater than or equal to end, stop the recursion
return;
swap(sentence[start], sentence[end]);
int main() {
string sentence;
getline(cin, sentence);
return 0;
Output:
#include <sstream>
27 | P a g e
GGNIMT
#include <vector>
#include <string>
int main() {
string str;
string word;
// Remove duplicates
}}
28 | P a g e
GGNIMT
return 0;
Output:
int main() {
string str;
getline(cin, str);
vowels++;
} else {
consonants++;
digits++;
29 | P a g e
GGNIMT
spaces++;
cout << "Number of white spaces: " << spaces << endl;
return 0;
Output:
#include <string>
30 | P a g e
GGNIMT
int freq = 0;
if (c == ch) {
freq++;
highestFreq = freq;
highestChar = ch;
if (freq > 0 && freq < lowestFreq) { // Ensure that the character is
present
lowestFreq = freq;
lowestChar = ch;
cout << "Highest frequency character: " << highestChar << " with
frequency " << highestFreq << endl;
cout << "Lowest frequency character: " << lowestChar << " with
frequency " << lowestFreq << endl;
int main() {
string str;
getline(cin, str);
findFrequency(str);
31 | P a g e
GGNIMT
return 0;
Output:
class Stack {
private:
int arr[MAX];
int top;
public:
Stack() {
bool isFull() {
bool isEmpty() {
if (isFull()) {
cout << "Stack Overflow! Cannot push " << value << endl;
} else {
32 | P a g e
GGNIMT
arr[++top] = value;
int pop() {
if (isEmpty()) {
return -1;
} else {
return arr[top--];
int peek() {
if (isEmpty()) {
return -1;
} else {
return arr[top];
};
int main() {
Stack stack;
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(40);
stack.push(50);
33 | P a g e
GGNIMT
cout << "Top element is: " << stack.peek() << endl; // Should be 50
cout << "Popped element: " << stack.pop() << endl; // Should be 50
cout << "Top element is: " << stack.peek() << endl; // Should be 40
stack.push(60);
cout << "Top element is: " << stack.peek() << endl; // Should be 60
return 0;
Output:
class Stack {
private:
public:
Stack(int size) {
capacity = size;
34 | P a g e
GGNIMT
~Stack() {
bool isFull() {
bool isEmpty() {
if (isFull()) {
cout << "Stack Overflow! Cannot push " << value << endl;
} else {
arr[++top] = value;
int pop() {
if (isEmpty()) {
return -1;
} else {
return arr[top--];
int peek() {
if (isEmpty()) {
35 | P a g e
GGNIMT
return -1;
} else {
return arr[top];
} }};
int main() {
int size;
Stack stack(size);
stack.push(10);
stack.push(20);
stack.push(50);
cout << "Top element is: " << stack.peek() << endl;
cout << "Top element is: " << stack.peek() << endl;
stack.push(60);
cout << "Top element is: " << stack.peek() << endl;
return 0;
Output:
36 | P a g e
GGNIMT
class MultipleStacks {
private:
int *top; // Pointer to track the top element for each stack
public:
capacity = size;
numStacks = n;
top = new int[numStacks]; // Array to hold the top index for each
stack
top[i] = -1;
~MultipleStacks() {
37 | P a g e
GGNIMT
delete[] arr;
delete[] top;
if (isFull(stackIndex)) {
cout << "Stack " << stackIndex + 1 << " Overflow! Cannot push "
<< value << endl;
} else {
top[stackIndex]++;
cout << value << " pushed to stack " << stackIndex + 1 << endl;
if (isEmpty(stackIndex)) {
cout << "Stack " << stackIndex + 1 << " Underflow! Cannot pop."
<< endl;
return -1;
} else {
top[stackIndex]--;
return poppedValue;
38 | P a g e
GGNIMT
if (isEmpty(stackIndex)) {
cout << "Stack " << stackIndex + 1 << " is empty!" << endl;
return -1;
} else {
} } };
int main() {
cout << "Top element of stack 1: " << stacks.peek(0) << endl;
cout << "Top element of stack 2: " << stacks.peek(1) << endl;
cout << "Top element of stack 3: " << stacks.peek(2) << endl;
cout << "Popped element from stack 1: " << stacks.pop(0) << endl;
cout << "Popped element from stack 2: " << stacks.pop(1) << endl;
cout << "Popped element from stack 3: " << stacks.pop(2) << endl;
return 0;
Output:
39 | P a g e
GGNIMT
#include <stack>
return 0;
stack<char> s;
if (isalnum(c)) {
} else if (c == '(') {
} else if (c == ')') {
40 | P a g e
GGNIMT
postfix += s.top();
} else { // Operator
postfix += s.top();
while (!s.empty()) {
postfix += s.top();
return postfix;
int main() {
string infix;
return 0;
Output:
41 | P a g e
GGNIMT
class Queue {
int* arr;
public:
~Queue() {
delete[] arr;
if (isFull()) {
return;
42 | P a g e
GGNIMT
arr[++rear] = val;
int dequeue() {
if (isEmpty()) {
return -1;
else front++;
return val;
void display() {
if (isEmpty()) {
return;
}};
int main() {
Queue q(5);
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.enqueue(40);
q.enqueue(50);
q.display();
43 | P a g e
GGNIMT
q.display();
return 0;
Output:
struct Node {
int data;
Node* next;
};
class Queue {
private:
public:
Queue() {
~Queue() {
44 | P a g e
GGNIMT
dequeue();
} }
bool isEmpty() {
newNode->data = value;
newNode->next = nullptr;
if (rear == nullptr) {
return;
rear->next = newNode;
rear = newNode;
int dequeue() {
if (isEmpty()) {
return -1;
front = front->next;
if (front == nullptr) {
delete temp;
45 | P a g e
GGNIMT
return value;
void display() {
if (isEmpty()) {
return;
temp = temp->next;
} };
int main() {
Queue q;
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.enqueue(40);
q.display();
q.display();
return 0;
Output:
46 | P a g e
GGNIMT
class CircularQueue {
private:
int *arr;
public:
~CircularQueue() {
delete[] arr;
bool isEmpty() {
bool isFull() {
if (isFull()) {
47 | P a g e
GGNIMT
return;
arr[rear] = value;
int dequeue() {
if (isEmpty()) {
return -1;
return value;
void display() {
if (isEmpty()) {
return;
int i = front;
while (i != rear) {
i = (i + 1) % size;
48 | P a g e
GGNIMT
};
int main() {
CircularQueue q(5);
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.enqueue(40);
q.enqueue(50);
q.display();
q.display();
q.enqueue(60);
q.display();
return 0;
Output:
49 | P a g e
GGNIMT
class Deque {
private:
int *arr;
public:
Deque(int n) {
size = n;
~Deque() {
delete[] arr;
bool isEmpty() {
bool isFull() {
if (isFull()) {
return;
50 | P a g e
GGNIMT
arr[front] = value;
if (isFull()) {
return;
arr[rear] = value;
int deleteFront() {
if (isEmpty()) {
return -1;
return value;
int deleteRear() {
if (isEmpty()) {
return -1;
51 | P a g e
GGNIMT
return value;
void display() {
if (isEmpty()) {
return;
int i = front;
while (i != rear) {
i = (i + 1) % size;
};
int main() {
Deque dq(5);
dq.insertRear(10);
dq.insertRear(20);
dq.insertFront(5);
dq.display();
cout << "Deleted from front: " << dq.deleteFront() << endl;
dq.display();
dq.insertRear(30);
dq.insertFront(2);
dq.display();
cout << "Deleted from rear: " << dq.deleteRear() << endl;
dq.display();
52 | P a g e
GGNIMT
return 0;
Output:
#include <vector>
class PriorityQueue {
private:
vector<int> heap;
index = (index - 1) / 2;
53 | P a g e
GGNIMT
if (largest != index) {
swap(heap[index], heap[largest]);
heapifyDown(largest);
public:
heap.push_back(value);
heapifyUp(heap.size() - 1);
int remove() {
heap[0] = heap.back();
heap.pop_back();
heapifyDown(0);
return root;
void display() {
for (int val : heap) cout << val << " ";
} };
int main() {
54 | P a g e
GGNIMT
PriorityQueue pq;
pq.insert(10);
pq.insert(30);
pq.insert(20);
pq.insert(40);
pq.display();
pq.display();
return 0;
Output:
int factorial(int n) {
if (n <= 1) {
return 1;
} else {
// Recursive call
} }
55 | P a g e
GGNIMT
int main() {
int num;
if (num < 0) {
cout << "Factorial is not defined for negative numbers!" << endl;
} else {
cout << "Factorial of " << num << " is: " << factorial(num) << endl;
return 0;
Output:
int fibonacci(int n) {
if (n <= 1) {
} else {
56 | P a g e
GGNIMT
int main() {
int n;
cout << "Enter the number of terms for Fibonacci series: ";
cin >> n;
cout << "Fibonacci series up to " << n << " terms: ";
return 0;
Output:
void mergearrays(int arr1[], int arr2[], int size1, int size2, int
mergedarray[])
mergedarray[i] = arr1[i];
mergedarray[size1 + i] = arr2[i];
57 | P a g e
GGNIMT
int main()
int arr1[size1];
int arr2[size2];
int mergedarray[mergedsize];
return 0;
58 | P a g e
GGNIMT
Output:
#include <iostream>
void mergeSortedArrays(int arr1[], int size1, int arr2[], int size2, int
mergedArray[]) {
int i = 0, j = 0, k = 0;
mergedArray[k++] = arr1[i++];
} else {
mergedArray[k++] = arr2[j++];
mergedArray[k++] = arr1[i++];
59 | P a g e
GGNIMT
mergedArray[k++] = arr2[j++];
}}
int main() {
cout << "Enter the size of the first sorted array: ";
int arr1[size1];
cout << "Enter the size of the second sorted array: ";
int arr2[size2];
int mergedArray[mergedSize];
mergedArray);
return 0;
60 | P a g e
GGNIMT
Output:
61 | P a g e