0% found this document useful (0 votes)
14 views3 pages

Unit II - Queue at CSJMU - 6 Slides Handouts

This document discusses queues, including their definition as a first-in first-out (FIFO) linear data structure, common operations on queues like enqueue and dequeue, representations of queues in memory, limitations of linear queues addressed by circular queues, algorithms for inserting and deleting items from a queue, applications of queues, and double-ended queues (deques) which allow insertion and removal from both ends.

Uploaded by

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

Unit II - Queue at CSJMU - 6 Slides Handouts

This document discusses queues, including their definition as a first-in first-out (FIFO) linear data structure, common operations on queues like enqueue and dequeue, representations of queues in memory, limitations of linear queues addressed by circular queues, algorithms for inserting and deleting items from a queue, applications of queues, and double-ended queues (deques) which allow insertion and removal from both ends.

Uploaded by

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

Unit - II

Queue Operations on Queue


 FIFO
 CreateQueue( ) - to create q as an empty queue.
 A linear data structure used to represent a linear
list of elements. In queue, deletion of an element  Enque(i) - to insert(add) element i in a queue q.
takes place only at one end, called Front and  Dequeue( ) - to access and remove an element
insertion takes place only at other end, called from queue q.
Rear.
 Peek( ) - to access the first element of the queue
 Examples: q without removing it.
 Queue of people waiting to purchase tickets.
 isFull( ) - to check whether the queue q is full.
 Queue of tasks waiting for printer, for access to disk
storage.  isEmpty( ) - to check whether the queue q is
 Applications: empty.
 In a single program multiple requests can be kept in
queue.
Unit II - Queues 1 Dr. Rabins Porwal Unit II - Queues 2 Dr. Rabins Porwal

1 2

0 1 2 3 4 5 6 7 8 9
Queue as an ADT -1 -1
Front Rear Representation of queue in memory
 A queue of elements of any particular type is a 0 1 2 3 4 5 6 7 8 9
finite sequence of elements of that type 0 4 5 7 12 8 9
Front Rear Representation of queue in memory
together with the following operations:
0 1 2 3 4 5 6 7 8 9
 Initialize a queue to be empty.
2 4 12 8 9
 Determine if queue is empty or not. Front Rear Representation of queue in memory
 Determine if queue is full or not 0 1 2 3 4 5 6 7 8 9
 If queue is not full, then insert a new element after 2 7 12 8 9 3 10 11
the last element in a queue. Front Rear Representation of queue in memory
 If queue is not empty, then retrieve the first element 0 1 2 3 4 5 6 7 8 9
of a queue. 2 9 12 8 9 3 10 11 15 20
 If queue is not empty, then delete the first element Front Rear Representation of queue in memory
from a queue. 0 1 2 3 4 5 6 7 8 9
0 8 12 8 9 3 10 11 15 20 60
Unit II - Queues 3 Dr. Rabins Porwal UnitFront
II - Queues Rear Representation
4 of queue in memory
Dr. Rabins Porwal

3 4

Limitations of a linear queue Circular Queue


 If the last position of the Queue is occupied, it  This overcomes the problem of unutilized
is not possible to enqueue any more space in linear queue, implemented as an
elements even though some positions are array. In the array implementation, there is a
vacant towards the front position of the possibility that the queue is reported full even
queue. though slots of queue are empty.
 This limitation can be overcome if we treat  In short, just because we have reached the
that the queue position with index 0 comes end of the array, the queue would not be
reported as full. The queue would be reported
immediately after the last queue position with
full only when all the slots in the array are
index MAX-1.The resulting queue is known occupied.
as Circular Queue
Unit II - Queues 5 Dr. Rabins Porwal Unit II - Queues 6 Dr. Rabins Porwal

5 6

Prepared by: Dr. Rabins Porwal 1


Unit - II

Algorithm to insert an item in Queue Algorithm to delete an item from Queue


QINSERT(QUEUE, N, FRONT, REAR, ITEM) QDELETE(QUEUE, N, FRONT, REAR, ITEM)
This procedure inserts an element ITEM into a queue This procedure deletes an element from a queue and
1. If FRONT=1 and REAR=N, or If FRONT=REAR+1, then: assigns it to variable ITEM
Write: OVERFLOW and return 1. If FRONT=NULL , then:
2. If FRONT = NULL, then: Write: UNDERFLOW and return
Set FRONT:=1 and REAR:=1 2. Set ITEM:=QUEUE[FRONT]
else if REAR=N, then: 3. If FRONT = REAR, then
Set REAR:=1 Set FRONT:=NULL and REAR:=NULL
else else if FRONT=N, then:
Set REAR:=REAR + 1 Set FRONT:=1
[End of If structure] else:
3. Set QUEUE[REAR]:=ITEM Set FRONT:=FRONT + 1
4.Return [End of If structure]
Unit II - Queues 7 Dr. Rabins Porwal 4.II -Return
Unit Queues 8 Dr. Rabins Porwal

7 8

Applications of queues Double Ended Queue (Deque)


 There are several algorithms that use queues to
solve problems efficiently. For example, we use  A linear list of elements in which elements
queue for performing breadth-first search on a
graph.
can be added or removed at either end but
not in the middle, i.e., the elements can be
 When the job are submitted to a networked printer,
they are arranged in order of arrival. Thus,
added or deleted from both front or rear
essentially, jobs sent to a printer are placed on a ends.
queue.  There are two variations of a deque – an
 Virtually every real-life line (supposed to be) a input restricted deque and output restricted
queue. For example, lines at ticket counter at deque, which are intermediate between
cinema halls, railway stations, bus stand, etc, are deque & a regular queue.
queues, because the service, i.e., ticket, is provided
on first-come first-served basis.
Unit II - Queues 9 Dr. Rabins Porwal Unit II - Queues 10 Dr. Rabins Porwal

9 10

Double Ended Queue (Contd…) Priority Queue


 Input restricted deque: A deque which allows
insertions at only one end but allows deletions from  A collection of elements where each element is
both ends of the list. assigned a priority & the order in which elements are
 Output restricted deque: A deque which allows deleted & processed, which is determined by the
deletions from only one end but allows insertions at following rules:
both ends of the list.  An element of higher priority is processed before any
 Two possibilities that must be considered while element of lower priority.
inserting or deleting elements into the queue are:  An element with the same priority are processed according
 When an attempt is made to insert an element into a deque to the order in which they are added to the queue.
which is already full, an overflow occurs.  Example: A timesharing system – in which programs
 When an attempt is made to delete an element from a of high priority are processed first and programs with
deque which is empty, underflow occurs.
the same priority form a standard queue.

Unit II - Queues 11 Dr. Rabins Porwal Unit II - Queues 12 Dr. Rabins Porwal

11 12

Prepared by: Dr. Rabins Porwal 2


Unit - II

Array Implementation of Priority Queue Priority Queue as one-way list


 In array implementation, insertion is easy but
 A priority queue can be represented in
deletion of elements would be difficult
memory by means of one-way list:
because while inserting elements in priority
 Each node in the list contains three items of
queue they are not inserted in an order. As a
information – an info field INFO, a priority no.
result, deleting an element with the highest PRNO and the link NEXT.
priority would require examining the entire  Node A will precede node B in the list if A has
array to search for such an element. higher priority than B or if both the nodes have
same priority but A was added to the list before B,
i.e., the order in one-way list corresponds to the
order of priority queue.

Unit II - Queues 13 Dr. Rabins Porwal Unit II - Queues 14 Dr. Rabins Porwal

13 14

Prepared by: Dr. Rabins Porwal 3

You might also like