Lab #3: Queue
Section A: Tutorial - Demonstrated by Instructor
Queue Representation
In queue, we access both ends for different reasons. The following diagram given below
tries to explain queue representation as data structure −
Fig-1
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.
Few more functions are required to make the above-mentioned queue operation efficient.
These are −
• peek() − Gets the element at the front of the queue without removing it.
• isFull() − Checks if the queue is full.
• isEmpty() − Checks if the queue is empty.
In queue, we always dequeue (or access) data, pointed by front pointer and while enqueing
(or storing) data in the queue we take help of rear pointer.
Let's first learn about supportive functions of a queue −
peek()
This function helps to see the data at the front of the queue. The algorithm of peek()
function is as follows −
Algorithm:
begin procedure peek
return queue[front]
end procedure
Implementation of peek() function in C programming language –
int peek()
{
return queue[front];
}
isFull() :
As we are using single dimension array to implement queue, we just check for the rear
pointer to reach at MAXSIZE to determine that the queue is full. Algorithm of isFull()
function −
Algorithm:
begin procedure isFull
if rear equals to MAXSIZE
return true
else
return false
endif
end procedure
Implementation of iFull() function in C programming language − bool
isFull()
{
if(rear == MAXSIZE - 1)
return true;
else
return false;
}
isEmpty()
Algorithm of isEmpty() function –
Algorithm:
begin procedure isEmpty
if front is less than MIN OR front is greater than rear
return true
else
return false
endif
end procedure
If the value of front is less than MIN or 0, it tells that the queue is not yet initialized, hence
empty.
Here's the C programming code –
bool isEmpty()
{
if(front < 0 || front > rear)
return true;
else
return false;
}
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.
Fig-2
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
Implementation of enqueue() in C programming language −
int enqueue(int data)
if(isFull())
return 0;
rear = rear + 1;
queue[rear] = data;
return 1;
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.
Fig-3
Algorithm for dequeue operation
procedure dequeue
if queue is empty return
underflow
end if
data= queue[front] front ←
front + 1 return true
end procedure
Implementation of dequeue() in C programming language −
int dequeue()
{
if(isEmpty()) return 0;
int data = queue[front]; front = front +
1;
return data;
}
Section B: Lab Experiments - Demonstrated by students
(Please Code yourself and show the output to instructor)
Experiment 1:
Write an algorithm and a menu driven C program that demonstrate the basic operations of Queue.
#include <iostream>
class Queue {
private:
int *arr;
int front;
int rear;
int size;
int capacity;
public:
// Constructor
Queue(int capacity) {
this->capacity = capacity;
this->front = 0;
this->rear = 0;
this->size = 0;
this->arr = new int[capacity];
}
// Destructor
~Queue() {
delete[] arr;
}
// Enquirer an element
void enqueue(int x) {
if (size == capacity) {
std::cout << "Queue is full" << std::endl;
return;
}
arr[rear] = x;
rear = (rear + 1) % capacity;
size++;
}
// Dequeue an element
int dequeue() {
if (size == 0) {
std::cout << "Queue is empty" << std::endl;
return -1;
}
int temp = arr[front];
front = (front + 1) % capacity;
size--;
return temp;
}
// Get the front element
int getFront() {
if (size == 0) {
std::cout << "Queue is empty" << std::endl;
return -1;
}
return arr[front];
}
// Get the rear element
int getRear() {
if (size == 0) {
std::cout << "Queue is empty" << std::endl;
return -1;
}
return arr[(rear - 1 + capacity) % capacity];
}
// Check if the queue is empty
bool isEmpty() {
return size == 0;
}
// Check if the queue is full
bool isFull() {
return size == capacity;
}
};
int main() {
Queue q(5);
[Link](1);
[Link](2);
[Link](3);
std::cout << "Front element: " << [Link]() << std::endl; // Output: 1
std::cout << "Rear element: " << [Link]() << std::endl; // Output: 3
std::cout << "Dequeued element: " << [Link]() << std::endl; // Output: 1
std::cout << "Front element: " << [Link]() << std::endl; // Output: 2
return 0;
}