Queue - Note
Queue - Note
Queue is also an abstract data type or a linear data structure, just like stack
data structure, in which the first element is inserted from one end called
the REAR(also called tail), and the removal of existing element takes place
from the other end called as FRONT(also called head).
This makes queue as FIFO(First in First Out) data structure, which means
that element inserted first will be removed first.
Which is exactly how queue system works in real world. If you go to a ticket
counter to buy movie tickets, and are first in the queue, then you will be the
first one to get the tickets. Right? Same is the case with Queue data
structure. Data inserted first, will leave the queue first.
The process to add an element into queue is called Enqueue/insert and the
process of removal of an element from queue is called Dequeueue/delete.
2.2Applications of Queue
Queue, as the name suggests is used whenever we need to manage any
group of objects in an order in which the first one coming in, also gets out
first while the others wait for their turn, like in the following scenarios:
Algorithm of enqueue:
Step 1: IF REAR = MAX-1 Write OVERFLOW
Goto step 4
[END OF IF]
Step 2: IF FRONT = -1 and REAR = -1
SET FRONT = REAR = 0
ELSE
SET REAR = REAR + 1
[END OF IF]
Step 3: SET QUEUE[REAR] = NUM
Step 4: EXIT
Algorithm of dequeue:
Step 1: IF FRONT = -1 OR FRONT > REAR
Write UNDERFLOW
ELSE
SET FRONT = FRONT + 1
[END OF IF]
Step 2: EXIT
Algorithm of Count:
Step 1: write rear-front+1
Step2: Exit
Algorithm of peek :
Step1 : write queue[front]
Step 2: Exit
Algorithm of isfull:
Step1: IF REAR = MAX-1 return True else return false
Step2: Exit
Algorithm of isempty:
Step 1: If FRONT = -1 OR FRONT > REAR return True else return false
Step2: Exit
Algorithm Display:
Step 1: IF FRONT = -1 OR FRONT > REAR
Write UNDERFLOW
Step 2: Set i=front
Step 3:while i<=rear
Step3.1: Write queue[i]
Step 3.2: set i=i+1
Enqueue/insertion: O(1)
Dequeueue/deletion: O(1)
Display:O(n)
Search:O(n)
Size: O(1)
Circular Queue is also a linear data structure, which follows the principle
of FIFO(First In First Out), but instead of ending the queue at the last
position, it again starts from the first position after the last, hence making
the queue behave like a circular data structure.
1. In case of a circular queue, head pointer will always point to the front of
the queue, and tailpointer will always point to the end of the queue.
2. Initially, the head and the tail pointers will be pointing to the same
location, this would mean that the queue is empty.
3. New data is always added to the location pointed by the tail pointer, and
once the data is added, tail pointer is incremented to point to the next
available location.
4. In a circular queue, data is not actually removed from the queue. Only
the head pointer is incremented by one position when dequeueue is
executed. As the queue data is only the data between head and tail,
hence the data left outside is not a part of the queue anymore, hence
removed.
5. The head and the tail pointer will get reinitialised to 0 every time they
reach the end of the queue.
6. Also, the head and the tail pointers can cross each other. In other
words, head pointer can be greater than the tail. Sounds odd? This will
happen when we dequeueue the queue a couple of times and
the tail pointer gets reinitialised upon reaching the end of the queue.
Overflow condition
If the MAX_SIZE is the size of the array used in the implementation of
circular queue. Then the overflow condition is
front=(rear+1)%MAX_SIZE
or
front=rear+1
Algorithm of Insertion :
Step 4: EXIT
Step 4: EXIT
void display() {
int i;
if (isEmpty())
else {
Step 4: EXIT
Step 4: EXIT
Algorithm delfromleft()
6.2Priority Queues
A priority queue is a data structure in which each element is assigned a
priority. The priority of the element will be used to determine the order in
which the elements will be processed. The general rules of processing the
elements of a priority queue are
An element with higher priority is processed before an element with a
lower priority.
Two elements with the same priority are processed on a first-come-
first-served (FCFS) basis.
A priority queue can be thought of as a modified queue in which when an
element has to be removed from the queue, the one with the highest-
priority is retrieved first. The priority of the element can be set based on
various factors. Priority queues are widely used in operating systems to
execute the highest priority process first.
Array Representation of a Priority Queue
When arrays are used to implement a priority queue, then a separate queue
for each priority number is maintained. Each of these queues will be
implemented using circular arrays or circular queues. Every individual queue
will have its own FRONT and REAR pointers. We use a two-dimensional array
for this purpose where each queue will be allocated the same amount of
space. Look at the two-dimensional representation of a priority queue given
below.
Given the front and rear values of each queue, the two-dimensional matrix
can be formed as shown in Figure .
FRONT[K] and REAR[K] contain the front and rear values of row K, where K
is the priority number. Note that here we are assuming that the row and
column indices start from 1, not 0. Obviously, while programming, we will
not take such assumptions.
Insertion To insert a new element with priority K in the priority queue, add
the element at the rear end of row K, where K is the row number as well as
the priority number of that element. For example, if we have to insert an
element R with priority number 3, then the priority queue will be given as
shown in Figure
Deletion To delete an element, we find the first nonempty queue and then
process the front element of the first non-empty queue. In our priority
queue, the first non-empty queue is the one with priority number 1 and the
front element is A, so A will be deleted and processed first. In technical
terms, find the element with the smallest K, such that FRONT[K] != NULL.
0 1 0 1 2 3 4
0 0
1 1
2 2
3 3
4 4
5 5
6 6