Dsa Lab Practicals
Dsa Lab Practicals
Session: 2024-25
2. To transpose a 2D array.
Output: -
EXPERIMENT - 3
Aim: - To implement stack using array.
Code:
#include <iostream>
using namespace std;
#define MAX 100
class Stack {
int arr[MAX];
int top;
public:
Stack() { top = -1; }
bool isFull() {
return top == MAX - 1;
}
bool isEmpty() {
return top == -1;
}
void push(int value) {
if (isFull()) {
cout << "Stack Overflow! Cannot push " << value << endl;
return;
}
arr[++top] = value;
cout << value << " pushed to stack.\n";
}
void pop() {
if (isEmpty()) {
cout << "Stack Underflow! Cannot pop.\n";
return;
}
cout << arr[top--] << " popped from stack.\n";
}
void peek() {
if (isEmpty()) {
cout << "Stack is empty.\n";
return;
}
cout << "Top element is: " << arr[top] << endl;
}
void display() {
if (isEmpty()) {
cout << "Stack is empty.\n";
return;
}
cout << "Stack elements:\n";
for (int i = top; i >= 0; i--)
cout << arr[i] << " ";
cout << endl;
}
};
int main() {
Stack stack;
int choice, value;
do {
cout << "\n--- Stack Menu ---\n";
cout << "1. Push\n2. Pop\n3. Peek\n4. Display\n5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value to push: ";
cin >> value;
stack.push(value);
break;
case 2:
stack.pop();
break;
case 3:
stack.peek();
break;
case 4:
stack.display();
break;
case 5:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice.\n";
}
} while (choice != 5);
return 0;
}
Output:
EXPERIMENT - 4
Aim: - To implement queue using array.
Code:
#include <iostream>
using namespace std;
#define MAX 100
class Queue {
int arr[MAX];
int front, rear;
public:
Queue() {
front = -1;
rear = -1;
}
bool isEmpty() {
return front == -1 || front > rear;
}
bool isFull() {
return rear == MAX - 1;
}
void dequeue() {
if (isEmpty()) {
cout << "Queue Underflow! Cannot dequeue.\n";
return;
}
cout << arr[front++] << " dequeued from queue.\n";
}
void peek() {
if (isEmpty()) {
cout << "Queue is empty.\n";
return;
}
cout << "Front element is: " << arr[front] << endl;
}
void display() {
if (isEmpty()) {
cout << "Queue is empty.\n";
return;
}
cout << "Queue elements: ";
for (int i = front; i <= rear; i++)
cout << arr[i] << " ";
cout << endl;
}
};
int main() {
Queue queue;
int choice, value;
do {
cout << "\n--- Queue Menu ---\n";
cout << "1. Enqueue\n2. Dequeue\n3. Peek\n4. Display\n5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value to enqueue: ";
cin >> value;
queue.enqueue(value);
break;
case 2:
queue.dequeue();
break;
case 3:
queue.peek();
break;
case 4:
queue.display();
break;
case 5:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice.\n";
}
} while (choice != 5);
return 0;
}
Output: -
EXPERIMENT -5
Aim: - To implement circular queue using array.
Code:
#include <iostream>
using namespace std;
#define SIZE 5
class CircularQueue {
int arr[SIZE];
int front, rear;
public:
CircularQueue() {
front = -1;
rear = -1;
}
bool isFull() {
return (front == (rear + 1) % SIZE);
}
bool isEmpty() {
return (front == -1);
}
void dequeue() {
if (isEmpty()) {
cout << "Queue Underflow! Cannot dequeue.\n";
return;
}
cout << arr[front] << " dequeued.\n";
if (front == rear) {
// Only one element was present
front = rear = -1;
} else {
front = (front + 1) % SIZE;
}
}
void peek() {
if (isEmpty()) {
cout << "Queue is empty.\n";
return;
}
cout << "Front element: " << arr[front] << endl;
}
void display() {
if (isEmpty()) {
cout << "Queue is empty.\n";
return;
}
cout << "Queue elements: ";
int i = front;
while (true) {
cout << arr[i] << " ";
if (i == rear)
break;
i = (i + 1) % SIZE;
}
cout << endl;
}
};
int main() {
CircularQueue cq;
int choice, value;
do {
cout << "\n--- Circular Queue Menu ---\n";
cout << "1. Enqueue\n2. Dequeue\n3. Peek\n4. Display\n5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value to enqueue: ";
cin >> value;
cq.enqueue(value);
break;
case 2:
cq.dequeue();
break;
case 3:
cq.peek();
break;
case 4:
cq.display();
break;
case 5:
cout << "Exi ng...\n";
break;
default:
cout << "Invalid choice.\n";
}
} while (choice != 5);
return 0;
}
Output: -
EXPERIMENT - 6
Aim : - To implement stack using linked list.
Code
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};
class Stack {
Node* top;
public:
Stack() {
top = nullptr;
}
bool isEmpty() {
return top == nullptr;
}
void pop() {
if (isEmpty()) {
cout << "Stack Underflow! Cannot pop.\n";
return;
}
Node* temp = top;
cout << temp->data << " popped from stack.\n";
top = top->next;
delete temp;
}
void peek() {
if (isEmpty()) {
cout << "Stack is empty.\n";
return;
}
cout << "Top element is: " << top->data << endl;
}
void display() {
if (isEmpty()) {
cout << "Stack is empty.\n";
return;
}
~Stack() {
while (!isEmpty()) {
pop();
}
}
};
int main() {
Stack stack;
int choice, value;
do {
switch (choice) {
case 1:
cout << "Enter value to push: ";
cin >> value;
stack.push(value);
break;
case 2:
stack.pop();
break;
case 3:
stack.peek();
break;
case 4:
stack.display();
break;
case 5:
cout << "Exi ng...\n";
break;
default:
cout << "Invalid choice.\n";
}
} while (choice != 5);
return 0;
}
OUTPUT:-
EXPERIMENT - 7
Aim:- To implement queue using linked list.
Code:
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};
class Queue {
Node* front;
Node* rear;
public:
Queue() {
front = rear = nullptr;
}
bool isEmpty() {
return front == nullptr;
}
if (rear == nullptr) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
void dequeue() {
if (isEmpty()) {
cout << "Queue Underflow! Cannot dequeue.\n";
return;
}
Node* temp = front;
cout << temp->data << " dequeued from queue.\n";
front = front->next;
if (front == nullptr) {
rear = nullptr;
}
delete temp;
}
void peek() {
if (isEmpty()) {
cout << "Queue is empty.\n";
return;
}
cout << "Front element: " << front->data << endl;
}
void display() {
if (isEmpty()) {
cout << "Queue is empty.\n";
return;
}
~Queue() {
while (!isEmpty()) {
dequeue();
}
}
};
int main() {
Queue q;
int choice, value;
do {
cout << "\n--- Queue Menu ---\n";
cout << "1. Enqueue\n2. Dequeue\n3. Peek\n4. Display\n5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value to enqueue: ";
cin >> value;
q.enqueue(value);
break;
case 2:
q.dequeue();
break;
case 3:
q.peek();
break;
case 4:
q.display();
break;
case 5:
cout << "Exi ng...\n";
break;
default:
cout << "Invalid choice.\n";
}
} while (choice != 5);
return 0;
}
OUTPUT:
EXPERIMENT – 8
Aim :To implement BFS using linked list.
Code:
#include <iostream>
#include <list>
using namespace std;
class Graph {
int V;
list<int> *adj;
public:
Graph(int V);
void addEdge(int v, int w);
void BFS(int s);
};
Graph::Graph(int V) {
this->V = V;
adj = new list<int>[V];
}
void Graph::BFS(int s) {
bool *visited = new bool[V];
for (int i = 0; i < V; i++)
visited[i] = false;
list<int> queue;
visited[s] = true;
queue.push_back(s);
while (!queue.empty()) {
s = queue.front();
cout << s << " ";
queue.pop_front();
delete[] visited;
}
int main() {
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
return 0;
}
OUTPUT:
EXPERIMENT – 9
Aim :To implement DFS using linked list.
Code:
#include <iostream>
#include <list>
using namespace std;
class Graph {
int V;
list<int> *adj;
public:
Graph(int V);
void addEdge(int v, int w);
void DFS(int v);
private:
void DFSU l(int v, bool visited[]);
};
Graph::Graph(int V) {
this->V = V;
adj = new list<int>[V];
}
void Graph::DFS(int v) {
bool *visited = new bool[V];
for (int i = 0; i < V; i++)
visited[i] = false;
int main() {
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
return 0;
}
OUTPUT:
EXPERIMENT – 10
Aim : To implement linear search.
Code:
#include <iostream>
using namespace std;
int main() {
int n, key;
int arr[n];
cout << "Enter " << n << " elements:\n";
for (int i = 0; i < n; i++)
cin >> arr[i];
if (result != -1)
cout << "Element found at index " << result << ".\n";
else
cout << "Element not found in the array.\n";
return 0;
}
OUTPUT:
EXPERIMENT – 11
Aim : To implement binary search.
Code:
#include <iostream>
using namespace std;
else
high = mid - 1;
}
int main() {
int arr[] = {2, 4, 10, 15, 23, 38, 56, 72, 91};
int size = sizeof(arr) / sizeof(arr[0]);
int key;
if (result != -1)
cout << "Element found at index: " << result << endl;
else
cout << "Element not found in the array." << endl;
return 0;
}
OUTPUT:
EXPERIMENT – 12
Aim : To implement Bubble Sorting.
Code:
#include <iostream>
using namespace std;
int main() {
bubbleSort(arr, size);
printArray(arr, size);
return 0;
}
OUTPUT:
EXPERIMENT – 13
Aim : To implement Selection Sorting.
Code:
#include <iostream>
using namespace std;
int minIndex = i;
for (int j = i + 1; j < size; j++) {
if (arr[j] < arr[minIndex])
minIndex = j;
}
if (minIndex != i) {
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
}
int main() {
return 0;
}
OUTPUT:
EXPERIMENT – 14
Aim : To implement Insertion Sorting.
Code:
#include <iostream>
using namespace std;
arr[j + 1] = key;
}
}
int main() {
int arr[] = {12, 11, 13, 5, 6};
int size = sizeof(arr) / sizeof(arr[0]);
return 0;
}
OUTPUT:
EXPERIMENT – 15
Aim : To implement Merge Sorting.
Code:
#include <iostream>
using namespace std;
int main() {
return 0;
}
OUTPUT:
EXPERIMENT – 16
Aim : To implement Heap Sorting.
Code:
#include <iostream>
using namespace std;
void heapify(int arr[], int size, int i) {
int largest = i;
int le = 2 * i + 1;
int right = 2 * i + 2;
heapSort(arr, size);
printArray(arr, size);
return 0;
}
OUTPUT:
EXPERIMENT – 17
Aim : To implement Matrix Multiplication by Strassen’s algorithm.
Code:
#include <iostream>
#include <vector>
if (n == 1) {
return {{A[0][0] * B[0][0]}};
}
int newSize = n / 2;
Matrix A11(newSize, vector<int>(newSize));
Matrix A12(newSize, vector<int>(newSize));
Matrix A21(newSize, vector<int>(newSize));
Matrix A22(newSize, vector<int>(newSize));
B11[i][j] = B[i][j];
B12[i][j] = B[i][j + newSize];
B21[i][j] = B[i + newSize][j];
B22[i][j] = B[i + newSize][j + newSize];
}
// 7 recursive mul plica ons
Matrix M1 = strassen(add(A11, A22), add(B11, B22));
Matrix M2 = strassen(add(A21, A22), B11);
Matrix M3 = strassen(A11, subtract(B12, B22));
Matrix M4 = strassen(A22, subtract(B21, B11));
Matrix M5 = strassen(add(A11, A12), B22);
Matrix M6 = strassen(subtract(A21, A11), add(B11, B12));
Matrix M7 = strassen(subtract(A12, A22), add(B21, B22));
return C;
}
// Main Func on
int main() {
int n;
cout << "Enter size of matrices (power of 2): ";
cin >> n;
return 0;
}
OUTPUT:
EXPERIMENT – 18
Aim : Find Minimum Spanning Tree using Kruskal’s Algorithm.
Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Structure to represent an edge
struct Edge {
int u, v, weight;
int find(int x) {
if (x != parent[x])
parent[x] = find(parent[x]); // Path compression
return parent[x];
}
// Kruskal's Algorithm
int kruskalMST(int V, vector<Edge>& edges, vector<Edge>& mst) {
sort(edges.begin(), edges.end());
DSU dsu(V);
int mstWeight = 0;
int main() {
cout << "Enter number of ver ces and edges: ";
int V, E;
cin >> V >> E;
vector<Edge> edges(E);
cout << "Enter edges in the format: u v weight\n";
for (int i = 0; i < E; ++i) {
cin >> edges[i].u >> edges[i].v >> edges[i].weight;
}
vector<Edge> mst;
int totalWeight = kruskalMST(V, edges, mst);
return 0;
}
OUTPUT: