13. Data Structure and Algorithms - Queue
13. Data Structure and Algorithms - Queue
Algorithms - Queue
We are familiar with queue in our day to day life as we wait for a service.
The queue data structure also means the same where the data elements
are arranged in a queue. The uniqueness of queue lies in the way items
are added and removed. The items are allowed at on end but removed
form the other end. So it is a First-in-First out method. An queue can be
implemented using python list where we can use the insert() and pop()
methods to add and remove elements. Their is no insertion as data
elements are always added at the end of the queue.
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.
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.
Enqueue Operation (Add)
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 −