You are on page 1of 10

CSL-221: Data Structures & Algorithms

Semester: BS IT – 03(A)

Lab 07: An array-based implementation of QUEUE


Exercises

Exercise 1

Write a program as follows for QUEUE

SOLUTION:

SOURCE CODE:

#include <iostream>
using namespace std;
int que[100], n = 100, front = -1, rear = -1;
void Insert() {
int val;
if (rear == n - 1)
cout << "Queue Overflow" << endl;
else {
if (front == -1) front = 0;
cout << "Insert the element in queue : " << endl;
cin >> val;
rear++;
que[rear] = val;
}
} void Deque() {
if (front == -1 || front > rear) {
cout << "Queue Underflow "; return;
}
else {
cout << "Element deleted from queue is : " << que[front] << endl;
front++;;
}
}
void Display() {
if (front == -1) cout << "Queue is empty" << endl;
else {
cout << "Queue elements are : ";
for (int i = front;
i <= rear; i++)
cout << que[i] << " ";
cout << endl;
}
} void main() {
int ch;
cout << "--------------Implementation Of Array-Based Queue----------- \n";
cout << "1)Enque an element to queue" << endl;
cout << "2) Deque element from queue" << endl;
cout << "3) Display all the elements of queue" << endl;
cout << "4) Exit" << endl;
do { cout << "Enter your choice : " << endl;
cin >> ch;
switch (ch) { case 1: Insert();
break;
case 2: Deque();
break;
case 3: Display();
break;
case 4: cout << "Exit" << endl;
break;
default: cout << "Invalid choice" << endl;
}
} while (ch != 4); system("pause");
}

OUTPUT:
Exercise 2

1. Test the program using the following procedure: QUEUE of size N=6
2.
3. Call enQueue(10)
4. Call enQueue(7)
5. Call enQueue(4)
6. Call deQueue ()
7. call deQueue()
8. Call enQueue(8)
9. Call enQueue(2)
10. Call Display ()

SOLUTION:

SOURCE CODE:

#include <iostream>
using namespace std;
int que[100], n = 6, front = -1, rear = -1;
void Insert() {
int val;
if (rear == n - 1) cout << "Queue Overflow" << endl;
else {
if (front == -1) front = 0;
cout << "Insert the element in queue : " << endl;
cin >> val;
rear++;
que[rear] = val;
}
} void Deque() {
if (front == -1 || front > rear) {
cout << "Queue Underflow ";
return;
}
else {
cout << "Element deleted from queue is : " << que[front] << endl;
front++;;
}
}
void Display() {
if (front == -1) cout << "Queue is empty" << endl;
else {
cout << "Queue elements are : ";
for (int i = front;
i <= rear;
i++) cout << que[i] << " ";
cout << endl;
}
} void main() {
int ch;
cout << "--------------Implementation Of Array-Based Queue----------- \n";
cout << "1)Enque an element to queue" << endl;
cout << "2) Deque element from queue" << endl;
cout << "3) Display all the elements of queue" << endl;
cout << "4) Exit" << endl;
do { cout << "Enter your choice : " << endl;
cin >> ch;
switch (ch) { case 1: Insert();
break;
case 2: Deque();
break;
case 3: Display();
break;
case 4: cout << "Exit" << endl;
break;
default: cout << "Invalid choice" << endl;
}
} while (ch != 4);
system("pause");
}

OUTPUT:
Exercise 3

There are 15 cars waiting in queue to refill the fuel at automated petrol pump station. You have
some specific information about each car such as car number which is unique for each car, car
model, and the amount of fuel which each car required to refill. You need to implement a system
which takes 15 cars as enqueue in a queue and dequeue cars after they have done with the refill.
1. Add a car in the queue.
2. Display all cars in the queue
3. Count the total number of cars after dequeue.

4. Dequeue a car after it is done with the refill.

SOLUTION:

SOURCE CODE:

#include<iostream>
#include<string>
using namespace std;
string queue[15];
int front = -1, rear = -1;
string item;
void Enque(string item){ if (rear<15){ if (front <= -1){ front = 0;
}
rear++;
queue[rear] = item;
}
else cout << "\tQUEUE OverFlow..\n";
} void Deque(){
if (front>-1 || front<rear){
front++;
}
else cout << "\tQUEUE underFlow..\n";
}
void total(){
cout << "Total Number of cars : " << (rear - front) + 1 << "\n";
} void Display(){
for (int i = front; i <= rear;
i++) cout << queue[i] << "\n";
cout <<endl;
} void main(){
cout << "----------Automated Petrol Pump------------\n";
int choice;
cout << "1. Car Enqueue \n";
cout << "2. Car Dequeue \n";
cout << "3. Display all Cars\n";
cout << "4. Display Total Number of Cars \n";
cout << "5. Exit \n";
cout << "----------------------------------\n";
do {
cout << "Enter choice : ";
cin >> choice;
switch (choice) {
case 1:
{
cout << "\tEnter the Vehicle's Number : ";
cin >> item;
Enque(item);
break;
}
case 2:
{
Deque();
break;
}
case 3:
{
Display();
break;
}
case 4:
{
total();
break;
} case 5: exit(1); break;
default: { cout << "Try Again \n";
}
}
} while (choice != 5); system("pause");
}

OUTPUT:
Exercise 4

Create a menu driven circular queue in which you perform the following
operations:  Enqueue
• Dequeue
• Display
• Exit
Also check whether the queue is empty or not.
SOLUTION:

SOURCE CODE:

#include<iostream>
#include<queue>
using namespace std;
class Queue { public:
int front = -1;
int rear = -1;
int array[10];
public:
Queue()
{
int front = -1;
int rear = -1;
for (size_t i = 0;
i < 10; i++)
{
array[i] = 0;
}
}
void Enqueue(int value)
{
if (rear == 9)
{
cout << "queue is in overflow condition" << endl;
return;
}
else if (front == -1 && rear == -1)
{
rear = 0; front = 0; array[rear] = value;
}
else
{
rear++;
array[rear] = value;
}
}
int dequeue()
{
if (front == -1 && rear == -1)
{
cout << "queue is in underflow condition" << endl;
}
else if (front == rear)
{
int x;
x = array[front];
array[front] = 0;
front = -1;
rear = -1;
return x;
}
else
{
int x;
x = array[front];
array[front] = 0;
front++;
return x;
}
}
void display()
{
for (size_t i = 0; i < 10; i++)
{
cout << array[i]; cout << endl;
}
}
int count()
{
return rear - front + 1;
}};
void main()
{
Queue que;
int option, value;
do{
cout << "-------------Circular Queue Implementation------------ - \n";
cout << "1. Enqueue" << endl;
cout << "2. Dequeue" << endl;
cout << "3. Display" << endl;
cout << "4. Exit" << endl;
cout << "Enter Your Choice : ";
cin >> option;
switch (option)
{
case 1:
cout << "Enter the element into the queue : ";
cin >> value;
que.Enqueue(value);
break;
case 2:
cout << "Elemnt dequed is" << "" << que.dequeue() << endl;
break;
case 3:
cout << "\t\t*Display*" << endl;
que.display();
break;
case 4:
cout << "\t\t*Exit*" << "" << que.count() << endl;
break;
}
}
while (option != 4);
system("pause");
}

OUTPUT:

You might also like