Queues
Queues
• MODULE 3
• QUEUES
1
The concept of queues using the analogies given below.
▪ People waiting for a bus. The first person standing in the line will be the first one to
get into the bus.
▪ People standing outside the ticketing window of a cinema hall. The first person in
the line will get the ticket first and thus will be the first one to move out of it.
▪ Luggage kept on conveyor belts. The bag which was placed first will be the first to
come out at the other end.
▪ Cars lined at a toll bridge. The first car to reach the bridge will be the first to leave.
2
➢ Queue is a data structure.
➢ Queue is open at both its ends.
➢ One end is always used to insert data (enqueue) (Rear) (tail of the
queue)
➢ The other is used to remove data (dequeue) (Front) (head of the queue)
➢ Queue follows First-In-First-Out methodology, i.e., the data item stored
first will be accessed first.
▪A queue is a FIFO (First-In, First-Out) data structure in which the
element that is inserted first is the first one to be taken out.
▪ Theelements in a queue are added at one end called the REAR and
removed from the other end called the FRONT
4
5
QUEUES
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() − The enqueue operation is used to insert the element at the rear end
of the queue.
▪ dequeue() − The dequeue operation performs the deletion from the front-end of
the queue. It also returns the element which has been removed from the front-
end. It returns an integer value.
Few more functions are required to make the above-mentioned queue operation efficient. These are
−
▪ peek() − This is the third operation that returns the element, which is pointed by
the front pointer in the queue but does not delete it.
▪ isfull() − When the Queue is completely full, then it shows the overflow
condition.
▪ isempty() − When the Queue is empty, i.e., no elements are in the Queue then it
throws the underflow condition.
➢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.
7
▪ In Fig. 8.1, FRONT = 0 and REAR = 5.
▪ Suppose we want to add another element with value 45, then
REAR would be incremented by 1 and the value would be stored
at the position pointed by REAR.
▪ The queue after addition would be as shown in Fig. 8.2. Here,
FRONT = 0 and REAR = 6.
▪ Every time a new element has to be added, we repeat the same
procedure.
▪ If we want to delete an element from the queue, then the value of
FRONT will be incremented.
▪ Deletions are done from only this end of the queue.
▪ The queue after deletion will be as shown in Fig. 8.3. Here,
FRONT = 1 and REAR = 6.
8
▪ Queue empty is denoted by,
▪ FRONT=REAR=-1
▪ The process of inserting an element in the queue is called enqueue, and the
process of deleting an element from the queue is called dequeue.
9
N=5
If Queue is empty, then FRONT = REAR
10
11
12
QINSERT ( QUEUE, N, FRONT, REAR, ITEM)
This procedure inserts an element ITEM into a queue.
1. [Queue already filled?]
If FRONT = 0 and REAR = N-1, or if FRONT = REAR + 1, THEN:
Write: OVERFLOW, and Return.
2. [Find new value of REAR.]
If FRONT = -1, then: [Queue initially empty.]
Set FRONT = 0 and REAR = 0.
Else if REAR = N-1, then
Set REAR = 1
Else:
Set REAR = REAR + 1.
[End of If Structure.]
13
void insert()
{
int num;
printf(“\n Enter the number to be inserted in the queue : “);
scanf(“%d”, &num);
if(rear == MAX-1)
printf(“\n OVERFLOW”);
else if(front == -1 && rear == -1)
front = rear = 0;
else
rear++;
queue[rear] = num;
}
14
QDELETE ( QUEUE, N, FRONT, REAR, ITEM)
This procedure deletes an element from a queue and assigns it to the variable
ITEM.
1. [Queue already empty?]
If FRONT = -1, then Write: UNDERFLOW, and Return.
2. Set ITEM = QUEUE[FRONT].
3. Return.
15
int delete_element()
{
int val;
if(front == -1 || front>rear)
{
printf(“\n UNDERFLOW”);
return -1;
}
else
{
val = queue[front];
if(f= =r)
F=R=-1;
front++;
if(front > rear)
front = rear = -1;
return val;
}
}
16
A queue data structure can be classified into the following types:
1. Priority Queue
2. Circular Queue
3. Deque
17
Priority Queue
✓ Introduction to Priority Queue
✓ Representation of Queue
✓ Different Implementation Strategies for Priority Queue
✓ Types of Priority Queue
✓ Operations of Priority Queue
✓ Coding Implementation of Priority Queue
✓ Applications of Priority Queue
What are the Characteristics of a Priority Queue?
A queue is termed as a priority queue if it has the following characteristics:
➢ Each item has some priority associated with it.
➢ An item with the highest priority is moved at the front and deleted first.
➢ If two elements share the same priority value, then the priority queue follows the
first-in-first-out principle for de queue operation.
Implementation of Queue
There are two ways of implementing the Queue:
• Sequentialallocation: The sequential allocation in a Queue can be
implemented using an array.
Suppose we want to insert a new element in the queue shown in Fig. 8.14. Even though
there is space available, the overflow condition still exists because the condition rear = MAX
– 1 still holds true.
This is a major drawback of a linear queue.
34
35
36
37
Enqueue Operation Dequeue Operation
➢ check if the queue is full ➢ check if the queue is empty
➢ for the first element, set value ➢ return the value pointed by FRONT
of FRONT to 0 ➢ circularly increase
➢ circularly increase the REAR index the FRONT index by 1
by 1 (i.e. if the rear reaches the end, ➢ for the last element, reset the
next it would be at the start of the values of FRONT and REAR to -1
queue)
➢ add the new element in the
position pointed to by REAR
#include <stdio.h>
#define SIZE 5
int items[SIZE];
int front = -1, rear = -1;
if (isEmpty()) {
{ int i;
} else { else
element = items[front]; {
rear = -1; {
else }
} }
return (element);
}
}
int main() {
// Fails because front = -1
deQueue();
enQueue(1);
enQueue(2);
enQueue(3);
enQueue(4);
enQueue(5);
// Fails to enqueue because front == 0 && rear == SIZE - 1
enQueue(6);
display();
deQueue();
display();
enQueue(7);
display();
// Fails to enqueue because front == rear + 1
enQueue(8);
Double Ended Queue
Deque or Double Ended Queue is a type of queue in which insertion and removal of
elements can either be performed from the front or the rear. Thus, it does not follow FIFO
rule (First In First Out). It can be used as stack (LIFO) or queue (FIFO).
Types/Variations of Deque
➢ Input Restricted Deque
In this deque, input is
restricted at a single
end but allows deletion
at both the ends.
✓ Palindrome
N-1
3. Insertion from front void enqueuefront(int x)
{
if(f==0 && r ==n-1) || (f =r+1 ))
▪ This operation adds an element at {
the front. printf(“Queue is full”);
}
1.Check the position of front. elseif(f==-1 && r==-1)
{
2.If front < 1, reinitialize f=r=0;
deque[f]=x;
front = n-1 (last index). }
1.Else, decrease front by 1. elseif(f==0)
{
2.Add the new key 5 into array[front]. f=n-1;
deque[f]=x;
}
else
{
f--;
deque[f]=x;
}
}
4. Delete from Rear
4. Delete from Rear void dequeuerear()
{
▪ This operation deletes an element if(f==-1 && r==-1)
from the rear. {
printf(“Queue is empty”);
1.Check if the deque is empty. }
elseif(f==r)
2.If the deque is empty (i.e. front = -1),
{
deletion cannot be performed printf(“%d”, deque[r]);
(underflow condition). f=r=-1;
3.If the deque has only one element }
(i.e. front = rear), set front = -1 and elseif(r==o)
rear = -1, else follow the steps below. {
printf(“%d”, deque[r]);
4.If rear is at the front (i.e. rear = 0), set r=n-1;
go to the front rear = n - 1. }
else
5.Else, rear = rear - 1. {
printf(“%d”, deque[r]);
r=r-1;
}
#define N 5
Enqueue operation enqueuefront(2);
int deque[N];
void enqueuerear(int x) enqueuefront(5);
int f=-1, r=-1;
{ enqueuerear(-1);
void enqueuefront(int x)
if(f==0 && r ==n-1) || (f =r+1 )) enqueuerear(0);
{ if(f==0 && r ==n-1) || (f =r+1 ))
printf(“Queue is full”); enqueuefront(7);
printf(“Queue is full”);
elseif(f==-1 && r==-1) enqueuefront(4);
elseif(f==-1 && r==-1) {
{ f=r=0; display();
f=r=0; deque[r]=x;
deque[f]=x; }
void display()
} elseif(r==n-1)
{
{
elseif(f==0) int i=f;
r=0;
{ while(i!=r)
deque[r]=x
f=n-1 {
}
deque[f]=x; printf(“%d”, deque[i]);
else
i=(i+1)%n;
} {
}
else r++;
printf(“%d”, deque[rear]);
{ dequeue[r]=x
}
f--; }
deque[f]=x; }}
void getfront()
{ void getrear()
{
if(f==0 && r ==n-1) || (f =r+1 )) if(f==0 && r ==n-1) || (f =r+1 ))
printf(“Queue is full”); printf(“Queue is full”);
}
}
printf(“%d”, deque[r];
printf(“%d”, deque[f];
#define N 5
Dequeue operation dequeuefront();
int deque[N];
dequeuerear();
int f=-1, r=-1;
void dequeuerear() dequeuefront();
void dequeuefront()
{
{
if(f==-1 && r==-1)
if(f==-1 && r==-1) {
{ printf(“Queue is empty”);
printf(“Queue is empty”); }
} elseif(f==r)
{
elseif(f==r)
printf(“%d”, deque[r]);
{ f=r=-1;
printf(“%d”, deque[f]); }
f=r=-1; elseif(r==o)
} {
elseif(f==n-1)
printf(“%d”, deque[r]);
r=n-1;
{
}
printf(“%d”, deque[f]); else
f=0;
else {
} { printf(“%d”, deque[r]);
printf(“%d”, deque[f]) r=r-1;
f=f+1; } }
Queues are widely used as waiting lists for a single shared resource like printer, disk, CPU.
▪ Queues are used to transfer data asynchronously (data not necessarily received at same rate
as sent) between two processes (IO buffers), e.g., pipes, file IO, sockets.
▪ Queues are used as buffers on MP3 players and portable CD players, iPod playlist.
▪ Queues are used in Playlist for jukebox to add songs to the end, play from the front of the
list.
▪ Queues are used in operating system for handling interrupts.
▪ When programming a real-time system that can be interrupted, for example, by a mouse
click, it is necessary to process the interrupts immediately, before proceeding with the current
job.
▪ If the interrupts have to be handled in the order of arrival, then a FIFO queue is the
appropriate data structure
62
1. Define Queue. Write QINSERT and QDELETE procedures for queues using arrays.
2. Implement queue data structure using C program, showing all the primitive operations
that can be performed on queue.
3. Explain priority queue with an example.
4. Develop a C program to implement a priority queue using array.
5. Explain Circular Queue with an example. Explain how it is advantageous over Linear
queue.
6. Write an algorithm to insert and delete an item from circular queue.
7. Explain Dequeue. How are insertion and deletion performed in dequeue? Explain with
suitable example.
8. Develop a C program to implement insertion and deletion from both the ends in a
dequeue.
9. Write a C program to insert and delete an item from a Circular Queue.
10. What is priority queue? Explain different types of priority queue with example. 63
64