You are on page 1of 14

LAB TASK 6–Circular Queue and Implementation of Deque using circular

array

Name / Group ID: ________________________ Reg. No: ___________________

Class / Section: __________________________ Date: _____________________

Time Required : 3 hrs


Programming Language :C++
Software Required :Dev -C++
Hardware Required : Computer System

Circular Queue ADT


Circular Queue is a linear data structure in which the operations are performed based on
FIFO (First In First Out) principle and the last position is connected back to the first position
to make a circle. It is also called ‘Ring Buffer’.

In a normal Queue, we can insert elements until queue becomes full. But once queue becomes
full, we can not insert the next element even if there is a space in front of queue.
Operations on Circular Queue:
Front: Get the front item from queue.

Rear: Get the last item from queue.

enQueue(value) This function is used to insert an element into the circular queue. In a circular
queue, the new element is always inserted at Rear position.
Steps:

1. Check whether queue is Full – Check ((rear == SIZE-1 && front == 0) || (rear == front-1)).

2. If it is full then display Queue is full. If queue is not full then, check if (rear == SIZE – 1
&&front != 0) if it is true then set rear=0 and insert element.

deQueue() This function is used to delete an element from the circular queue. In a circular queue,
the element is always deleted from front position.
Steps:

3. Check whether queue is Empty means check (front==-1).

4. If it is empty then display Queue is empty. If queue is not empty then step 3.

5. Check if (front==rear) if it is true then set front=rear= -1 else check if (front==size-1), if it is


true then set front=0 and return the element.
Circular Queue Implementation:
//C++ program to implement a circular queue

#include <iostream>
using namespace std;

int cqueue[3];
int front = -1, rear = -1, n=5;

void insertCQ(int val) {


if ((front == 0 && rear == n-1) || (front == rear+1)) {
cout<<"Queue Overflow \n";
return;
}
if (front == -1) {
front = 0;
rear = 0;
} else {
if (rear == n - 1)
rear = 0;
else
rear = rear + 1;
}
cqueue[rear] = val ;
}
void deleteCQ() {
if (front == -1) {
cout<<"Queue Underflow\n";
return ;
}
cout<<"Element deleted from queue is : "<<cqueue[front]<<endl;

if (front == rear) {
front = -1;
rear = -1;
} else {
if (front == n - 1)
front = 0;
else
front = front + 1;
}
}
void displayCQ() {
int f = front, r = rear;
if (front == -1) {
cout<<"Queue is empty"<<endl;
return;
}
cout<<"Queue elements are :\n";
if (f <= r) {
while (f <= r){
cout<<cqueue[f]<<" ";
f++;
}
} else {
while (f <= n - 1) {
cout<<cqueue[f]<<" ";
f++;
}
f = 0;
while (f <= r) {
cout<<cqueue[f]<<" ";
f++;
}
}
cout<<endl;
}
int main() {
insertCQ(15);
insertCQ(20);
insertCQ(25);
displayCQ();
deleteCQ();
displayCQ();
insertCQ(35);
displayCQ();
deleteCQ();
displayCQ();
insertCQ(50);
displayCQ();
return 0;
}

Output
Deque
Deque or Double Ended Queue is a generalized version of Queue data structure that allows
insert and delete at both ends.In previous post we had discussed introduction of deque. Now
in this post we see how we implement deque Using circular array.

Operations on Deque:
Mainly the following four basic operations are performed on queue:

insetFront(): Adds an item at the front of Deque.

insertRear(): Adds an item at the rear of Deque.

deleteFront(): Deletes an item from front of Deque.

deleteRear(): Deletes an item from rear of Deque.

In addition to above operations, following operations are also supported


getFront(): Gets the front item from queue.

getRear(): Gets the last item from queue.

isEmpty(): Checks whether Deque is empty or not.

isFull(): Checks whether Deque is full or not.

Circular array implementation deque


For implementing deque, we need to keep track of two indices, front and rear. We
enqueue(push) an item at the rear or the front end of qedue and dequeue(pop) an item from
both rear and front end.

Working
1. Create an empty array ‘arr’ of size ‘n’
initialize front = -1 ,rear = 0
Inserting First element in deque, at either front or rear will lead to the same result.
After insert Front Points = 0 and Rear points = 0

Insert Elements at Rear end

a). First we check deque if Full or Not

b). IF Rear == Size-1


then reinitialize Rear = 0 ;
Else increment Rear by '1'
and push current key into Arr[ rear ] = key

Front remain same.

Insert Elements at Front end

a). First we check deque if Full or Not

b). IF Front == 0 || initial position, move Front


to points last index of array
front = size - 1
Else decremented front by '1' and push
current key into Arr[ Front] = key

Rear remain same.


Delete Element From Rear end
a). first Check deque is Empty or Not

b). If deque has only one element


front = -1 ; rear =-1 ;
Else IF Rear points to the first index of array
it's means we have to move rear to points
last index [ now first inserted element at
front end become rear end ]
rear = size-1 ;
Else || decrease rear by '1'
rear = rear-1;

Delete Element From Front end


a). first Check deque is Empty or Not

b). If deque has only one element


front = -1 ; rear =-1 ;
Else IF front points to the last index of the array
it's means we have no more elements in array so
we move front to points first index of array
front = 0 ;
Else || increment Front by '1'
front = front+1;
Deque Implementation:
//C++ program to implement a Deque using circular
#include<iostream>
using namespace std;

