Dsa Lab Manual
Dsa Lab Manual
LAB MANUAL
Data Structures and Algorithm
IT-201
B.Tech. 5th Semester
List of Experiments
Page
S.No. Practical Signature
No.
a)WAP to implement Linear search and Binary
search on 1D array of Integers.
1 4
b)WAP to implement Insertion and Selection sort
on 1D array of strings.
(a) WAP to implement Stack ADT using Arrays
which has basic operations as Create(),
IsEmpty(), Push(), Pop(), IsFull() with appropriate
2 prototype to a functions. 7
Experiment 1
Objective :-
Code :-
(a)
#include <iostream>
#include <algorithm>
int main() {
int n, key;
int arr[n];
return 0;
}
(b)
#include <iostream>
#include <string>
#include <vector>
int main() {
int n;
std::cout << "Enter the number of strings: ";
std::cin >> n;
std::vector<std::string> arr(n);
std::cout << "Enter the strings: ";
for (int i = 0; i < n; i++) {
std::cin >> arr[i];
}
selectionSort(arr);
std::cout << "Strings after Selection Sort: ";
for (const auto& str : arr) {
std::cout << str << " ";
}
std::cout << std::endl;
return 0;
6
Output :-
(a)
(b)
7
Experiment 2
Objective :-
(a) WAP to implement Stack ADT using Arrays which has basic operations as Create(),
IsEmpty(), Push(), Pop(), IsFull() with appropriate prototype to a functions.
Code :-
(a)
#include <iostream>
#define MAX 100
class Stack {
int arr[MAX];
int top;
public:
Stack() { top = -1; }
bool isEmpty() {
return top == -1;
}
bool isFull() {
return top == MAX - 1;
}
int pop() {
if (isEmpty()) {
std::cout << "Stack is empty. Cannot pop" << std::endl;
return -1;
} else {
return arr[top--];
}
}
};
int main() {
Stack stack;
stack.push(10);
stack.push(20);
stack.push(30);
std::cout << "Popped: " << stack.pop() << std::endl;
std::cout << "Popped: " << stack.pop() << std::endl;
}
(b)
#include <iostream>
#include <cctype>
#include <cmath>
#define MAX 100
8
class Stack {
int arr[MAX];
int top;
public:
Stack() { top = -1; }
bool isEmpty() {
return top == -1;
}
bool isFull() {
return top == MAX - 1;
}
int pop() {
if (!isEmpty()) {
return arr[top--];
}
return -1;
}
};
int main() {
std::string exp = "53+82-*";
std::cout << "Postfix Evaluation: " << evaluatePostfix(exp) << std::endl;
return 0;
}
Output :-
(a)
9
(b)
10
Experiment 3
Objective :-
a)WAP to implement a 3-stacks of size ‘m’ in an array of size ‘n’ with all the basic
number (1,2,3), m ≅ n/3. Stacks are not overlapping each other. Leftmost stack facing
operations such as IsEmpty(i), Push(i), Pop(i), IsFull(i) where ‘i’ denotes the stack
the left direction and other two stacks are facing in the right direction.
b)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 :-
(a)
#include <iostream>
#define MAX 100
class ThreeStacks {
int arr[MAX];
int top1, top2, top3;
int n;
public:
ThreeStacks(int n) {
this->n = n;
top1 = -1;
top2 = n / 3;
top3 = n - 1;
}
int main() {
int n = 15;
ThreeStacks ts(n);
ts.push(1, 10);
ts.push(2, 20);
ts.push(3, 30);
std::cout << "Pop from Stack 1: " << ts.pop(1) << std::endl;
std::cout << "Pop from Stack 2: " << ts.pop(2) << std::endl;
std::cout << "Pop from Stack 3: " << ts.pop(3) << std::endl;
return 0;
}
(b)
#include <iostream>
#include <stack>
#include <string>
bool isOperator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/' || c == '$' || c == '#';
}
if (isalnum(c)) {
postfix += c;
}
else if (c == '(') {
s.push(c);
}
else if (c == ')') {
while (!s.empty() && s.top() != '(') {
postfix += s.top();
s.pop();
}
s.pop();
}
else if (isOperator(c)) {
while (!s.empty() && precedence(s.top()) >= precedence(c)) {
postfix += s.top();
s.pop();
}
s.push(c);
}
}
12
while (!s.empty()) {
postfix += s.top();
s.pop();
}
return postfix;
}
int main() {
string infix;
cout << "Enter infix expression: ";
cin >> infix;
string postfix = infixToPostfix(infix);
cout << "Postfix expression: " << postfix << endl;
return 0;
}
Output :-
(a)
(b)
13
Experiment 4
Objective :-
(a) WAP to implement Queue ADT using Arrays with the basic functions of Create(),
IsEmpty(), Insert(), Delete() and IsFull() with suitable prototype to a functions.
(b) WAP to implement Queue using Stacks.
Code :-
(a)
#include <iostream>
using namespace std;
class Queue {
int arr[MAX];
int front, rear;
public:
Queue() {
front = rear = -1;
}
bool isEmpty() {
return front == -1;
}
bool isFull() {
return rear == MAX - 1;
}
int deleteQueue() {
if (isEmpty()) {
cout << "Queue is empty. Cannot delete" << endl;
return -1;
}
int value = arr[front];
if (front >= rear) front = rear = -1;
else front++;
return value;
}
void display() {
if (isEmpty()) {
cout << "Queue is empty" << endl;
return;
}
for (int i = front; i <= rear; i++)
cout << arr[i] << " ";
cout << endl;
}
};
int main() {
Queue q;
q.insert(10);
14
q.insert(20);
q.insert(30);
q.display();
cout << q.deleteQueue() << " deleted from queue" << endl;
q.display();
return 0;
}
(b)
#include <iostream>
#include <stack>
using namespace std;
class QueueUsingStacks {
stack<int> s1, s2;
public:
void enqueue(int value) {
s1.push(value);
}
int dequeue() {
if (s1.empty() && s2.empty()) {
cout << "Queue is empty. Cannot dequeue" << endl;
return -1;
}
if (s2.empty()) {
while (!s1.empty()) {
s2.push(s1.top());
s1.pop();
}
}
int value = s2.top();
s2.pop();
return value;
}
bool isEmpty() {
return s1.empty() && s2.empty();
}
void display() {
if (isEmpty()) {
cout << "Queue is empty" << endl;
return;
}
while (!s2.empty()) {
cout << s2.top() << " ";
s2.pop();
}
while (!s1.empty()) {
cout << s1.top() << " ";
}
cout << endl;
}
};
int main() {
QueueUsingStacks q;
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
cout << q.dequeue() << " dequeued from queue" << endl;
q.display();
return 0;
}
15
Output :-
(a)
(b)
16
Experiment 5
Objective :-
WAP to construct simple linear linked list using dynamic memory allocation for the
given elements with the following functions,
(a) Inserting a new node,
(b) Accessing a node (finding the position wrt header),
(c) Removing a node with particular key value,
(d) Complete deletion of a linked list, and
(e) Displaying the current list.
(f) Copy the linked list and return the pointer of the new list.
Implement the above program for the elements as Strings.
Code :-
#include <iostream>
#include <string>
struct Node {
std::string data;
Node* next;
};
class LinkedList {
private:
Node* head;
public:
LinkedList() {
head = nullptr;
}
void deleteList() {
Node* current = head;
while (current != nullptr) {
Node* temp = current;
17
current = current->next;
delete temp;
}
head = nullptr;
}
void display() {
Node* current = head;
while (current != nullptr) {
std::cout << current->data << " ";
current = current->next;
}
std::cout << std::endl;
}
LinkedList* copy() {
LinkedList* newList = new LinkedList();
Node* current = head;
while (current != nullptr) {
newList->insert(current->data);
current = current->next;
}
return newList;
}
~LinkedList() {
deleteList();
}
};
int main() {
LinkedList list;
list.insert("Node1");
list.insert("Node2");
list.insert("Node3");
list.display();
list.remove("Node2");
list.display();
list.deleteList();
copiedList->display();
delete copiedList;
return 0;
}
Output :-
18
Experiment 6
Objective :-
(a) WAP to find second last node of the given linked list.\
(b) WAP to concatenate two singly linked list in sorted order either ascending or
descending.
Code :-
(a)
#include <iostream>
struct Node {
int data;
Node* next;
};
return secondLast;
}
int main() {
Node* head = new Node{10, new Node{20, new Node{30, new Node{40, nullptr}}}};
return 0;
}
(b)
#include <iostream>
struct Node {
int data;
Node* next;
};
int main() {
Node* list1 = nullptr;
Node* list2 = nullptr;
append(list1, 10);
append(list1, 30);
append(list1, 50);
append(list2, 20);
append(list2, 40);
append(list2, 60);
return 0;
}
(c)
#include <iostream>
struct Node {
int data;
Node* next;
};
while (fast) {
fast = fast->next;
if (fast) {
slow = slow->next;
fast = fast->next;
}
}
*front = head;
*back = slow->next;
slow->next = nullptr;
}
Node* a;
Node* b;
split(*head, &a, &b);
mergeSort(&a);
mergeSort(&b);
int main() {
Node* head = nullptr;
append(head, 30);
append(head, 10);
append(head, 40);
append(head, 20);
mergeSort(&head);
return 0;
}
Output :-
(a)
21
(b)
(c)
22
Experiment 7
Objective :-
WAP to create a Circular Singly Linked List for the given elements with the following
functions,
(a) Inserting a node, before the node with key givenkey,
(b) Inserting a node, after the node with key givenkey,
(c) Accessing a node (finding the position wrt header),
(d) Removing a node with particular key value,
(e) Complete deletion of a list,
(f) Displaying the current list, and
(g) Sorting the list.
Implement the above program for the elements as Strings.
Code :-
#include <iostream>
#include <string>
struct Node {
std::string data;
Node* next;
};
class CircularLinkedList {
public:
Node* head;
CircularLinkedList() {
head = nullptr;
}
if (head->data == key) {
Node* last = head;
while (last->next != head) last = last->next;
newNode->next = head;
last->next = newNode;
head = newNode;
return;
}
do {
prev = current;
current = current->next;
if (current->data == key) {
newNode->next = current;
prev->next = newNode;
return;
}
} while (current != head);
}
newNode->data = newData;
do {
if (current->data == key) return pos;
current = current->next;
pos++;
} while (current != head);
return -1;
}
if (head->data == key) {
Node* last = head;
while (last->next != head) last = last->next;
if (head == last) {
delete head;
head = nullptr;
} else {
Node* temp = head;
last->next = head->next;
head = head->next;
delete temp;
}
return;
}
do {
prev = current;
current = current->next;
if (current->data == key) {
prev->next = current->next;
delete current;
return;
}
} while (current != head);
}
void deleteList() {
if (head == nullptr) return;
do {
nextNode = current->next;
delete current;
current = nextNode;
} while (current != head);
head = nullptr;
24
void display() {
if (head == nullptr) return;
void sortList() {
if (head == nullptr) return;
do {
index = current->next;
while (index != head) {
if (current->data > index->data) {
temp = current->data;
current->data = index->data;
index->data = temp;
}
index = index->next;
}
current = current->next;
} while (current->next != head);
}
};
int main() {
CircularLinkedList list;
list.insertAfter("", "apple");
list.insertAfter("apple", "banana");
list.insertAfter("banana", "cherry");
list.display();
list.insertBefore("banana", "apricot");
list.display();
list.insertAfter("banana", "date");
list.display();
list.removeNode("banana");
list.display();
list.sortList();
list.display();
list.deleteList();
list.display();
return 0;
}
Output :-
25
Experiment 8
Objective :-
(a) WAP to build a binary tree for the given elements (integers) and also give traversal
functions : inorder, preorder, postorder.
(b) WAP to traverse a given binary tree in inorder, preorder, postorder, converse inorder,
converse preorder, converse postorder fashion. (converse - traverse in a reverse
direction)
(c) WAP to transform given tree into a binary tree.
(d) WAP to represent an arithematic expression in binary tree format.
Code :-
(a)
#include <iostream>
struct Node {
int data;
Node* left;
Node* right;
};
int main() {
Node* root = nullptr;
root = insertNode(root, 50);
insertNode(root, 30);
insertNode(root, 20);
insertNode(root, 40);
insertNode(root, 70);
insertNode(root, 60);
insertNode(root, 80);
return 0;
}
(b)
#include <iostream>
struct Node {
int data;
Node* left;
Node* right;
};
int main() {
Node* root = nullptr;
root = insertNode(root, 50);
insertNode(root, 30);
insertNode(root, 20);
insertNode(root, 40);
insertNode(root, 70);
insertNode(root, 60);
insertNode(root, 80);
return 0;
}
(c)
#include <iostream>
#include <vector>
struct TreeNode {
int data;
std::vector<TreeNode*> children;
TreeNode(int val) : data(val) {}
28
};
struct BinaryTreeNode {
int data;
BinaryTreeNode* left;
BinaryTreeNode* right;
BinaryTreeNode(int val) : data(val), left(nullptr), right(nullptr) {}
};
int main() {
TreeNode* root = new TreeNode(1);
root->children.push_back(new TreeNode(2));
root->children.push_back(new TreeNode(3));
root->children.push_back(new TreeNode(4));
root->children[0]->children.push_back(new TreeNode(5));
root->children[0]->children.push_back(new TreeNode(6));
root->children[1]->children.push_back(new TreeNode(7));
return 0;
}
(d)
#include <iostream>
#include <stack>
#include <cctype>
struct Node {
char data;
Node* left;
Node* right;
};
bool isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/');
}
int main() {
std::string postfix = "ab+cde+**";
Node* root = constructExpressionTree(postfix);
return 0;
}
Output :-
(a)
(b)
30
(c)
(d)
31
Experiment 9
Objective :-
(a) WAP to implement priority queues using heaps (min heap or max heap) with add and
delete functions.
(b) WAP to perform heap sort on the given list of elements.
Code :-
(a)
#include <iostream>
#include <vector>
class MinHeap {
private:
std::vector<int> heap;
if (smallest != index) {
std::swap(heap[smallest], heap[index]);
heapifyDown(smallest);
}
}
public:
void add(int value) {
heap.push_back(value);
heapifyUp(heap.size() - 1);
}
int deleteMin() {
if (heap.empty()) return -1;
int minElement = heap[0];
heap[0] = heap.back();
heap.pop_back();
heapifyDown(0);
return minElement;
}
void printHeap() {
for (int i : heap) std::cout << i << " ";
std::cout << std::endl;
}
};
int main() {
MinHeap pq;
pq.add(10);
pq.add(20);
32
pq.add(5);
pq.add(30);
pq.printHeap();
std::cout << "Min element deleted: " << pq.deleteMin() << std::endl;
pq.printHeap();
}
(b)
#include <iostream>
#include <vector>
if (largest != i) {
std::swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}
int main() {
std::vector<int> arr = {12, 11, 13, 5, 6, 7};
int n = arr.size();
heapSort(arr, n);
return 0;
}
33
Output :-
(a)
(b)
34
Experiment 10
Objective :-
(a) WAP to perform BFS for any given graph.
(b)WAP to perform DFS for any given graph.
Code :-
(a)
#include <iostream>
#include <vector>
#include <queue>
while (!q.empty()) {
int node = q.front();
q.pop();
std::cout << node << " ";
int main() {
int n, edges;
std::cin >> n >> edges;
std::vector<std::vector<int>> adj(n);
int start;
std::cout << "Enter start node for BFS: ";
std::cin >> start;
BFS(start, adj, n);
return 0;
}
(b)
#include <iostream>
#include <vector>
}
}
int main() {
int n, edges;
std::cin >> n >> edges;
std::vector<std::vector<int>> adj(n);
int start;
std::cout << "Enter start node for DFS: ";
std::cin >> start;
DFS(start, adj, n);
return 0;
}
Output :-
(a)
36
(b)
37
Experiment 11
Objective :-
(a) Wap to implement merge short on 1D Array of integers.
(b) Wap to implement bubble short on 1D Array of integers.
Code :-
(a)
#include <iostream>
using namespace std;
int main() {
int arr[] = {38, 27, 43, 3, 9, 82, 10};
int arrSize = sizeof(arr) / sizeof(arr[0]);
cout << "Original array: ";
for (int i = 0; i < arrSize; i++)
cout << arr[i] << " ";
cout << endl;
mergeSort(arr, 0, arrSize - 1);
cout << "Sorted array: ";
for (int i = 0; i < arrSize; i++)
cout << arr[i] << " ";
cout << endl;
return 0;
}
38
(b)
#include <iostream>
using namespace std;
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Original array: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
bubbleSort(arr, n);
cout << "Sorted array: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
return 0;
}
Output :-
(a)
39
(b)