LAB TASK 6-Circular Queue and Implementation of Deque Using Circular Array
LAB TASK 6-Circular Queue and Implementation of Deque Using Circular Array
LAB TASK 6-Circular Queue and Implementation of Deque Using Circular Array
array
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.
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:
4. If it is empty then display Queue is empty. If queue is not empty then step 3.
#include <iostream>
using namespace std;
int cqueue[3];
int front = -1, rear = -1, n=5;
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:
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
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();
//main program
int main()
{
//Deque dq(5);
cout<<"Insert element 1 at rear end \n";
insertrear(1);
cout<< "rear element of deque " << " " <<getRear() <<endl;
deleterear();
cout<<"After deleterear, rear = " <<getRear() <<endl;
Output
Exercise:
Run the given program and write down the output and explain the steps.
Program
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