#define MAX_size 10 // Maximum size of array or Dequeue

int array[MAX_size];
int front = -1;
int rear=0;
int size=5;

// Operations on Deque:
void insertfront(int key);
void insertrear(int key);
void deletefront();
void deleterear();
int getFront();
int getRear();

// Check if Deque is full


bool isFull(){
return ((front == 0 && rear == size-1)||front == rear+1);
}
// Check if Deque is empty
bool isEmpty(){
return (front == -1);
}

// Insert an element at front of the deque


void insertfront(int key)
{
if (isFull()) {
cout<<"Overflow!!\n" <<endl;
return;
}

// If queue is initially empty,set front=rear=0; start of deque


if (front == -1) {
front = 0;
rear = 0;
}
else if (front == 0) // front is first position of queue
front = size - 1 ;
else // decrement front 1 position
front = front-1;

array[front] = key ; // insert current element into Deque


}

// insert element at the rear end of deque


void insertrear(int key)
{
if (isFull()) {
cout<< " Overflow!!\n " <<endl;
return;
}

// If queue is initially empty,set front=rear=0; start of deque


if (front == -1) {
front = 0;
rear = 0;
}
else if (rear == size-1) // rear is at last position of queue
rear = 0;
else // increment rear by 1 position
rear = rear+1;

array[rear] = key ; // insert current element into Deque


}

// Delete element at front of Deque


void deletefront()
{
if (isEmpty())
{
cout<< "Queue Underflow!!\n" <<endl;
return ;
}

// Deque has only one element


if (front == rear)
{
front = -1;
rear = -1;
}
else
// back to initial position
if (front == size -1)
front = 0;

else // remove current front value from Deque;increment front by 1


front = front+1;
}

// Delete element at rear end of Deque


void deleterear()
{
if (isEmpty())
{
cout<<" Underflow!!\n" <<endl ;
return ;
}
// Deque has only one element
if (front == rear)
{
front = -1;
rear = -1;
}
else if (rear == 0)
rear = size-1;
else
rear = rear-1;
}
// retrieve front element of Deque
int getFront()
{
if (isEmpty()) {
cout<< " Underflow!!\n" <<endl;
return -1 ;
}
return array[front];
}

// retrieve rear element of Deque


int getRear()
{
if(isEmpty() || rear < 0) {
cout<< " Underflow!!\n" <<endl;
return -1 ;
}
return array[rear];
}

//main program
int main()
{
//Deque dq(5);
cout<<"Insert element 1 at rear end \n";
insertrear(1);

cout<< "insert element 3 at rear end \n";


insertrear(3);

cout<< "rear element of deque " << " " <<getRear() <<endl;

deleterear();
cout<<"After deleterear, rear = " <<getRear() <<endl;

cout<< "inserting element 5 at front end \n";


insertfront(5);

cout<< "front element of deque " << " "<<getFront() <<endl;


deletefront();

cout<< "After deletefront, front = " <<getFront() <<endl;


return 0;
}

Output
Exercise:

Run the given program and write down the output and explain the steps.

Program

// C++ program to find circular tour for a truck


#include <iostream>
using namespace std;

// A petrol pump has petrol and distance to next petrol pump


struct petrolPump
{
int petrol;
int distance;
};

// The function returns starting point if there is a possible solution,


// otherwise returns -1
int printTour(struct petrolPumparr[], int n)
{
// Consider first petrol pump as a starting point
int start = 0;
int end = 1;

int curr_petrol = arr[start].petrol - arr[start].distance;

/* Run a loop while all petrol pumps are not visited.


And we have reached first petrol pump again with 0 or more petrol */
while (end != start || curr_petrol< 0)
{
// If curremt amount of petrol in truck becomes less than 0, then
// remove the starting petrol pump from tour
while (curr_petrol< 0 &&start != end)
{
// Remove starting petrol pump. Change start
curr_petrol -= arr[start].petrol - arr[start].distance;
start = (start + 1) % n;

// If 0 is being considered as start again, then there is no


// possible solution
if (start == 0)
return -1;
}

// Add a petrol pump to current tour


curr_petrol += arr[end].petrol - arr[end].distance;

end = (end + 1) % n;
}
// Return starting point
return start;
}

// Driver code
int main()
{
struct petrolPumparr[] = {{6, 4}, {3, 6}, {7, 3}};

int n = sizeof(arr)/sizeof(arr[0]);
int start = printTour(arr, n);

if(start == -1)
cout<<"No solution";
else
cout<<"Start = "<<start;

return 0;
}

Output :

Start=2

Steps are As Follow:


 Firstly we Create a Structure Name as Petrolpump
 A Petrolpump has petrol and distance to next petrol pump.
 Consider first petrol pump as a starting point
 The function returns starting point if there is a possible solution otherwise returns -1
 Intializing the Variables.
 Run a loop while all petrol pumps are not visited. And we have reached first petrol pump
again with 0 or more petrol.
 If curremt amount of petrol in truck becomes less than 0, then remove the starting petrol
pump from tour
 Remove starting petrol pump. Change start
 If 0 is being considered as start again, then there is no possible solution
 Add a petrol pump to current tour.
 Displays and run the value in main and Return Value.

You might also like