DSA Manual
DSA Manual
1
Preface
This lab manual has been prepared to facilitate the students of
software engineering in studying and implementing linear and
nonlinear data structures and algorithms. The students will also analyze
the best and most efficient data structures and algorithms to solve
problems. The students can learn and implement several searching and
sorting algorithms. After the completion of this course students will be
able to practically implement the concepts of data structure and
algorithms on real life applications.
Tools/ Technologies
• C++ Language
• Dev C++
• VS Code
2
TABLE OF CONTENTS
Preface...............................................................................................2
Tools/Technologies.............................................................................2
LAB 1: Arrays and its Operations…...……..............................................4
LAB 2: Searching and Sorting ............................................................. 7
LAB 3: Recursion .............................................................................. 12
LAB 4: Array Based Stack Implementation ....................................... 14
LAB 5: Array Based Linear Queue Implementation ........................... 16
LAB 6: Array based Circular Queue Implementation ........................ 18
Lab 7: Insertion in Linked List........................................................... 21
Lab 8: Deletion from Linked List ....................................................... 23
Lab 9: Linked List based Stack and Queue ........................................ 26
Lab 10: Doubly Linked List Insertion and Deletion ............................ 29
LAB 11: Reverse using Linked List .................................................... 31
LAB 12: Sorting using Divide and Conquer Approach ....................... 33
3
LAB 1
1. Declare an array of 5 integers and assign 4 values. Now take an
index and number from user and insert that number in user entered
index (Implement C++ Program).
#include <iostream>
using namespace std;
int main()
{
int arr[] = {5, 3, 7, 9};
int length = sizeof(arr) / sizeof(int);
int index;
cout << "Enter index number : ";
cin >> index;
4
int insertVal;
cout << "Enter value to insert : ";
cin >> insertVal;
for (int i = length - 1; i > index; i--)
{
arr[i] = arr[i - 1];
}
arr[index] = insertVal;
5
2. Declare an array of 5 integers. Take an index from user and delete
the number from array (Implement C++ Program).
#include <iostream>
using namespace std;
int main()
{
int arr[] = {5, 3, 6, 7, 9};
int length = sizeof(arr) / sizeof(int);
int index;
cout << "Enter index number : ";
cin >> index;
6
cout << "\nIndex not found! Please try again :\n";
cout << "Enter index number : ";
cin >> index;
}
7
console(output):
LAB 2
1. Matrix Addition(2D Array):
#include <iostream>
using namespace std;
int main()
{
int rows, columns;
8
cout << "Enter number of columns for matrix 1 & 2 : ";
cin >> columns;
int matrix1[rows][columns];
int matrix2[rows][columns];
int sumMatrix[rows][columns];
9
{
for (int j = 0; j < columns; j++)
{
cout << "Enter values at index " << i << j << " : ";
cin >> matrix1[i][j];
}
}
10
sumMatrix[i][j] = matrix1[i][j] + matrix2[j][i];
}
}
11
2. Matrix Multiplication(2D Array)
#include <iostream>
using namespace std;
int main()
{
int row1, column1, row2, column2;
12
cout << "Enter number of rows for matrix 1 : ";
cin >> row1;
13
cin >> column1;
int transposeMatrix[column2][row2];
int mulMatrix[row1][column2] = {0};
14
}
cout << "\nMatrix number 2 : " << endl;
for (int i = 0; i < row2; i++)
{
for (int j = 0; j < column2; j++)
{
cout << "Enter values at index " << i << j << " : ";
cin >> matrix2[i][j];
}
}
for (int i = 0; i < row2; i++)
{
for (int j = 0; j < column2; j++)
{
transposeMatrix[j][i] = matrix2[i][j];
}
}
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < column2; j++)
{
for (int k = 0; k < column1; k++)
15
{
mulMatrix[i][j] += matrix1[i][k] * transposeMatrix[k][j];
}
}
}
cout << "\nMultiplication of Matrix : " << endl;
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < column2; j++)
{
cout << mulMatrix[i][j] << " ";
}
cout << endl;
}
return 0;
}
16
console(output):
17
LAB 3
1.Take Stack Array from User and Apply Bubble Sort Using Stack Rule.
#include <iostream>
using namespace std;
int main()
18
{
int length;
cout << "Enter length : ";
cin >> length;
int stackArr[length];
19
bubbleSort(stackArr, top);
return 0;
}
console(output):
20
2. Take Stack Array from User and Apply Selection Sort Using Stack
Rule.
#include <iostream>
using namespace std;
21
}
}
int main()
{
int length;
cout << "Enter length : ";
cin >> length;
int stackArr[length];
int top = -1;
22
cout << stackArr[i] << " ";
}
selectionSort(stackArr, top);
23
3. Take Array from User and Apply Binary Search
#include <iostream>
using namespace std;
if (stackArr[mid] == searchVal)
{
return mid;
}
else if (stackArr[mid] > searchVal)
{
end = mid - 1;
}
else
{
24
start = mid + 1;
}
}
return -1;
}
int main()
{
int length;
cout << "Enter length : ";
cin >> length;
int stackArr[length];
25
cin >> stackArr[i];
top++;
}
int searchVal;
cout << "\nEnter the value to search : ";
cin >> searchVal;
return 0;
}
26
console(output):
27
LAB 4
Objective: Implement a queue using arrays in C++ and provide a menu-
driven program that allows the user to perform various queue
operations such as inserting, deleting, searching, sorting, and displaying
the queue. The program will also offer options for sorting and
searching, allowing the user to choose the desired method.
Menu Options:
1. Insert: Enqueue an element into the queue.
2. Delete: Dequeue an element from the queue.
3. Search:
o Present a sub-menu asking the user to choose between:
Linear Search: Search an element in the queue using
linear search.
Binary Search: Search an element using binary search
(ensure the queue is sorted before performing binary
search).
4. Sort:
o Present a sub-menu asking the user to choose between:
Ascending Order: Sort the queue in ascending order
Descending Order: Sort the queue in descending order.
You can choose sorting algorithm (bubble or selection
sort).
5. Display: Show all elements in the queue.
28
Code:
#include <iostream>
using namespace std;
void push(int queueArr[], int insertVal, int size, int &front, int &back)
{
if (back == size - 1)
{
cout << "Queue is full" << endl;
}
else
{
if (front == -1)
{
front = 0;
}
back++;
queueArr[back] = insertVal;
cout << "value inserted" << endl;
}
}
29
void pop(int queueArr[], int &front, int &back)
{
if (front == -1 || front > back)
{
cout << "Queue is empty" << endl;
}
else
{
cout << "Deleted value : " << queueArr[front] << endl;
front++;
}
}
do
{
cout << "\n1. Ascending Sort\n";
cout << "2. Descending Sort\n";
30
cout << "3. Exit Sort\n";
if (sortChoice == 3)
{
cout << "Exiting..." << endl;
return;
}
31
if (sortChoice == 1)
{
if (queueArr[j] > queueArr[j + 1])
{
swap(queueArr[j], queueArr[j + 1]);
}
}
else if (sortChoice == 2)
{
if (queueArr[j] < queueArr[j + 1])
{
swap(queueArr[j], queueArr[j + 1]);
}
}
}
}
}
32
if (queueArr[i] == searchVal)
{
return i;
}
}
return -1;
}
33
{
start = mid + 1;
}
else if (searchVal < queueArr[mid])
{
end = mid - 1;
}
else
{
return mid;
}
}
return -1;
}
do
34
{
cout << "\n1. Linear Search\n";
cout << "2. Binary Search\n";
cout << "3. Exit Search\n";
switch (searchChoice)
{
case 1:
linearResult = linearSearch(queueArr, searchVal, front, back);
if (linearResult == -1)
{
cout << "Search value not found in queue" << endl;
}
else
{
cout << "Search value found in queue at index : " <<
linearResult << endl;
}
35
break;
case 2:
binaryResult = binarySearch(queueArr, searchVal, front, back);
if (binaryResult == -1)
{
cout << "Search value not found in queue" << endl;
}
else
{
cout << "Search value found in queue at index : " <<
binaryResult << endl;
}
break;
case 3:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid Choice. Please choose between 1 & 2" << endl;
36
break;
}
} while (searchChoice != 3);
}
37
int main()
{
const int size = 5;
int queueArr[size];
int front = -1, back = -1;
int choice, insertVal, searchVal;
do
{
cout << "\n1. Insert\n";
cout << "2. Delete\n";
cout << "3. Sort\n";
cout << "4. Search\n";
cout << "5. Display\n";
cout << "6. Exit\n";
switch (choice)
{
case 1:
38
cout << "Enter value to insert : ";
cin >> insertVal;
39
cout << endl;
}
break;
case 4:
if (front == -1 || front > back)
{
cout << "Queue is empty" << endl;
}
else
{
cout << "Enter value to search : ";
cin >> searchVal;
search(queueArr, searchVal, front, back);
}
break;
case 5:
display(queueArr, front, back);
break;
case 6:
cout << "Exiting..." << endl;
break;
40
default:
cout << "Invalid Choice. Please choose between 1 to 6" << endl;
}
} while (choice != 6);
}
console(output):
1.Push :
41
2.Pop:
3.Sort:
42
4.Search:
43
5.Display:
44
LAB 5
Task 1: Sum of Digits
Question: Write a recursive function to calculate the sum of the digits
of a given integer. Implement the function and test it with the input
12345. What should the output be?
#include <iostream>
using namespace std;
int main()
{
int num = 12345;
45
int digitSum = recSumOfDigits(num);
return 0;
}
console(output):
cout << base << " raised to the power of " << exponent << " is : " <<
recPowerFunc(base, exponent);
return 0;
}
console(output):
47
LAB 6
Q1: Insert elements at the tail/end in the linked list and write a
function to display the elements located at even positions (2, 4, 6,
etc.).
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *next;
Node(int value)
{
data = value;
next = NULL;
}
};
if (head == NULL)
49
{
cout << "Linked List is empty" << endl;
}
else
{
cout << "Even Nodes : ";
while (temp != NULL)
{
if (node % 2 == 0)
{
cout << temp->data << "->";
}
temp = temp->next;
node++;
}
cout << "NULL" << endl;
}
}
50
if (head == NULL)
{
cout << "Linked List is empty" << endl;
}
else
{
cout << "Linked List All Nodes : ";
while (temp != NULL)
{
cout << temp->data << "->";
temp = temp->next;
}
cout << "NULL" << endl;
}
}
int main()
{
Node *head = NULL;
int choice, value, nodeNum = 1;
51
do
{
cout << "\n1. Insert At Tail\n";
cout << "2. Display Even Nodes\n";
cout << "3. Display All Nodes\n";
cout << "4. Exit\n";
switch (choice)
{
case 1:
cout << "Enter value at Node(" << nodeNum << ") : ";
cin >> value;
nodeNum++;
insertAtTail(head, value);
break;
case 2:
displayEven(head);
52
break;
case 3:
displayAll(head);
break;
case 4:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice. Please choose between 1 to 4" << endl;
}
} while (choice != 4);
return 0;
}
console(output):
53
Q2: Use recursion to traverse the linked list and return the maximum
value present in the list.
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *next;
54
Node(int value)
{
data = value;
next = NULL;
}
};
if (head == NULL)
{
head = newNode;
}
else
{
Node *temp = head;
55
temp = temp->next;
}
temp->next = newNode;
}
}
56
{
int maxValue = maxNode(head);
if (maxValue == -1)
{
cout << "linked list is empty" << endl;
}
else
{
cout << "Maximum Node Value : " << maxValue << endl;
}
}
if (head == NULL)
{
cout << "linked list is empty" << endl;
}
else
57
{
cout << "Linked List Nodes : ";
while (temp != NULL)
{
cout << temp->data << "->";
temp = temp->next;
}
cout << "NULL" << endl;
}
}
int main()
{
Node *head = NULL;
int choice, value, nodeNum = 1;
do
{
cout << "\n1. Insert at Tail\n";
cout << "2. Display All Nodes\n";
cout << "3. Display Max Node\n";
cout << "4. Exit\n";
58
cout << "\nSelect your choice : ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter value at Node(" << nodeNum << ") : ";
cin >> value;
nodeNum++;
insertAtTail(head, value);
break;
case 2:
displayAll(head);
break;
case 3:
displayMax(head);
break;
case 4:
cout << "Exiting..." << endl;
59
break;
default:
cout << "Invalid choice. Please choose between 1 to 4" << endl;
}
} while (choice != 4);
return 0;
}
console(output):
60
Q3: Write a recursive function to count and return the number of
nodes in the list.
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *next;
Node(int value)
{
data = value;
next = NULL;
}
};
61
if (head == NULL)
{
head = newNode;
}
else
{
Node *temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = newNode;
}
}
62
{
return 1 + recNumOfNodes(head->next);
}
}
int main()
{
Node *head = NULL;
int choice, value, nodeNum = 1;
do
{
cout << "\n1. Insert At Tail\n";
cout << "2. Display Number of Nodes\n";
cout << "3. Exit\n";
63
cout << "\nSelect your choice : ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter value at Node(" << nodeNum << ") : ";
cin >> value;
nodeNum++;
insertAtTail(head, value);
break;
case 2:
displayNumOfNodes(head);
break;
case 3:
cout << "Exiting..." << endl;
break;
default:
64
cout << "Invalid choice. Please choose between 1 to 3" << endl;
}
} while (choice != 3);
return 0;
}
console(output):
65
LAB 7
Develop a C++ program that implements a singly linked list with the
following functionalities:
1. Insertion of a node at the beginning or the end of the list, based on
the user's choice.
2. Deletion of a node from the beginning or the end of the list, based
on the user's choice.
3. Display the linked list
#include <iostream>
using namespace std;
class Node
{
public:
string name;
int rollNo;
Node *next;
66
this->next = NULL;
}
};
if (head == NULL)
{
head = newNode;
cout << "Node inserted at start" << endl;
}
else
{
newNode->next = head;
head = newNode;
cout << "Node inserted at start" << endl;
}
}
67
{
Node *newNode = new Node(name, rollNo);
if (head == NULL)
{
head = newNode;
cout << "Node inserted at end" << endl;
}
else
{
Node *currNode = head;
while (currNode->next != NULL)
{
currNode = currNode->next;
}
currNode->next = newNode;
cout << "Node inserted at end" << endl;
}
}
68
{
if (head == NULL)
{
cout << "Linked List is empty" << endl;
}
else
{
head = head->next;
cout << "Node deleted from start" << endl;
}
}
69
else
{
Node *secondLast = head;
Node *LastNode = head->next;
while (LastNode->next != NULL)
{
LastNode = LastNode->next;
secondLast = secondLast->next;
}
secondLast->next = NULL;
cout << "Node deleted from end" << endl;
}
}
70
else
{
Node *temp = head;
cout << "\nLinked List Data : ";
while (temp != NULL)
{
cout << temp->name << " -> ";
cout << temp->rollNo << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}
}
int main()
{
Node *head = NULL;
do
71
{
cout << "\n1. Insert At Start\n";
cout << "2. Insert At End\n";
cout << "3. Delete From Start\n";
cout << "4. Delete From End\n";
cout << "5. Display All Nodes\n";
cout << "6. Exit\n";
switch (choice)
{
case 1:
cout << "Enter Name : ";
cin >> name;
72
case 2:
cout << "Enter Name : ";
cin >> name;
case 3:
deleteFromStart(head);
break;
case 4:
deleteFromEnd(head);
break;
case 5:
display(head);
break;
73
case 6:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice. Please choose between 1 to 6" << endl;
}
} while (choice != 6);
}
console(output):
74
LAB 8
Write a C++ program to implement a doubly linked list. The program
should allow the user to insert nodes either at the beginning or the
end of the list. Additionally, create two display functions to show the
list elements in forward and reverse order, and prompt the user to
choose their preferred display direction.
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *prev;
Node *next;
75
Node(int value)
{
this->data = value;
this->prev = NULL;
this->next = NULL;
}
};
if (head == NULL)
{
head = tail = newNode;
}
else
{
newNode->next = head;
head->prev = newNode;
head = newNode;
76
}
cout << "Node inserted at start" << endl;
}
if (tail == NULL)
{
head = tail = newNode;
}
else
{
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
cout << "Node inserted at end" << endl;
}
77
{
int choice;
if (choice == 1)
{
insertAtStart(head, tail, value);
}
else if (choice == 2)
{
insertAtEnd(head, tail, value);
}
else
{
cout << "Invalid choice" << endl;
}
}
78
void forwardDisplay(Node *head)
{
if (head == NULL)
{
cout << "Linked List is empty" << endl;
}
else
{
Node *currNode = head;
while (currNode != NULL)
{
cout << currNode->data << " -> ";
currNode = currNode->next;
}
cout << "NULL" << endl;
}
}
79
{
cout << "Linked List is empty" << endl;
}
else
{
Node *currNode = tail;
while (currNode != NULL)
{
cout << currNode->data << " -> ";
currNode = currNode->prev;
}
cout << "NULL" << endl;
}
}
80
cout << "\nSelect your choice : ";
cin >> choice;
if (choice == 1)
{
forwardDisplay(head);
}
else if (choice == 2)
{
reverseDisplay(tail);
}
else
{
cout << "Invalid choice" << endl;
}
}
int main()
{
Node *head = NULL, *tail = NULL;
int choice, value;
do
81
{
cout << "\n1) Insert" << endl;
cout << "2) Display" << endl;
cout << "3) Exit" << endl;
switch (choice)
{
case 1:
cout << "Enter value to insert : ";
cin >> value;
82
default:
cout << "Invalid choice. Please choose from 1 to 3" << endl;
}
} while (choice != 3);
return 0;
}
console(output):
83
LAB 9
Q#1: Implement a stack using a linked list in C++.
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *next;
Node(int value)
{
this->data = value;
this->next = NULL;
}
};
84
Node *newNode = new Node(value);
if (top == NULL)
{
top = newNode;
}
else
{
newNode->next = top;
top = newNode;
}
cout << value << " inserted at top of stack" << endl;
}
85
cout << top->data << " is deleted from top of stack" << endl;
top = top->next;
}
}
86
cout << top->data << " -> ";
top = top->next;
}
cout << "NULL" << endl;
}
87
if (choice == 1)
{
cout << "\nStack Linked List : ";
recDisplay(top);
}
else if (choice == 2)
{
iterDisplay(top);
}
else
{
cout << "Invalid choice" << endl;
}
}
}
int main()
{
Node *top = NULL;
int choice, value;
do
{
88
cout << "\n1) Insert\n";
cout << "2) Delete\n";
cout << "3) Display\n";
cout << "4) Exit\n";
switch (choice)
{
case 1:
cout << "Enter value to insert : ";
cin >> value;
insertion(top, value);
break;
case 2:
deletion(top);
break;
case 3:
89
display(top);
break;
case 4:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice. Please choose from 1 to 4" << endl;
}
} while (choice != 4);
return 0;
}
90
Q#2. Implement a queue using a linked list in C++.
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *next;
Node(int value)
{
this->data = value;
this->next = NULL;
}
};
91
if (front == NULL)
{
front = back = newNode;
}
else
{
back->next = newNode;
back = newNode;
}
cout << value << " inserted to Queue" << endl;
}
92
if (front == NULL)
{
back == NULL;
}
}
}
93
cout << "\nQueue Linked List : ";
while (front != NULL)
{
cout << front->data << " -> ";
front = front->next;
}
cout << "NULL" << endl;
}
94
cout << "\nSelect your choice : ";
cin >> choice;
if (choice == 1)
{
cout << "\nQueue Linked List : ";
recDisplay(front);
}
else if (choice == 2)
{
iterDisplay(front);
}
else
{
cout << "Invalid choice" << endl;
}
}
}
int main()
{
Node *front = NULL;
95
Node *back = NULL;
switch (choice)
{
case 1:
cout << "Enter value to insert : ";
cin >> value;
96
case 2:
deletion(front, back);
break;
case 3:
display(front);
break;
case 4:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice. Please choose from 1 to 4" << endl;
}
} while (choice != 4);
return 0;
}
97
LAB 10
Implement a C++ program to insert nodes in BST.
Find Even and Odd Nodes
Find Sum and Average of all Nodes.
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *left;
98
Node *right;
Node(int value)
{
this->data = value;
this->left = NULL;
this->right = NULL;
}
};
99
else
{
root->right = insert(root->right, value);
}
return root;
}
100
{
if (root == NULL)
{
return 0;
}
101
return;
displayEven(root->left);
if (root->data % 2 == 0)
{
cout << root->data << " ";
}
displayEven(root->right);
}
int main()
102
{
Node *root = NULL;
int choice, value, sum;
do
{
cout << "\n1) Insert Node" << endl;
cout << "2) Display Sum" << endl;
cout << "3) Display Avg" << endl;
cout << "4) Display Even" << endl;
cout << "5) Display Odd" << endl;
cout << "6) Exit" << endl;
switch (choice)
{
case 1:
cout << "Enter value to insert : ";
cin >> value;
103
insert(root, value);
break;
case 2:
sum = displaySum(root);
cout << "Sum of Nodes : " << sum << endl;
break;
case 3:
displayAvg(root);
break;
case 4:
cout << "Even Nodes : ";
displayEven(root);
break;
case 5:
cout << "Odd Nodes : ";
displayOdd(root);
break;
104
case 6:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice" << endl;
}
} while (choice != 6);
return 0;
}
LAB 11
Implement Merge Sort in C++.
Also
Explain divide and conquer.
Take a list of 8 elements and dry run the Merge Sort Algorithm.
105
#include <iostream>
using namespace std;
int i = 0, j = 0, k = left;
while (i < n1 && j < n2)
106
{
if (leftArray[i] <= rightArray[j])
{
arr[k++] = leftArray[i++];
}
else
{
arr[k++] = rightArray[j++];
}
}
107
void mergeSort(int arr[], int left, int right)
{
if (left >= right)
{
return;
}
108
}
int main()
{
int length = 8;
int arr[length] = {12, 11, 13, 5, 6, 7, 1, 4};
return 0;
}
OUTPUT:
109
LAB 12
Implement Quick Sort Algorithm in C++.
110
#include <iostream>
using namespace std;
swap(arr[i], arr[j]);
}
}
return i + 1;
}
111
void quickSort(int arr[], int si, int ei)
{
if (si < ei)
{
int pIdx = partition(arr, si, ei);
112
int main()
{
int length = 8;
int arr[length] = {6, 3, 9, 5, 2, 8, 1, 4};
return 0;
}
OUTPUT:
113
114