Queue
Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is open at both its ends. One end is always used to insert data (enqueue) and the other is used to remove data (dequeue). Queue follows First-In-First-Out methodology, i.e., the data item stored first will be accessed first.
A real-world example of queue can be a single-lane one-way road, where the vehicle enters first, exits first. More real-world examples can be seen as queues at the ticket windows and bus-stops.
Queue Representation
As we now understand that in queue, we access both ends for different reasons. The following diagram given below tries to explain queue representation as data structure −
As in stacks, a queue can also be implemented using Arrays, Linked-lists, Pointers and Structures. For the sake of simplicity, we shall implement queues using one-dimensional array.
Basic Operations
Queue operations may involve initializing or defining the queue, utilizing it, and then completely erasing it from the memory. Here we shall try to understand the basic operations associated with queues −
enqueue() − add (store) an item to the queue.
dequeue() − remove (access) an item from the queue.
Enqueue Operation
Queues maintain two data pointers, front and rear. Therefore, its operations are comparatively difficult to implement than that of stacks.
The following steps should be taken to enqueue (insert) data into a queue −
Step 1 − Check if the queue is full.
Step 2 − If the queue is full, produce overflow error and exit.
Step 3 − If the queue is not full, increment rear pointer to point the next empty space.
Step 4 − Add data element to the queue location, where the rear is pointing.
Step 5 − return success.
Sometimes, we also check to see if a queue is initialized or not, to handle any unforeseen situations.
Algorithm for enqueue operation
procedure enqueue(data) if queue is full return overflow endif rear ← rear + 1 queue[rear] ← data return true end procedure
Dequeue Operation
Accessing data from the queue is a process of two tasks − access the data where front is pointing and remove the data after access. The following steps are taken to perform dequeue operation −
Step 1 − Check if the queue is empty.
Step 2 − If the queue is empty, produce underflow error and exit.
Step 3 − If the queue is not empty, access the data where front is pointing.
Step 4 − Increment front pointer to point to the next available data element.
Step 5 − Return success.
Algorithm for dequeue operation
procedure dequeue if queue is empty return underflow end if data = queue[front] front ← front + 1 return true end procedure
Ex: Implement queue through array
#include <iostream>
using namespace std;
#define QSIZE 4
class qu {
int f;
int r;
int ary[QSIZE];
public:
qu(){
f = -1;
r = -1;
}
void insertion() {
if (f==-1){
f++;
r++;
}
else if (r == QSIZE-1){
cout << "Queue already Full" << endl;
return;
}
else {
r++;
}
cout << "Type the value to be inserted : " << endl;
cin >> ary[r];
cout << endl;
return;
}
void display() {
if (f==-1){
cout << " queue is empty!!" << endl;
}
else {
for (int i= f; i<= r; i++) {
cout << ary[i] << " ";
}
cout << endl;
}
return;
}
void deletion() {
int temp;
if (f==-1) {
cout << "queue is already empty!!" << endl;
}
else {
cout << " Deleting from front value = " << ary[f] << endl;
for (int i=f; i<=r-1; i++){
ary[i] = ary[i+1];
}
r = r-1;
}
return;
}
};
int main(){
cout << "Ready to create queue" << endl;
qu q1;
int choice=0;
while (choice != 4) {
cout << "Enter choice : \n ";
cout << " 1. Insert 2.Delete 3.Display 4. Exit \n";
cin >> choice;
switch (choice) {
case 1: q1.insertion(); break;
case 2: q1.deletion(); break;
case 3: q1.display(); break;
case 4: exit(-1);
}
}
return 0;
}