Table of Contents
A queue is a linear data structure that follows the FIFO rule of handling elements stored in it. It has a few basic operations such as ENQUEUE, DEQUEUE, and PEEK.
- Enqueue means inserting the data in the queue from the rear end.
- Dequeue means removing the data in the queue from the front end.
There are four types of queues -
- Simple Queue
- Circular Queue
- Double Ended Queue
- Priority Queue
Simple Queue
Allows the basic Enqueue and Dequeue operations using pointers to keep track of “rear” and “front”.
- Usually the initial values for “rear” and “front” are
rear=0; front=0. - Upon
Enqueue(Q,el)insert the element atrearindex and incrementrear. - Upon
Dequeue(Q)remove the element atfrontindex and incrementfront. - Queue is considered empty if
front >= rear.
A problem with the simple queue is that it wastes space as most implementations only reset rear and front when the queue is fully emptied. Even if it’s partially empty but rear == maxsize - 1, insertions won’t be allowed due to reliance on the pointers for information regarding “emptiness”.
Circular Queue
To counteract the wastage of space in the Simple Queue implementation, we can use a circular queue.
- Here we use similar Enqueue and Dequeue operations as the Simple Queue but update
rearandfrontby doingrear = (rear+1) % maxsizeandfront = (front+1) % maxsize. - The way to check if the queue is full becomes
front == (rear+1) % maxsize.
So now even if the queue is only partially empty, insertion of elements would still be allowed.
Double Ended Queue (Deque)
A double ended queue (deque) is an extension of the queue data structure that allows insertion and deletion at both ends.
- Elements can be inserted at both
frontandrear - Elements can be removed from both
frontandrear - Can be implemented using arrays (circular) or linked lists
Operations
insertFront(Q, el)– insert element at the frontinsertRear(Q, el)– insert element at the reardeleteFront(Q)– remove element from the frontdeleteRear(Q)– remove element from the rear
Pointer handling (array-based, circular)
front = (front - 1 + maxsize) % maxsize(insert at front)rear = (rear + 1) % maxsize(insert at rear)front = (front + 1) % maxsize(delete from front)rear = (rear - 1 + maxsize) % maxsize(delete from rear)
Conditions
- Empty:
front == −1(orfront == reardepending on convention) - Full:
(front == (rear+1) % maxsize)
Priority Queue
A special type of queue in which deletion of elements is based upon priority of element, not on the basis of arrival time. Implemented using a Heap.
Types -
- Max Priority Queue (high to low)
- Min Priority Queue (low to high)
Each element has two attributes <value, priority>.
Question 1 -
