You are on page 1of 24

CLO-1

Week#09
Queue and Its Implementation

Course: Data Structures and Algorithm (CS-215)


Course Teacher: Dr. Umme Laila

Contact Info:
Room No: 112
Email: umme.laila@iobm.edu.pk

1
Introduction to Queue
• Queues are linear data structures in which we add elements
to one end (REAR) and remove them from the other end
(FRONT).

• Also called First-In-First-Out (FIFO) list, since the first


item to be inserted is the first to be deleted.

• Queue Operations:
• Enqueue: Insert an element at the REAR of the queue.
• Dequeue: Delete an element at the FRONT of the queue.

2
Real-life Application of Queue

3
Application of Queue in computer Science
•Queue is used in
 Waiting lists for a single shared resource like CPU,
Disk, and Printer.

 Buffers on MP3 players and portable CD players.

 Operating system to handle the interruption.

 Add song at the end or to play from the front.

4
Example

REAR
FRONT

5
Queue Representation
• Queues may be represented as one-way list or linear arrays
QUEUE.
• Two pointer variables are used
• FRONT – containing the location of front element of the queue.
• REAR – containing the location of the rear element of the queue

• The FRONT=NULL will indicate the queue is empty.

•Whenever an element is deleted from the queue, the value FRONT is


increased by 1
FRONT: =FRONT+1

•Whenever an element is inserted or added to the queue the REAR is


increased by 1.
REAR= REAR+1

•After N insertions, the rear element of the queue will occupy QUEUE [N]

6
Array Representation of Queue

7
Array Representation of Queue
•Suppose the queue is at the end and we need to insert an ITEM at N then
we consider that the QUEUE is

QUEUE IS CIRCULAR, that is, that QUEUE [1] comes after QUEUE
[N] in the array.

•We insert ITEM into the QUEUE by assigning ITEM to QUEUE [1] in the
array. Instead of increasing REAR to N+1 we reset REAR=1 and then
assign

QUEUE [1]:= ITEM

•IfFRONT=N and an element of QUEUE is deleted, we reset FRONT=1


instead of increasing FRONT to N+1

•Suppose that our queue contains only one elements


FRONT = REAR  NULL

•Suppose that the element is deleted


FRONT := NULL
REAR := NULL
•To indicate that the queue is empty. 8
Example 6.11
•Suppose
a Queue may be maintained by circular array
QUEUE with N=5 memory locations.

•Observe that queue occupies consecutive locations when it


occupies location at the beginning and at the end of the array.

•If the queue is viewed as a circular array, this means that it


still occupies consecutive locations.

•The queue will be empty only when FRONT=REAR and an


element is deleted.
•For this reason
FRONT = NULL
REAR =NULL

9
Example 6.11

10
Example 6.11 (Contd.)

11
Algorithms for Queue Operations

12
Algorithms for Queue Operations

13
DEQUE

• Double-ended Queue (dequeue), often abbreviated to


• Deque

• It is a linear list in which elements can be added or removed at


either end but not in the middle.

• This differs from the queue or First-In-First-Out List (FIFO), where


elements can only be added to one end and removed from the
other.

• We assume that the deque is maintained by a circular array


DEQUE with two pointers, points to the two ends of deque.

14
DEQUE
• There are two variations of a deque

1. Input-restricted Deque: it restricts the insertion of the element


at one end only, the deletion of elements can be done at both
the end of a deque

2. output-restricted Deque: it restricts the deletion of the


elements at one end only and allows insertion to be done at
both the end of a deque

15
DEQUE
Various operations of Dequeu are:-

1.Insertion / enque from rear


2.Insertion / enque from front
3.Deletion / deque from front
4 Deletion / deque from rear
5.display

16
DEQUE
Algorithm to Insert / enqueue from rear

FRONT = -1, REAR = -1, TO REPRESENT DEQUEUE IS


EMPTY
Algorithm INSERTREAR(QUEUE[N],FRONT,REAR,ITEM)
{
//QUEUE is an array of size N ,ITEM is element to be
inserted.
1. if (REAR == N-1)
1.1 PRINT “OVERFLOW”
2. else
2.1 IF(FRONT==-1)
2.1.1 FRONT = 0
2.2. REAR = REAR+1
2.3 QUEUE[REAR] = ITEM
}
17
DEQUE
Algorithm to Insert / enqueue from front

FRONT = -1, REAR = -1, TO REPRESENT DEQUEUE IS EMPTY


Algorithm INSERTFRONT(QUEUE[N],FRONT,REAR,ITEM)
{
//QUEUE is an array of size N ,ITEM is element to be inserted.
1. if( FRONT == 0)&& (REAR ==N-1)
1.1 Print “CAN NOT INSERT”
2. else
2.1 if (FRONT == -1)
2.1.1 FRONT = 0
2.1.2 REAR = 0
2.2 else
2.2.1 if(FRONT == 0 && REAR != N-1)
2.2.2 FRONT= N-1
2.3 else
2.3.1 FRONT = FRONT-1
2.4 QUEUE[FRONT] = ITEM 18
DEQUE
Algorithm to Delete / dequeue from front

FRONT = -1, REAR = -1, TO REPRESENT DEQUEUE IS


EMPTY
Algorithm DELETE(QUEUE[N],ITEM,FRONT,REAR)
{
1. if ( FRONT == – 1 )
1.1 Print “QUEUE EMPTY”
2. else
2.1 ITEM = QUEUE[FRONT]
2.2 FRONT = FRONT +1
}

19
DEQUE
Algorithm to Delete / dequeue from rear

FRONT = -1, REAR = -1, TO REPRESENT DEQUEUE IS


EMPTY
Algorithm DELETE(QUEUE[N],ITEM,FRONT,REAR)
{
1. if (( FRONT == – 1 ) || (FRONT == REAR+1))
1.1 Print “QUEUE EMPTY”
2. else
2.1 ITEM = QUEUE[REAR]
2.2 REAR = REAR - 1
}

20
DEQUE

• Important points to be considered, when applying the procedures.

 OVERFLOW: when an element to be inserted into a deque which is


already full

 UNDERFLOW: When an element is to be deleted from a deque which


is empty.

21
APPLICATION OF DEQUEUE

• Job scheduling algorithm.

• Deque is used in storing a web browser's history. Recently visited


URLs are added to the front of the dequeue, and the URL at the
back of the dequeue is removed after some specified number of
insertions at the front.

22
Priority Queues
• A priority queue is a collection of elements where the elements
are stored according to their priority levels.

• The order in which the elements should get added or removed is


decided by the priority or the elements (ascending or
descending).

• Following rules are applied to maintain a priority queue.

 The element with a higher priority is processed before any element of


lower priority
 If there are elements with same priority, then the element added first in
the queue would get processed.

23
Applications of Priority Queues
• Priority queues are used for implementing job scheduling by the
operating system, where jobs with higher priorities are to be
processed first.

• Simulation systems, where priority corresponds to event times.

24

You might also like