0% found this document useful (0 votes)
17 views

DSA Manual

Uploaded by

wwwsafyan311p
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

DSA Manual

Uploaded by

wwwsafyan311p
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 114

LAB MANUAL

DATA STRUCTURE & ALGORITHMS


SEDA-225

DEPARTMENT OF SOFTWARE ENGINEERING


FACULTY OF ENGINEERING & COMPUTING
NATIONAL UNIVERSITY OF MODERN LANGUAGES
ISLAMABAD

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;

while (index > length)


{
cout << "\nIndex not found! Please try again :\n";
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;

cout << "\nInsertion Array : ";


for (int i = 0; i < length; i++)
{
cout << arr[i] << " ";
}
return 0;
}
console(output):

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;

while (index > length)


{

6
cout << "\nIndex not found! Please try again :\n";
cout << "Enter index number : ";
cin >> index;
}

for (int i = index; i < length - 1; i++)


{
arr[i] = arr[i + 1];
}

cout << "\nDeletion Array : ";

for (int i = 0; i < length - 1; i++)


{
cout << arr[i] << " ";
}
return 0;
}

7
console(output):

LAB 2
1. Matrix Addition(2D Array):
#include <iostream>
using namespace std;

int main()
{
int rows, columns;

cout << "Enter number of rows for matrix 1 & 2 : ";


cin >> rows;

8
cout << "Enter number of columns for matrix 1 & 2 : ";
cin >> columns;

while (rows != columns)


{
cout << "\nError : Number of rows and columns are not equal.
Please try again :\n";

cout << "Enter number of rows for matrix 1 & 2 : ";


cin >> rows;

cout << "Enter number of columns for matrix 1 & 2 : ";


cin >> columns;
}

int matrix1[rows][columns];
int matrix2[rows][columns];

int sumMatrix[rows][columns];

cout << "\nMatrix number 1 : " << endl;


for (int i = 0; i < rows; i++)

9
{
for (int j = 0; j < columns; j++)
{
cout << "Enter values at index " << i << j << " : ";
cin >> matrix1[i][j];
}
}

cout << "\nMatrix number 2 : " << endl;


for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
cout << "Enter values at index " << i << j << " : ";
cin >> matrix2[i][j];
}
}

for (int i = 0; i < rows; i++)


{
for (int j = 0; j < columns; j++)
{

10
sumMatrix[i][j] = matrix1[i][j] + matrix2[j][i];
}
}

cout << "\nSum of Matrix 1 & 2 : " << endl;


for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
cout << sumMatrix[i][j] << " ";
}

cout << endl;


}
return 0;
}
console(output):

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;

cout << "Enter number of columns for matrix 1 : ";


cin >> column1;

cout << "\nEnter number of rows for matrix 2 : ";


cin >> row2;

cout << "Enter number of columns for matrix 2 : ";


cin >> column2;

while (row1 != column2)


{
cout << "\nError : Matrix 1 rows & Matrix 2 columns are not equal.
Please try again :\n";

cout << "Enter number of rows for matrix 1 : ";


cin >> row1;

cout << "Enter number of columns for matrix 1 : ";

13
cin >> column1;

cout << "\nEnter number of rows for matrix 2 : ";


cin >> row2;

cout << "Enter number of columns for matrix 2 : ";


cin >> column2;
}
int matrix1[row1][column1];
int matrix2[row2][column2];

int transposeMatrix[column2][row2];
int mulMatrix[row1][column2] = {0};

cout << "\nMatrix number 1 : " << endl;


for (int i = 0; i < row1; i++)
{
for (int j = 0; j < column1; j++)
{
cout << "Enter values at index " << i << j << " : ";
cin >> matrix1[i][j];
}

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;

void bubbleSort(int stackArr[], int top)


{
for (int i = top; i > 0; i--)
{
for (int j = top; j > 0; j--)
{
if (stackArr[j] < stackArr[j - 1])
{
int tempVal = stackArr[j];
stackArr[j] = stackArr[j - 1];
stackArr[j - 1] = tempVal;
}
}
}
}

int main()

18
{
int length;
cout << "Enter length : ";
cin >> length;

int stackArr[length];

int top = -1;

cout << "\n";


for (int i = 0; i < length; i++)
{
cout << "Enter value at index " << i << " : ";
cin >> stackArr[i];
top++;
}

cout << "\nStack Array : ";


for (int i = top; i >= 0; i--)
{
cout << stackArr[i] << " ";
}

19
bubbleSort(stackArr, top);

cout << "\nBubble Sort Stack Array : ";


for (int i = top; i >= 0; i--)
{
cout << stackArr[i] << " ";
}

return 0;
}
console(output):

20
2. Take Stack Array from User and Apply Selection Sort Using Stack
Rule.
#include <iostream>
using namespace std;

void selectionSort(int stackArr[], int top)


{
for (int i = top; i > 0; i--)
{
int smallest = i;

for (int j = i - 1; j >= 0; j--)


{
if (stackArr[smallest] < stackArr[j])
{
smallest = j;
}
}

int tempVal = stackArr[smallest];


stackArr[smallest] = stackArr[i];
stackArr[i] = tempVal;

21
}
}
int main()
{
int length;
cout << "Enter length : ";
cin >> length;

int stackArr[length];
int top = -1;

cout << "\n";


for (int i = 0; i < length; i++)
{
cout << "Enter value at index " << i << " : ";
cin >> stackArr[i];
top++;
}

cout << "\nStack Array : ";


for (int i = top; i >= 0; i--)
{

22
cout << stackArr[i] << " ";
}

selectionSort(stackArr, top);

cout << "\nSelection Sort Stack Array : ";


for (int i = top; i >= 0; i--)
{
cout << stackArr[i] << " ";
}
return 0;
}
console(output):

23
3. Take Array from User and Apply Binary Search
#include <iostream>
using namespace std;

int binarySearch(int stackArr[], int top, int searchVal)


{
int start = 0, end = top;

while (start <= end)


{
int mid = (start + end) / 2;

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];

int top = -1;

cout << "\nPlease enter values in increasing order :\n";

for (int i = 0; i < length; i++)


{
cout << "Enter value at index " << i << " : ";

25
cin >> stackArr[i];

top++;
}

cout << "\nStack Array : ";

for (int i = top; i >= 0; i--)


{
cout << stackArr[i] << " ";
}

int searchVal;
cout << "\nEnter the value to search : ";
cin >> searchVal;

cout << "\nSearch value found at index : " << binarySearch(stackArr,


top, 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++;
}
}

void bubbleSort(int queueArr[], int front, int back)


{
int sortChoice;

do
{
cout << "\n1. Ascending Sort\n";
cout << "2. Descending Sort\n";

30
cout << "3. Exit Sort\n";

cout << "\nSelect your choice : ";


cin >> sortChoice;

if (sortChoice == 3)
{
cout << "Exiting..." << endl;
return;
}

if (sortChoice != 1 && sortChoice != 2)


{
cout << "Invalid choice. Please choose between 1 to 3" << endl;
}

} while (sortChoice != 1 && sortChoice != 2);

for (int i = front; i <= back; i++)


{
for (int j = front; j < back - (i - front); j++)
{

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]);
}
}
}
}
}

int linearSearch(int queueArr[], int searchVal, int front, int back)


{
for (int i = front; i <= back; i++)
{

32
if (queueArr[i] == searchVal)
{
return i;
}
}

return -1;
}

int binarySearch(int queueArr[], int searchVal, int front, int back)


{
cout << "\nIn binary search, Please convert queue into ascending
order : ";
bubbleSort(queueArr, front, back);

int start = front, end = back;

while (start <= end)


{
int mid = start + (end - start) / 2;

if (searchVal > queueArr[mid])

33
{
start = mid + 1;
}
else if (searchVal < queueArr[mid])
{
end = mid - 1;
}
else
{
return mid;
}
}

return -1;
}

void search(int queueArr[], int searchVal, int front, int back)


{
int searchChoice;
int linearResult, binaryResult;

do

34
{
cout << "\n1. Linear Search\n";
cout << "2. Binary Search\n";
cout << "3. Exit Search\n";

cout << "\nSelect your choice : ";


cin >> searchChoice;

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);
}

void display(int queueArr[], int front, int back)


{
if (front == -1 || front > back)
{
cout << "Queue is empty" << endl;
}
else
{
cout << "Queue Array : ";
for (int i = front; i <= back; i++)
{
cout << queueArr[i] << " ";
}

cout << endl;


}
}

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";

cout << "\nSelect your choice : ";


cin >> choice;

switch (choice)
{
case 1:

38
cout << "Enter value to insert : ";
cin >> insertVal;

push(queueArr, insertVal, size, front, back);


break;
case 2:
pop(queueArr, front, back);
break;
case 3:
if (front == -1 || front > back)
{
cout << "Queue is empty" << endl;
}
else
{
bubbleSort(queueArr, front, back);

cout << "Bubble Sort : ";


for (int i = front; i <= back; i++)
{
cout << queueArr[i] << " ";
}

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 recSumOfDigits(int num)


{
if (num == 0)
{
return 0;
}
else
{
return (num % 10) + recSumOfDigits(num / 10);
}
}

int main()
{
int num = 12345;
45
int digitSum = recSumOfDigits(num);

cout << "Sum of digits : " << digitSum;

return 0;
}
console(output):

Task 2: Power Function


Question: Implement a recursive function to calculate the power of a
number. Write the function and test it with base = 2 and exp = 3. What
is the result of 2^3?
#include <iostream>
using namespace std;

int recPowerFunc(int base, int exponent)


{
if (exponent == 0)
{
46
return 1;
}
else
{
return base * recPowerFunc(base, exponent - 1);
}
}
int main()
{
int base = 2, exponent = 3;

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;
}
};

void insertAtTail(Node *&head, int value)


{
Node *newNode = new Node(value);
48
if (head == NULL)
{
head = newNode;
}
else
{
Node *temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = newNode;
}
}

void displayEven(Node *head)


{
Node *temp = head;
int node = 1;

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;
}
}

void displayAll(Node *head)


{
Node *temp = head;

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";

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:
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;
}
};

void insertAtTail(Node *&head, int value)


{
Node *newNode = new Node(value);

if (head == NULL)
{
head = newNode;
}
else
{
Node *temp = head;

while (temp->next != NULL)


{

55
temp = temp->next;
}
temp->next = newNode;
}
}

int maxNode(Node *head)


{
if (head == NULL)
{
return -1;
}
if (head->next == NULL)
{
return head->data;
}

int max = maxNode(head->next);


return (head->data > max) ? head->data : max;
}

void displayMax(Node *head)

56
{
int maxValue = maxNode(head);

if (maxValue == -1)
{
cout << "linked list is empty" << endl;
}
else
{
cout << "Maximum Node Value : " << maxValue << endl;
}
}

void displayAll(Node *head)


{
Node *temp = head;

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;
}
};

void insertAtTail(Node *&head, int value)


{
Node *newNode = new Node(value);

61
if (head == NULL)
{
head = newNode;
}
else
{
Node *temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = newNode;
}
}

int recNumOfNodes(Node *head)


{
if (head == NULL)
{
return 0;
}
else

62
{
return 1 + recNumOfNodes(head->next);
}
}

void displayNumOfNodes(Node *head)


{
int count = recNumOfNodes(head);
cout << "Number of Nodes : " << count << endl;
}

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;

Node(string name, int rollNo)


{
this->name = name;
this->rollNo = rollNo;

66
this->next = NULL;
}
};

void insertAtStart(Node *&head, string name, int rollNo)


{
Node *newNode = new Node(name, rollNo);

if (head == NULL)
{
head = newNode;
cout << "Node inserted at start" << endl;
}
else
{
newNode->next = head;
head = newNode;
cout << "Node inserted at start" << endl;
}
}

void insertAtEnd(Node *&head, string name, int rollNo)

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;
}
}

void deleteFromStart(Node *&head)

68
{
if (head == NULL)
{
cout << "Linked List is empty" << endl;
}
else
{
head = head->next;
cout << "Node deleted from start" << endl;
}
}

void deleteFromEnd(Node *&head)


{
if (head == NULL)
{
cout << "Linked List is empty" << endl;
}
else if (head->next == NULL)
{
head = NULL;
}

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;
}
}

void display(Node *head)


{
if (head == NULL)
{
cout << "Linked List is empty" << 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;

int choice, rollNo;


string name;

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";

cout << "\nSelect your choice : ";


cin >> choice;

switch (choice)
{
case 1:
cout << "Enter Name : ";
cin >> name;

cout << "Enter Roll No : ";


cin >> rollNo;

insertAtStart(head, name, rollNo);


break;

72
case 2:
cout << "Enter Name : ";
cin >> name;

cout << "Enter Roll No : ";


cin >> rollNo;

insertAtEnd(head, name, rollNo);


break;

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;
}
};

void insertAtStart(Node *&head, Node *&tail, int value)


{
Node *newNode = new Node(value);

if (head == NULL)
{
head = tail = newNode;
}
else
{
newNode->next = head;
head->prev = newNode;
head = newNode;

76
}
cout << "Node inserted at start" << endl;
}

void insertAtEnd(Node *&head, Node *&tail, int value)


{
Node *newNode = new Node(value);

if (tail == NULL)
{
head = tail = newNode;
}
else
{
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
cout << "Node inserted at end" << endl;
}

void insert(Node *&head, Node *&tail, int value)

77
{
int choice;

cout << "\n1) Insert At Start" << endl;


cout << "2) Insert At End" << endl;

cout << "\nSelect your choice : ";


cin >> 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;
}
}

void reverseDisplay(Node *tail)


{
if (tail == NULL)

79
{
cout << "Linked List is empty" << endl;
}
else
{
Node *currNode = tail;
while (currNode != NULL)
{
cout << currNode->data << " -> ";
currNode = currNode->prev;
}
cout << "NULL" << endl;
}
}

void display(Node *head, Node *tail)


{
int choice;

cout << "\n1) Forward Display" << endl;


cout << "2) Reverse Display" << 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;

cout << "\nSelect your choice : ";


cin >> choice;

switch (choice)
{
case 1:
cout << "Enter value to insert : ";
cin >> value;

insert(head, tail, value);


break;
case 2:
display(head, tail);
break;
case 3:
cout << "Exiting..." << endl;
break;

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;
}
};

void insertion(Node *&top, int value)


{

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;
}

void deletion(Node *&top)


{
if (top == NULL)
{
cout << "Stack is empty" << endl;
}
else
{

85
cout << top->data << " is deleted from top of stack" << endl;
top = top->next;
}
}

void recDisplay(Node *top)


{
if (top == NULL)
{
cout << "NULL" << endl;
return;
}

cout << top->data << " -> ";


recDisplay(top->next);
}

void iterDisplay(Node *top)


{
cout << "\nStack Linked List : ";
while (top != NULL)
{

86
cout << top->data << " -> ";
top = top->next;
}
cout << "NULL" << endl;
}

void display(Node *top)


{
if (top == NULL)
{
cout << "Stack is empty" << endl;
}
else
{
int choice;

cout << "\n1) Recursive Display" << endl;


cout << "2) Iterative Display" << endl;

cout << "\nSelect your choice : ";


cin >> choice;

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";

cout << "\nSelect your choice : ";


cin >> choice;

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;
}
};

void insertion(Node *&front, Node *&back, int value)


{
Node *newNode = new Node(value);

91
if (front == NULL)
{
front = back = newNode;
}
else
{
back->next = newNode;
back = newNode;
}
cout << value << " inserted to Queue" << endl;
}

void deletion(Node *&front, Node *&back)


{
if (front == NULL)
{
cout << "Queue is empty" << endl;
}
else
{
cout << front->data << " deleted from queue" << endl;
front = front->next;

92
if (front == NULL)
{
back == NULL;
}
}
}

void recDisplay(Node *front)


{
if (front == NULL)
{
cout << "NULL" << endl;
return;
}

cout << front->data << " -> ";


recDisplay(front->next);
}

void iterDisplay(Node *front)


{

93
cout << "\nQueue Linked List : ";
while (front != NULL)
{
cout << front->data << " -> ";
front = front->next;
}
cout << "NULL" << endl;
}

void display(Node *front)


{
if (front == NULL)
{
cout << "Queue is empty" << endl;
}
else
{
int choice;

cout << "\n1) Recursive Display" << endl;


cout << "2) Iterative Display" << 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;

int choice, value;


do
{
cout << "\n1) Insert\n";
cout << "2) Delete\n";
cout << "3) Display\n";
cout << "4) Exit\n";

cout << "\nSelect your choice : ";


cin >> choice;

switch (choice)
{
case 1:
cout << "Enter value to insert : ";
cin >> value;

insertion(front, back, value);


break;

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;
}
};

Node *insert(Node *&root, int value)


{
if (root == NULL)
{
root = new Node(value);
return root;
}

if (value < root->data)


{
root->left = insert(root->left, value);
}

99
else
{
root->right = insert(root->right, value);
}

return root;
}

int displaySum(Node *root)


{
if (root == NULL)
{
return 0;
}
else
{
return displaySum(root->left) + displaySum(root->right) + root-
>data;
}
}

int countNodes(Node *root)

100
{
if (root == NULL)
{
return 0;
}

return countNodes(root->left) + countNodes(root->right) + 1;


}

void displayAvg(Node *root)


{
int count = countNodes(root);
int sum = displaySum(root);

double avg = (sum / count);

cout << "Average of Nodes : " << avg << endl;


}

void displayEven(Node *root)


{
if (root == nullptr)

101
return;
displayEven(root->left);
if (root->data % 2 == 0)
{
cout << root->data << " ";
}
displayEven(root->right);
}

void displayOdd(Node *root)


{
if (root == nullptr)
return;
displayOdd(root->left);
if (root->data % 2 != 0)
{
cout << root->data << " ";
}
displayOdd(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;

cout << "\nSelect your choice : ";


cin >> choice;

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;

void merge(int arr[], int left, int mid, int right)


{
int n1 = mid - left + 1;
int n2 = right - mid;

int leftArray[n1], rightArray[n2];

for (int i = 0; i < n1; i++)


{
leftArray[i] = arr[left + i];
}

for (int j = 0; j < n2; j++)


{
rightArray[j] = arr[mid + 1 + j];
}

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++];
}
}

while (i < n1)


{
arr[k++] = leftArray[i++];
}

while (j < n2)


{
arr[k++] = rightArray[j++];
}
}

107
void mergeSort(int arr[], int left, int right)
{
if (left >= right)
{
return;
}

int mid = left + (right - left) / 2;

mergeSort(arr, left, mid);


mergeSort(arr, mid + 1, right);

merge(arr, left, mid, right);


}

void printArray(int arr[], int size)


{
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
cout << endl;

108
}

int main()
{
int length = 8;
int arr[length] = {12, 11, 13, 5, 6, 7, 1, 4};

cout << "\nOriginal array: ";


printArray(arr, length);

mergeSort(arr, 0, length - 1);

cout << "\nSorted Array: ";


printArray(arr, length);
cout << "\n";

return 0;
}
OUTPUT:

109
LAB 12
Implement Quick Sort Algorithm in C++.

110
#include <iostream>
using namespace std;

int partition(int arr[], int si, int ei)


{
int pivot = arr[ei];
int i = si - 1;

for (int j = si; j < ei; j++)


{
if (arr[j] < pivot)
{
i++;

swap(arr[i], arr[j]);
}
}

swap(arr[i + 1], arr[ei]);

return i + 1;
}

111
void quickSort(int arr[], int si, int ei)
{
if (si < ei)
{
int pIdx = partition(arr, si, ei);

quickSort(arr, si, pIdx - 1);


quickSort(arr, pIdx + 1, ei);
}
}

void printArr(int arr[], int length)


{
cout << "Quick Sort Array: ";
for (int i = 0; i < length; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}

112
int main()
{
int length = 8;
int arr[length] = {6, 3, 9, 5, 2, 8, 1, 4};

cout << "Original Array: ";


for (int i = 0; i < length; i++)
{
cout << arr[i] << " ";
}
cout << endl;

quickSort(arr, 0, length - 1);


printArr(arr, length);

return 0;
}
OUTPUT:

113
114

You might also like