0% found this document useful (0 votes)
9 views

Queue

The document provides an overview of queues as a FIFO data structure, detailing their operations, representations, and types, including circular queues and priority queues. It explains the insertion and deletion processes, comparisons with stacks, and various applications of queues in computing. Additionally, it discusses deques and multiple queues, highlighting their functionalities and use cases.

Uploaded by

HaaniMoon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Queue

The document provides an overview of queues as a FIFO data structure, detailing their operations, representations, and types, including circular queues and priority queues. It explains the insertion and deletion processes, comparisons with stacks, and various applications of queues in computing. Additionally, it discusses deques and multiple queues, highlighting their functionalities and use cases.

Uploaded by

HaaniMoon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

QUEUES

Introduction
• Queue is an important data structure which stores its elements in an
ordered manner.

• We can explain the concept of queues using the following analogy:


People moving on an escalator. The people who got on the
escalator first will be the first one to step out of it.

• 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.

• The elements in a queue are added at one end called the rear and
removed from the other one end called the front.
Array Representation of Queues

• Queues can be easily represented using linear arrays.


• Every queue has front and rear variables that point to the position from
where deletions and insertions can be done, respectively.
• Consider the queue shown in figure

• Here, front = 0 and rear = 5.


• If we want to add one more value in the list say with value 45, then rear
would be incremented by 1 and the value would be stored at the
position pointed by rear.
Array Representation of Queues

• Now, front = 0 and rear = 6. Every time a new element has to be added,
we will repeat the same procedure.

• Now, 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.

• Now, front = 1 and rear = 6.


Array Representation of Queues

• Before inserting an element in the queue we must check for


overflow conditions.

• An overflow occurs when we try to insert an element into a queue


that is already full, i.e. when rear = MAX – 1, where MAX specifies
the maximum number of elements that the queue can hold.

• Similarly, before deleting an element from the queue, we must


check for underflow condition.

• An underflow occurs when we try to delete an element from a queue


that is already empty. If front = -1 and rear = -1, this means there is
no element in the queue.
Algorithm for Insertion Operation
Algorithm for Deletion Operation
Stack v/s Queue
Stack Queue
A Linear List Which allows insertion or deletion of an A Linear List Which allows insertion and one end and
element at one end only is called as Stack deletion at another end is called as Queue

Since insertion and deletion of an element are Since insertion and deletion of an element are
performed at one end of the stack, the elements can performed at opposite end of the queue, the elements
only be removed in the opposite order of insertion. can only be removed in the same order of insertion

Stack is called as Last In First Out (LIFO) List. Queue is called as First In First Out (FIFO) List.

The most accessible element is called as TOP of the Insertion of element is performed at FRONT end and
stack and insertion and deletion is performed at this deletion is performed from REAR end
end only.

Example of stack is arranging plates in one above Example is ordinary queue is supermarket billing
one. queue.

Insertion operation is referred as PUSH and deletion Insertion operation is referred as ENQUEUE and
operation is referred as POP deletion operation is referred as DQUEUE

Function calling in any languages uses Stack Task Scheduling by Operating System uses queue
Circular Queues

• We will explain the concept of circular queues using an example.

• In this queue, front = 2 and rear = 9.

• Now, if you want to insert a new element, it cannot be done because the space is available only at the left of the queue.

• If rear = MAX – 1, then OVERFLOW condition exists.

• This is the major drawback of a linear queue. Even if space is available, no insertions can be done once rear is equal to
MAX – 1.

• This leads to wastage of space. In order to overcome this problem, we use circular queues.

• In a circular queue, the first index comes right after the last index.
Inserting an Element in a Circular Queue

• For insertion we check for three conditions which are as follows:


• If front=0 and rear= MAX – 1, then the circular queue is full.

• If rear != MAX – 1, then the rear will be incremented and value will be
inserted

• If front!=0 and rear=MAX -1, then it means that the queue is not full. So, set
rear = 0 and insert the new element.
Algorithm to Insert an Element in a Circular Queue
Deleting an Element from a Circular Queue

• To delete an element again we will check for three conditions:


• If front = -1, then it means there are no elements in the queue. So an underflow condition
will be reported.

• If the queue is not empty and after returning the value on front, if front = rear, then it
means now the queue has become empty and so front and rear are set to -1.

• If the queue is not empty and after returning the value on front, if front = MAX -1, then
front is set to 0.
Algorithm to Delete an Element from a Circular Queue
Circular Queue (Example)

• Perform following operations in a circular queue of length


4 and give the Front, Rear and Size of the queue after
each operation.

Insert A Insert B Delete Insert C

Insert D Insert E Insert F Delete

Delete Insert G Insert H Insert I

Delete Delete Delete Delete


Circular Queue (Example)

Consider the following queue, where queue is a circular


queue having 6 memory cells. Front=2, Rear=4, Queue: _,
A, C, D, _, _

Describe queue as following operation take place:


– F is added to the queue
– Two letters are deleted
– R is added to the queue
– S is added to the queue
– One letter is deleted
Deques
• A deque is a list in which elements can be inserted or deleted at either end.

• It is also known as a head-tail linked list because elements can be added to or removed from the front (head) or
back (tail).

• A deque can be implemented either using a circular array or a circular doubly linked list.

• In a deque, two pointers are maintained, LEFT or FRONT and RIGHT or REAR which point to either end of the
deque.

• The elements in a deque stretch from LEFT end to the RIGHT and since it is circular, Deque[N-1] is followed by
Deque[0].

• Use: Palindrome-Checker
Deques

• There are two variants of a double-ended queue:


 Input restricted deque: In this dequeue insertions can be done only at one of the ends
while deletions can be done from both the ends.

 Output restricted deque: In this dequeue deletions can be done only at one of the ends
while insertions can be done on both the ends.
Priority Queues

• A priority queue is a queue in which each element is assigned a priority.


• The priority of elements is used to determine the order in which these
elements will be processed.
• The general rule of processing elements of a priority queue can be given as:
 An element with higher priority is processed before an element with lower
priority
 Two elements with same priority are processed on a first come first served
(FCFS) basis
• Priority queues are widely used in operating systems to execute the highest
priority process first.
• In computer’s memory priority queues can be represented using arrays or
linked lists.
Types of Priority Queues

• There are two types of priority queues:

• Ascending Priority Queue: Elements can be inserted in any order but only smallest element
can be removed.

• Descending Priority Queue: Elements can be inserted in any order but only largest element
can be removed.
Array Representation of Priority Queues

• 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 can use a two-dimensional array for this purpose where each queue will be allocated
same amount of space.

• Given the front and rear values of each queue, a two dimensional matrix can be formed.
Multiple Queues
• When implementing a queue using an array, the size of the array must be
known in advance.
• If the queue is allocated less space, then frequent OVERFLOW conditions will
be encountered.
• To deal with this problem, the code will have to be modified to reallocate more
space for the array, but this results in sheer wastage of memory. Thus, there
lies a tradeoff between the frequency of overflows and the space allocated.
• A better solution to deal with this problem is to have multiple queues or to have
more than one queue in the same array.
• One important point to note is that while queue A will grow from left to right, the
queue B on the same time will grow from right to left.
Applications of Queues

• Queues are widely used as waiting lists for a single shared resource like printer,
disk, CPU.
• Queues are used to transfer data asynchronously 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 OS for handling interrupts. When programming a real-time
system that can be interrupted, for ex, 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
THANK YOU

You might also like