Dsa File
Dsa File
CODE:
#include <iostream>
int start = 0;
swap(arr[start], arr[end]);
start++;
end--;
int main() {
printArray(arr, size);
reverseArray(arr, size);
printArray(arr, size);
return 0;
}
OUTPUT:
EXPERIMENT-2
CODE:
#include <iostream>
#include <unordered_map>
frequency[arr[i]]++;
int nonRepeatedCount = 0;
if (pair.second == 1) {
nonRepeatedCount++;
return nonRepeatedCount;
int main() {
cout << "Number of non-repeated elements: " << nonRepeatedCount << endl;
return 0;
OUTPUT:
EXPERIMENT-3
AIM: WAP with function to swap two numbers using call by reference.
CODE:
#include <iostream>
int temp = a;
a = b;
b = temp;
int main() {
int x = 5;
int y = 10;
cout << "Before swapping: x = " << x << ", y = " << y << endl;
swap(x, y);
cout << "After swapping: x = " << x << ", y = " << y << endl;
return 0;
OUTPUT:
EXPERIMENT-4
AIM: WAP to identify the missing numbers in a given Array within the range [1...N].
CODE:
#include <iostream>
#include <vector>
present[arr[i]] = true;
if (!present[i]) {
int main() {
int arr[] = {3, 7, 1, 2, 8, 4, 5};
return 0;
OUTPUT:
EXPERIMENT-5
AIM: Write a function that accepts as input a string and determines the frequency of
occurences of each of the distinct characters in string. Test your function using suitable data.
CODE:
#include <iostream>
#include <unordered_map>
frequency[ch]++;
cout << "'" << pair.first << "' : " << pair.second << endl;
int main() {
string input = "hello world";
characterFrequency(input);
return 0;
OUTPUT:
EXPERIMENT-6
AIM: WAP to find the Median of the elements after merging the two sorted Arrays of same size.
CODE:
#include <iostream>
#include <vector>
int i = 0, j = 0, k = 0;
merged[k++] = arr1[i++];
} else {
merged[k++] = arr2[j++];
}
while (i < size) {
merged[k++] = arr1[i++];
merged[k++] = arr2[j++];
double median;
if (merged.size() % 2 == 0) {
} else {
return median;
int main() {
cout << "Median of the merged arrays: " << median << endl;
return 0;
OUTPUT:
EXPERIMENT-7
AIM: WAP to Create a new file, Open an existing file, read the file to search a given word, write
into the file and close the file.
CODE:
#include <iostream>
#include <fstream>
#include <string>
ofstream outFile(filename);
if (outFile.is_open()) {
outFile.close();
ifstream inFile(filename);
string line;
if (inFile.is_open()) {
if (line.find(word) != string::npos) {
cout << "Word '" << word << "' found in line: " << line << endl;
found = true;
inFile.close();
if (!found) {
cout << "Word '" << word << "' not found in the file.\n";
if (outFile.is_open()) {
outFile.close();
int main() {
createFile(filename);
searchWordInFile(filename, wordToSearch);
writeToFile(filename, textToWrite);
searchWordInFile(filename, "additional");
return 0;
OUTPUT:
EXPERIMENT-8
AIM: WAP to implement Dequeue using Arrays with all the basic operations.
CODE:
#include <iostream>
class Dequeue {
int* arr;
int front;
int rear;
int capacity;
public:
Dequeue(int size) {
capacity = size;
front = -1;
rear = 0;
~Dequeue() {
delete[] arr;
if (isFull()) {
return;
arr[front] = key;
if (isFull()) {
return;
arr[rear] = key;
void deleteFront() {
if (isEmpty()) {
return;
if (front == rear) {
front = -1;
rear = 0;
void deleteRear() {
if (isEmpty()) {
return;
if (front == rear) {
front = -1;
rear = 0;
int getFront() {
if (isEmpty()) {
return -1;
return arr[front];
int getRear() {
if (isEmpty() || rear == 0) {
return -1;
bool isFull() {
bool isEmpty() {
void display() {
if (isEmpty()) {
cout << "Dequeue is empty\n";
return;
};
int main() {
Dequeue dq(5);
dq.insertRear(10);
dq.insertRear(20);
dq.insertFront(30);
dq.insertFront(40);
dq.display();
dq.deleteRear();
dq.display();
dq.deleteFront();
dq.display();
return 0;
OUTPUT:
EXPERIMENT-9
AIM: WAP to implement Merge Sort on 1D array of Student structures (contains student_name,
student_roll_no, total_marks) with key as student_roll_no.
CODE:
#include <iostream>
#include <string>
struct Student {
string student_name;
int student_roll_no;
float total_marks;
};
arr[k++] = L[i++];
} else {
arr[k++] = R[j++];
arr[k++] = L[i++];
arr[k++] = R[j++];
delete[] L;
delete[] R;
int main() {
Student students[] = {
{"Alice", 3, 85.5},
{"Bob", 1, 90.0},
{"Charlie", 2, 75.0},
{"David", 5, 95.5},
{"Eve", 4, 80.0}
};
printStudents(students, size);
printStudents(students, size);
return 0;
OUTPUT:
EXPERIMENT - 10
AIM: WAP to implement Linear search and Binary search on 1D array of Integers.
CODE :
#include <iostream>
#include <vector>
#include <algorithm>
if (arr[i] == target) {
int low = 0;
if (arr[mid] == target) {
low = mid + 1;
else {
high = mid - 1;
}
return -1; // Return -1 if the element is not found
int main() {
vector<int> arr = {3, 6, 8, 12, 14, 17, 20, 25, 28, 30};
if (resultLinear != -1)
cout << "Linear Search: Element " << target << " found at index " << resultLinear << endl;
else
if (resultBinary != -1)
cout << "Binary Search: Element " << target << " found at index " << resultBinary << endl;
else
return 0;
OUTPUT :
EXPERIMENT – 11
#include <vector>
#include <string>
int n = arr.size();
int j = i - 1;
arr[j + 1] = arr[j];
j--;
arr[j + 1] = key;
int n = arr.size();
int minIndex = i;
minIndex = j;
swap(arr[i], arr[minIndex]);
}
int main() {
insertionSort(arr1);
printArray(arr1);
selectionSort(arr2);
printArray(arr2);
return 0;
OUTPUT :
EXPERIMENT-12
CODE:
#include <iostream>
#include <vector>
int i = low - 1;
i++;
swap(arr[i], arr[j]);
return i + 1;
int main() {
printArray(arr);
return 0;
OUTPUT:
EXPERIMENT-13
AIM: WAP to store and display a Lower-Right triangular matrix in RMO and CMO fashion.
CODE:
#include <iostream>
if (j >= i)
else
}
void displayCMO(int* arr, int n) {
if (j >= i)
else
int main() {
int n;
cin >> n;
cout << "Enter the elements for the lower-right triangular matrix:" << endl;
displayRMO(arr, n);
displayCMO(arr, n);
delete[] arr;
return 0;
OUTPUT :
EXPERIMENT – 14
AIM: WAP to store and display a Lower-Left triangular matrix in RMO and CMO fashion.
CODE:
#include <iostream>
if (j <= i)
else
if (j <= i)
else
int main() {
int n;
cin >> n;
cout << "Enter the elements for the upper-left triangular matrix:" << endl;
displayRMO(arr, n);
displayCMO(arr, n);
delete[] arr;
return 0;
}
OUTPUT :
EXPERIMENT-15
AIM : WAP to store and display an Upper-Left triangular matrix in RMO and CMO fashion
CODE:
#include <iostream>
if (j <= i)
else
if (j <= i)
else
int main() {
int n;
cin >> n;
cout << "Enter the elements for the upper-left triangular matrix:" << endl;
displayRMO(arr, n);
displayCMO(arr, n);
delete[] arr;
return 0;
OUTPUT :
EXPERIMENT-16
AIM: WAP to store and display a Z matrix in RMO and CMO fashion. (Z matrix contains non-zero
elements in first row, last row and right diagonal only.)
CODE:
#include <iostream>
int index = 0;
else
int index = 0;
if (i == 0 || i == n - 1 || j == n - 1 - i)
else
int main() {
int n;
cin >> n;
int size = 2 * n - 1;
displayRMO(arr, n);
displayCMO(arr, n);
delete[] arr;
return 0;
OUTPUT:
EXPERIMENT-17
CODE:
cpp
Copy code
#include <iostream>
#include <stack>
#include <cctype>
stack<int> s;
if (isdigit(ch))
s.push(ch - '0');
else {
switch (ch) {
return s.top();
int main() {
string expression;
return 0;
OUTPUT:
EXPERIMENT- 18
CODE:
#include <iostream>
#include <stack>
#include <string>
int main() {
string str;
getline(cin, str);
stack<char> s;
string reversed;
reversed += s.top();
s.pop();
if (reversed == str) {
} else {
return 0;
OUTPUT:
EXPERIMENT-19
AIM: WAP to transform infix expression into equivalent postfix expression using stack. Also use
the user defined operators, $,#, etc, with appropiate priorities. Eg. A+(B*C D/E$F)*G)*H, {*,/} > $
> {+,-}
CODE:
#include <iostream>
#include <stack>
#include <string>
#include <cctype>
#include <map>
return 0;
bool isOperator(char c) {
stack<char> s;
string postfix;
if (isalnum(c)) {
postfix += c;
} else if (c == '(') {
s.push(c);
} else if (c == ')') {
postfix += s.top();
s.pop();
s.pop();
} else if (isOperator(c)) {
postfix += s.top();
s.pop();
s.push(c);
}
while (!s.empty()) {
postfix += s.top();
s.pop();
return postfix;
int main() {
string infix;
getline(cin, infix);
return 0;
OUTPUT :
EXPERIMENT-20
AIM: WAP wihch gives the solution to the Tower of Hanoi problem for n disks. Test the program
using: a) N=3, b) N=4.
CODE:
#include <iostream>
if (n == 1) {
cout << "Move disk 1 from rod " << from_rod << " to rod " << to_rod << endl;
return;
cout << "Move disk " << n << " from rod " << from_rod << " to rod " << to_rod << endl;
int main() {
int N;
cout << "Enter the number of disks (e.g., N=3 or N=4): ";
cin >> N;
cout << "Solution for Tower of Hanoi with " << N << " disks:\n";
return 0;
OUTPUT:
EXPERIMENT-21
#define N 10
return (x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == 1);
sol[x][y] = 1;
return true;
if (isSafe(maze, x, y, m, n)) {
sol[x][y] = 1;
sol[x][y] = 0;
return false;
}
if (!solveMazeUtil(maze, 0, 0, m, n, sol)) {
return false;
printSolution(sol, m, n);
return true;
int main() {
int maze[N][N] = {
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 1, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 1, 1, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 1, 1, 0},
{0, 0, 0, 0, 0, 0, 1, 0, 1, 1},
{0, 1, 1, 1, 1, 1, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 1, 1, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
};
solveMaze(maze, m, n);
return 0;
OUTPUT:
EXPERIMENT-22
AIM: WAP to reverse a singly linked list using one auxillary pointer. And try without using any
auxillary pointer.
CODE:
#include <iostream>
struct Node {
int data;
Node* next;
};
head = newNode;
return;
while (temp->next) {
temp = temp->next;
temp->next = newNode;
while (head->next) {
head->next = temp->next;
prev = temp;
head = prev;
while (head) {
head = head->next;
}
int main() {
insertAtEnd(head, 1);
insertAtEnd(head, 2);
insertAtEnd(head, 3);
insertAtEnd(head, 4);
printList(head);
reverseWithoutAux(head);
printList(head);
return 0;
OUTPUT:
EXPERIMENT-23
AIM:
CODE:
#include <iostream>
struct Node {
int data;
Node* next;
};
if (!head) {
head = newNode;
return;
while (temp->next) {
temp = temp->next;
temp->next = newNode;
if (!head || !head->next) {
cout << "The list is too short to have a second last node." << endl;
return nullptr;
while (temp->next->next) {
temp = temp->next;
return temp;
}
void printList(Node* head) {
while (head) {
head = head->next;
int main() {
insertAtEnd(head, 1);
insertAtEnd(head, 2);
insertAtEnd(head, 3);
insertAtEnd(head, 4);
printList(head);
if (secondLast) {
cout << "The second last node is: " << secondLast->data << endl;
return 0;
OUTPUT:
EXPERIMENT-24
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
if (!head) {
head = newNode;
return;
temp->next = newNode;
while (head) {
print = !print;
head = head->next;
int main() {
insertAtEnd(head, 1);
insertAtEnd(head, 2);
insertAtEnd(head, 3);
insertAtEnd(head, 4);
insertAtEnd(head, 5);
insertAtEnd(head, 6);
printAlternateNodes(head);
return 0;
OUTPUT:
EXXPERIMENT-25
CODE:
#include <iostream>
struct Node {
int data;
Node* next;
};
if (!head) {
head = newNode;
return;
temp->next = newNode;
slow = slow->next;
fast = fast->next->next;
return slow;
Node* result;
result = left;
} else {
result = right;
return result;
}
middle->next = nullptr;
while (head) {
head = head->next;
int main() {
insertAtEnd(head, 4);
insertAtEnd(head, 2);
insertAtEnd(head, 1);
insertAtEnd(head, 3);
printList(head);
head = mergeSort(head);
printList(head);
return 0;
OUTPUT:
EXPERIMENT- 26
AIM : WAP to create a Circular Singly Linked List for (a) Inserting a node, before the node with
key givenkey
CODE:
#include <iostream>
struct Node {
int data;
Node* next;
};
if (!head) {
head = newNode;
newNode->next = head;
return;
temp->next = newNode;
newNode->next = head;
if (!head) return;
if (head->data == key) {
temp->next = newNode;
newNode->next = head;
head = newNode;
return;
current = current->next;
if (current->next->data == key) {
newNode->next = current->next;
current->next = newNode;
if (!head) return;
do {
int main() {
insertAtEnd(head, 1);
insertAtEnd(head, 2);
insertAtEnd(head, 3);
insertAtEnd(head, 4);
printList(head);
insertBefore(head, 3, 5);
printList(head);
return 0;
OUTPUT:
EXPERIMENT-27
AIM: WAP to create a Circular Singly Linked List for Inserting a node, after the node with key
givenkey,
CODE:
#include <iostream>
int data;
Node* next;
};
if (!head) {
head = newNode;
newNode->next = head;
return;
temp->next = newNode;
newNode->next = head;
if (!head) return;
do {
if (current->data == key) {
newNode->next = current->next;
current->next = newNode;
return;
current = current->next;
if (!head) return;
do {
temp = temp->next;
int main() {
insertAtEnd(head, 1);
insertAtEnd(head, 2);
insertAtEnd(head, 3);
insertAtEnd(head, 4);
printList(head);
insertAfter(head, 2, 5);
printList(head);
return 0;
OUTPUT:
EXPERIMENT-28
CODE:
#include <iostream>
#include <vector>
struct NaryNode {
int data;
vector<NaryNode*> children;
};
struct BinaryTreeNode {
int data;
BinaryTreeNode* left;
BinaryTreeNode* right;
};
newRoot->left = convertToBinaryTree(root->children[0]);
current->right = convertToBinaryTree(root->children[i]);
current = current->right;
}
return newRoot;
if (!root) return;
printBinaryTree(root->left);
printBinaryTree(root->right);
int main() {
root->children.push_back(new NaryNode(2));
root->children.push_back(new NaryNode(3));
root->children.push_back(new NaryNode(4));
root->children[0]->children.push_back(new NaryNode(5));
root->children[0]->children.push_back(new NaryNode(6));
printBinaryTree(binaryTreeRoot);
return 0;
OUTPUT:
EXPERIMENT-29
AIM: WAP to represent an arithematic expression in binary tree format.
CODE:
#include <iostream>
#include <stack>
#include <string>
struct TreeNode {
string data;
TreeNode* left;
TreeNode* right;
};
stack<TreeNode*> nodes;
if (isalnum(ch)) {
} else {
operatorNode->left = left;
operatorNode->right = right;
nodes.push(operatorNode);
return nodes.top();
}
void inorderTraversal(TreeNode* root) {
if (!root) return;
inorderTraversal(root->left);
inorderTraversal(root->right);
int main() {
inorderTraversal(root);
return 0;
OUTPUT:
EXPERIMENT-30
CODE:
#include <iostream>
#include <vector>
class Graph {
int V;
vector<vector<int>> adj;
void DFS(int u, int parent[], int low[], int disc[], vector<pair<int, int>>& cutEdges);
public:
Graph(int V);
void findCutEdges();
};
Graph::Graph(int V) {
this->V = V;
adj.resize(V);
adj[u].push_back(v);
adj[v].push_back(u);
void Graph::DFS(int u, int parent[], int low[], int disc[], vector<pair<int, int>>& cutEdges) {
if (disc[v] == -1) {
parent[v] = u;
cutEdges.push_back({u, v});
} else if (v != parent[u]) {
}
}
void Graph::findCutEdges() {
disc[i] = -1;
low[i] = -1;
parent[i] = -1;
if (disc[i] == -1) {
cout << edge.first << " -- " << edge.second << endl;
delete[] disc;
delete[] low;
delete[] parent;
int main() {
Graph g(5);
g.addEdge(0, 1);
g.addEdge(1, 2);
g.addEdge(1, 3);
g.addEdge(3, 4);
g.addEdge(2, 4);
g.findCutEdges();
return 0;
OUTPUT:
EXPERIMENT-31
AIM: WAP to implement doubly linked list for Inserting a node, before the node with givenkey.
CODE:
#include <iostream>
struct Node {
int data;
Node* prev;
Node* next;
};
class DoublyLinkedList {
private:
Node* head;
public:
DoublyLinkedList() : head(nullptr) {}
if (!head) {
head = newNode;
return;
while (temp->next) {
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
if (!head) return;
if (head->data == key) {
newNode->next = head;
head->prev = newNode;
head = newNode;
return;
current = current->next;
}
if (current) {
newNode->next = current;
newNode->prev = current->prev;
if (current->prev) {
current->prev->next = newNode;
current->prev = newNode;
void printList() {
while (temp) {
temp = temp->next;
};
int main() {
DoublyLinkedList dll;
dll.insertAtEnd(1);
dll.insertAtEnd(2);
dll.insertAtEnd(3);
dll.insertAtEnd(4);
dll.printList();
dll.insertBefore(3, 5);
return 0;
OUTPUT:
EXPERIMENT-32
AIM: WAP to implement doubly linked list of Removing a node with particular key value
CODE:
#include <iostream>
struct Node {
int data;
Node* prev;
Node* next;
};
class DoublyLinkedList {
private:
Node* head;
public:
DoublyLinkedList() : head(nullptr) {}
if (!head) {
head = newNode;
return;
while (temp->next) {
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
if (!head) return;
while (current) {
if (current->data == key) {
if (current->prev) {
current->prev->next = current->next;
} else {
head = current->next;
if (current->next) {
current->next->prev = current->prev;
delete current;
return;
current = current->next;
}
void printList() {
while (temp) {
temp = temp->next;
};
int main() {
DoublyLinkedList dll;
dll.insertAtEnd(1);
dll.insertAtEnd(2);
dll.insertAtEnd(3);
dll.insertAtEnd(4);
dll.printList();
dll.removeNode(3);
dll.printList();
return 0;
OUTPUT: