[go: up one dir, main page]

0% found this document useful (0 votes)
2 views21 pages

DSA-Lab Report 4

Uploaded by

safyankhan.ik804
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)
2 views21 pages

DSA-Lab Report 4

Uploaded by

safyankhan.ik804
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/ 21

NAME : MUHAMMAD SAFYAN

CLASS : BSSE(3rd)
ROLL NO : FL-249
SUBJECT : DSA LAB
LAB REPORT : 04
SUBMITTED TO : Mr. H.M. Adnan Asghar
------------------------------------------
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;
}
}

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";
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++)
{
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++)
{
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])


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


}
}

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

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;

default:
cout << "Invalid Choice. Please choose between 1 to 6"
<< endl;
}
} while (choice != 6);
}

console(output):
1.Push :
2.Pop:

3.Sort:
4.Search:
5.Display:

You might also like