QUEUE

Queue is an abstract, very similar arrangement of data to Stacks. Unlike stacks, both ends of a queue are open. In order to insert data (enqueue), one end is often used and the other is used to delete data (dequeue). The queue follows the First-In-First-Out approach , i.e. first access to the data item stored first.

Queue Representation

As we now know that in queue, for various purposes, we enter both ends. The following diagram below helps to describe queue representation as the structure of data

As in stacks, using Arrays, Linked-lists, Pointers and Structures, a queue can also be implemented. We will implement queues using a one-dimensional array for the sake of simplicity.

Basic Operations

Initializing or defining the queue, using it, and then fully erasing it from the memory can involve queue operations. We will try to explain the fundamental operations associated with queues here.−

enqueue() − add (store) an item to the queue.

dequeue() − remove (access) an item from the queue.

Enqueue Operation

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.

Algorithm

Dequeue Operation

 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