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

Practical Number 9 Dsu

Uploaded by

Vaibhav Malkar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Practical Number 9 Dsu

Uploaded by

Vaibhav Malkar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

PRACTICAL NUMBER 9 DSU

#include <iostream>
#define max 9
using namespace std;

class Queue {
private:
int item[max], front, rear;

public:
Queue() {
front = rear = -1;
}

int isFull() {
if ( rear == max - 1) {
return 1;
}
if (front == rear + 1) {
return 1;
}
return 0;
}
int isEmpty() {
if (front == -1)
return 1;
else
return 0;
}

void enQueue(int value) {


if (isFull()) {
cout << "Queue is full";
} else {
if (front == -1) front = 0;
rear = (rear + 1) % max;
item[rear] = value;
cout << endl
<< "Inserted " << value << endl;
}
}
int deQueue() {
int element;
if (isEmpty()) {
cout << "Queue is empty" << endl;
return (-1);
} else {
element = item[front];
if (front == rear) {
front = -1;
rear = -1;
}

else {
front = (front + 1) % max;
}
return (element);
}
}

void display() {
int i;
if (isEmpty()) {
cout << endl
<< "Empty Queue" << endl;
} else {
cout << "Front -> " << front;
cout << endl
<< "Items -> ";
for (i = front; i != rear; i = (i + 1) % max)
cout << item[i];
cout << item[i];
cout << endl
<< "Rear -> " << rear;
}
}
};

int main() {
Queue q;
q.deQueue();
q.enQueue(72);
q.enQueue(88);
q.enQueue(47);
q.enQueue(23);
q.display();

int elem = q.deQueue();

if (elem != -1)
cout << endl
<< "Deleted Element is " << elem;

q.display();
q.enQueue(7);
q.display();
q.enQueue(8);
}

You might also like