Lab 4

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

DATA STRUCTURES AND ALGORITHMS

LAB-4

SUBMITTED BY

Tayyaba Riaz
Roll No: 2020-bcs-039

SUBMITTED TO

SIR ADEEL KHALID


Task 1
Perform all sample codes and understand them.

 Program 1

#include<iostream>
using namespace std;
#define n 10
class Queue
{
public:
int arr[n];
int front;
int rear;
public:
Queue() {
front = -1;
rear = -1;
}
bool isEmpty() {
if (front == rear == -1 || front > rear)
return true;
else
return false;
}
bool isFull() {
if (rear == n - 1)
return true;
else
return false;
}

void enqueue(int data) {


if (!isFull() && isEmpty()) {
rear++;
front++;
arr[rear] = data;

else if (!isFull() && !isEmpty()) {


rear++;
arr[rear] = data;
}
else
cout << "Queue is overflow : ";
}
void dequeue() {
if (isEmpty()) {
cout << "Queue is underflow ";
}
else
{

2
cout << "\nDeleted : " <<arr[front];
front++;
if (front == rear)
front = rear = -1;
}
front++;
}
void display()
{
if (isEmpty())
cout << "\nQueue is Empty!!!";
else
{
int i;
cout << "\nQueue elements are:\n";
for (i = front; i <= rear; i++)
cout << arr[i] << endl;
}
}

};
int main() {
int value, choice;
Queue q;
while (1) {
cout << "\n\n***** MENU *****\n";
cout << "1. Insertion\n2. Deletion\n3. Display\n4. Exit";
cout << "\nEnter your choice: ";
cin >> choice;
switch (choice) {
case 1: cout << "Enter the value to be insert: ";
cin >> value;
q.enqueue(value);
break;
case 2: q.dequeue();
break;
case 3: q.display();
break;

case 4: exit(0);
default: cout << "\nWrong selection!!! Try again!!!";
}

}
getchar();
return 0;
}

OUTPUT

3
 Program 2

#include<iostream>
#include<conio.h>
using namespace std;
#define SIZE 5
void enQueue(int);
void deQueue();
void display();
int cQueue[SIZE], front = -1, rear = -1;
void main()
{
int choice, value;

while (1) {
cout << "\n****** MENU ******\n";
cout << "1. Insert\n2. Delete\n3. Display\n4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: cout << "\nEnter the value to be insert: ";
cin >> value;
enQueue(value);
break;
case 2: deQueue();
break;
case 3: display();
break;
case 4: exit(0);
default: cout << "\nPlease select the correct choice!!!\n";
}

4
}
}
void enQueue(int value)
{
if ((front == 0 && rear == SIZE - 1) || (front == rear + 1))
cout << "\nCircular Queue is Full! Insertion not possible!!!\n";
else {
if (rear == SIZE - 1 && front != 0)
rear = -1;
cQueue[++rear] = value;
cout << "\nInsertion Success!!!\n";
if (front == -1)
front = 0;
}
}
void deQueue()
{
if (front == -1 && rear == -1)
cout << "\nCircular Queue is Empty! Deletion is not possible!!!\n";
else {
cout << "\nDeleted element :" << cQueue[front++];
if (front == SIZE)
front = 0;
if (front - 1 == rear)
front = rear = -1;
}
}
void display()

{
if (front == -1)
cout << "\nCircular Queue is Empty!!!\n";
else {
int i = front;
cout << "\nCircular Queue Elements are : \n";
if (front <= rear) {
while (i <= rear)
cout << "\t" << cQueue[i++];
}
else {
while (i <= SIZE - 1)
cout << "\t" << cQueue[i++];
i = 0;
while (i <= rear)
cout << "\t" << cQueue[i++];
}
}
}

OUTPUT:

5
TASK 2
Implement any real world example using queue.

#include "pch.h"
#include<iostream>
#include<string>
using namespace std;
#define n 100
class Queue
{
public:
int arr[n];
int front;
int rear;
public:
Queue() {
front = -1;
rear = -1;
}
bool isEmpty() {
if (front == rear == -1 || front > rear)
return true;
else
return false;
}
bool isFull() {
if (rear == n - 1)
return true;
else

6
return false;
}

void enqueue(int value) {


if (rear == n - 1)
cout << "\nQueue is Full!!! Insertion is not possible!!!";
else {
if (front == -1)
front = 0;
rear++;
arr[rear] = value;
}
}

void dequeue() {
if (isEmpty()) {
cout << "Queue is underflow ";
}
else
{
front++;
if (front == rear)
front = rear = -1;
}
front++;
}
void display()
{
if (isEmpty())
cout << "\nQueue is Empty!!!";
else
{
int i;
for (i = front; i <= rear; i++)
cout <<"CAR : "<< arr[i] << endl;
}
}

};
int main() {
Queue q;
string a;
cout << "WELCOME TO WAH PETROL PUMP : "<<endl;
cout << " PLEASE MAKE A QUEUE :\n";
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
q.enqueue(4);
q.enqueue(5);
if (q.isEmpty()) {
cout << "QUEUE IS EMPTY ";
}
else
{
cout << "\nNumber of cars in Queue \n";
q.display();
}

7
done: cout << "\nPress Done after paying amount : ";
cin >> a;
if ((a == "Done" || a == "done" )&& !q.isEmpty()) {
q.dequeue();
cout << "\nNext Car ";
goto done;
}
cout << "\nRemaining cars : ";
q.display();

return 0;
}

OUTPUT

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy