Unit 3 Queues
Unit 3 Queues
Introduction:
A queue is a data structure used for storing data. In queue, the order in which the data arrives is
important. In general, a queue is a line of people or things waiting to be served in sequential order
starting at the beginning of the line or sequence.
Definition : A queue is an ordered list in which insertions are done at one end (rear) and deletions are
done at other end (front). The first element to be inserted is the first one to be deleted. Hence, it is
called as First In First Out(FIFO) list.
When an element is inserted in a queue, the concept is called as EnQueue, and when an element
is removed from the queue, the concept is called as DeQueue. Trying to DeQueue an empty queue is
called as underflow and trying to EnQueue an element in a full queue is called as overflow. As an
example, consider the snapshot of the
10 20 3 40 50 60 70
0
front rear
Queue ADT:
The definition of an abstract data type clearly states that for a data structure to be abstract, it
should have the following two characteristics
1. There should be a particular way in which components are related to each other and
2. A statement of the operations that can be performed on elements of the abstract data type
should be specified.
Thus, a queue, as an abstract data type, can be defined as follows:
A queue of elements of type A is a finite sequence of elements of A together with the following
operations :
1. Initialize a queue to be empty.
2. Check if a queue is empty or not
3. Check if a queue is full or not.
4. Insert a new element after the last element in a queue, if it is not full.
5. Retrieve the first element of a queue, if it is not empty.
6. Delete the first element in a queue, if it is not empty.
Applications of Queue:
1. Queue of processes waiting to be processed
2. Operating systems schedule jobs (with equal priority) in the order of arrival(e.g., a print queue).
3. Simulation of real-world queues such as lines at a ticket counter or any other first-come first-
served scenario requires a queue.
4. Multiprogramming.
5. Asynchronous data transfer.
6. Waiting times of customers at call center.
7. Determining number of cashiers to have at a supermarket.
8. Round-robin scheduling : Iterate through a set of processes in a circular manner and service
each element.
Limitations of Queues:
1. The main disadvantages is that queue is not readily searchable and adding or removing elements
from the middle of the queue is very complex.
2. It is not meant to allow elements to be removed from the middle.
3. A queue is simply like a line. first in line, first to get service
1. Traffic light functioning is the best example for circular Queues. The colours in the traffic light
follow a circular pattern.
2. In page replacement algorithms, a circular list of pages is maintained and when a page needs to
be replaced, the page in the front of the queue will be chosen. 6. Define Dequeue ?
Dequeue :
1. A dequeue (double ended queue) is a linear list in which insertion and deletion are performed
from the either end of the structure.
2. There are two variations of Dqueue
i. Input restricted dqueue-allows insertion at only one end
ii. Output restricted dqueue-allows deletion from only one end
3. Such a structure can be represented by following fig.
Priority Queue :
A priority queue is a collection of elements such that each element has been assigned a priority and
such that the order in which elements are deleted and processed comes from the following rules :
1. An element of higher priority is processed before any element of lower priority.
2. Two elements with same priority are processed according to the order in which they were added
to the queue.
An example of priority queue can be a time sharing system that is programs of high priority are
processed first and programs with the same priority form a standard queue
Each node in the list contains three items of information – an information field(INFO), a priority
number(PRNO) and the link (NEXT)
Node A will precede Node B in the list when A has higher priority than B or when both the nodes
have same priority but A was added to the list before B. This means that the order in one-way list
corresponds to the order of priority queue
Operations (or) Implementation of Queue using Arrays:
ARRAY REPRESENATION OF QUEUES :
Queues can be easily represented using linear arrays. As stated earlier, every queue has front
and rear variables that point to the position from where deletions and insertions can be done
respectively. Figure below shows the representation of queue as an
1. Insert or enqueue
2. Delete or dequeue
3. Peek
1. Insert or enqueue operation :
Queues maintain two data variables front and rear. Therefore, its operators are comparatively
difficult to implement than that of stacks.
Before inserting an element in the queue, we must check for overflow conditions. An overflow
will occur when we try to insert an element into a queue that is already full. When Rear MAX - 1,
where MAX is the size of the queue, we have an overflow condition. Note that we have written MAX -
1, because the index starts from 0.
fron rear
t
12 9 7 18 14 36
0 1 2 3 4 5 6 7 8 9
Queue
Here, front =0 and rear =5. Suppose we want to add another element with the value 45, then the
rear would be incremented by 1 and the value would be stored at the position pointed by the rear. The
queue after addition would be as shown in figure.
fron rear
t
12 9 7 18 14 36 45
0 1 2 3 4 5 6 7 8 9
Queue after insertion of a new element
Here,front 0 and rear = 6. Every time a new element has to be added, we repeat the same
procedure.
Example : If we want to delete an element from the queue, then the value of the front will be
incremented. Deletions are done from only this end of the queue after deletion will be as shown in
figure.
front rear
9 7 18 14 36 45
0 1 2 3 4 5 6 7 8 9
Queue after deletion of an element
Here, front =1 and rear = 6
3. Peek Operation :
Peek is an operation that return the data at the front of the queue. The algorithm for peek
operation :
To insert an element with the value 50, we first check if FRONT=NULL. If the condition holds,
then the queue is empty. So, we allocate memory for a new node, store the value in its data part and
NULL in its next part. If FRONT !=NULL, then we will insert the new node at the end of the linked
queue. Thus, the updated queue becomes as shown in figure.
Delete operation :
The delete operation is used to delete the element that was first inserted in a queue. That is, the
delete operation deletes the element whose address is stored in the FRONT. However before deleting
the value, we must first check it FRONT=NULL, If the condition is true, then the queue is empty and
no more deletions can be done. If an attempt is made to delete a value from a queue that is already
empty, an UNDERFLOW message is printed.
If the condition is false, then we delete the first node pointed by FRONT. The FRONT will now
refers to the second element of the linked queue. Thus, the updated queue becomes as shown in figure.
Circular queues are the queues implemented in circular form rather than in a straight line.
Circular queues overcome the problem of unutilised space in linear queue implemented as an array. In
the array implementation there is a possibility that the queue is reported full even through slots of the
queue are empty (since rear has reached the end of array).
Suppose an array Q of n elements is used to implement a circular queue. If we go on adding
elements to the queue we may reach Q[n-1]. We cannot add any more elements to the queue since the
end of the array has been reached.
Instead of reporting the queue is full. If some elements in the queue have been deleted then
there might be empty slots at the beginning of the queue. In such case these slots would be filled by
new elements added to the queue. A circular queue is implemented in the same manner as a linear
queue is implemented. The only difference will be in the code that performs insertion and deletion
operations.
Insertion Operation :
For insertion, we now have to check for the following three conditions:
1.If rear !=MAX-1, then the value will be inserted and rear will be incremented illustrated in figure.
2.If front = 0 and rear = MAX-1, then print that the circular queue is full. Look at the queue given
in figure. Which illustrates this point.
3. If front !=0 and rear = MAX-1, then it means that the queue is not full. So, set 0 and insert the new
element there, as shown in figure
2. Deletion Operation :
To delete an element again we check for three conditions.
i. Look at figure. If front = -1, then there are no elements in the queue. So, an underflow condition will
be reported.
0 1 2 3 4 5 6 7 8 9
Front=rear=-1
Empty Queue
ii. If the queue is not empty and after returning the value on the front, front=rear, then the queue has
now become empty and so, front and rear are set to -1. This is illustrated in figure.
81
0 1 2 3 4 5 6 7 8 9
Front=rear=9
Delete last element and set rear=front=-1
iii.If the queue is not empty and after returning the value on the front, front=MAX-1, then front is set
to 0. This is shown in figure.
7 63 9 1 27 39 81
2 8
0 1 2 3 4 Rear=5 6 7 8 Front=9
Program:
#include <stdio.h>
# define max 6
int queue[max]; // array declaration
int front=-1;
int rear=-1;
// function to insert an element in a circular queue
void enqueue(int element)
{
if(front==-1 && rear==-1) // condition to check queue is empty
{
front=0;
rear=0;
queue[rear]=element;
}
else if((rear+1)%max==front) // condition to check queue is full
{
printf("Queue is overflow..");
}
else
{
rear=(rear+1)%max; // rear is incremented
queue[rear]=element; // assigning a value to the queue at the rear position.
}
}
switch(choice)
{
case 1:
printf("Enter the element which is to be inserted");
scanf("%d", &x);
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();
}}
return 0;
}
Properties of deque:
Deque can be used both as stack and queue as it allows the insertion and deletion operations on
both ends.
In deque, the insertion and deletion operation can be performed from one side. The stack
follows the LIFO rule in which both the insertion and deletion can be performed only from one end;
therefore, we conclude that deque can be considered as a stack.
In deque, the insertion can be performed on one end, and the deletion can be done on another
end. The queue follows the FIFO rule in which the element is inserted on one end and deleted from
another end. Therefore, we conclude that the deque can also be considered as the queue.
There are two types of Queues, Input-restricted queue, and output-restricted queue.
Input-restricted queue: The input-restricted queue means that some restrictions are applied to the
insertion. In input-restricted queue, the insertion is applied to one end while the deletion is applied
from both the ends.
Output-restricted queue: The output-restricted queue means that some restrictions are applied to the
deletion operation. In an output-restricted queue, the deletion can be applied only from one end,
whereas the insertion is possible from both ends.
Operations on Deque:
The following are the operations applied on deque:
1. Insert at front
2. Delete from end
3. insert at rear
4. delete from rear
Other than insertion and deletion, we can also perform peek operation in deque. Through peek
operation, we can get the front and the rear element of the dequeue. We can perform two more
operations on dequeue
isFull(): This function returns a true value if the stack is full; otherwise, it returns a false value.
isEmpty(): This function returns a true value if the stack is empty; otherwise it returns a false value.