You are on page 1of 37

Queue

Dr. Manmath Narayan Sahoo


Dept. of CSE, NIT Rourkela
Queue
• A queue is a linear collection of elements
in which
– Insertion takes place at one end called Rear
– deletion can take place only at one end called
Front, and
• Queues are First-In-First-Out (FIFO)
data structures
Queue

• Queue are represented in two-ways


– Linear Array
– One-way Linked List
Array representation of Queue
• A queue is maintained by a
– linear array QUEUE
– Two index variables
• FRONT : Containing the index of the front
element of the queue
• REAR : Containing the index of the rear element
of the queue

• FRONT == 0 indicates that the queue is


empty, if array indexing starts from 1
Queue
FRONT: 1 AA BB CC DD …
REAR: 4 1 2 3 4 5 6 7 N

Delete an element

FRONT: 2 BB CC DD …
REAR: 4 1 2 3 4 5 6 7 N

Whenever an element is deleted from the


queue, the value of FRONT is increased by 1
FRONT = FRONT + 1
Queue
FRONT: 2 BB CC DD …
REAR: 4 1 2 3 4 5 6 7 N

Insert an element
FRONT: 2 BB CC DD EE …
REAR: 5
1 2 3 4 5 6 7 N

Whenever an element is inserted into the


queue, the value of REAR is increased by 1
REAR = REAR + 1
Queue
• REAR = N and Insert an element into
queue

FRONT: 7
XX … ZZ
REAR: N
1 2 3 4 5 6 7 N

Move the entire queue to the beginning of the


array
Change the FRONT and REAR accordingly
Insert the element
This procedure is too expensive
Queue
• Queue is assumed to be circular
• QUEUE[1] comes after QUEUE[N]
• Instead of increasing REAR to N +1, we
reset REAR = 1 and then assign
QUEUE[REAR] = ITEM
Queue
• FRONT = N and an element of QUEUE
is Deleted

FRONT: N
AA BB CC DD EE FF XX … ZZ
REAR: 7
1 2 3 4 5 6 7 N

We reset FRONT = 1, instead of increasing FRONT


to N + 1
Queue
• QUEUE contain one element
FRONT = REAR  0

FRONT: 7
XX …
REAR: 7
1 2 3 4 5 6 7 N
Algorithm to Insert in Q
[1] If FRONT = REAR % N + 1 then
Print: Overflow and Exit
[2] [Empty Queue]
If REAR = 0 then
Set FRONT = 1 and REAR = 1
Else Set REAR = REAR % N + 1
[3] Set QUEUE[REAR] = ITEM
[4] Exit
Queue

FRONT: 7
AA BB CC DD EE FF XX … ZZ
REAR: 6
1 2 3 4 5 6 7 N

FRONT = REAR % N + 1 [FULL QUEUE]


Algorithm to Delete from Q

[1] If FRONT = 0 then Print: Underflow


and Exit

[2] Set ITEM = QUEUE[FRONT]

[3] If FRONT = REAR then


Set FRONT = 0 and REAR = 0
Else Set FRONT = FRONT % N + 1
[4] Exit
Linked List Representation of Queue

• A linked queue is a queue implemented


as linked list with two pointer variable
FRONT and REAR pointing to the nodes
which is in the FRONT and REAR of the
queue
Linked List Representation of Queue

REAR
FRONT
AA BB CC X

15
Insertion in a Queue

REAR
FRONT
AA BB CC X

NEW
DD

16
Insertion in a Queue

FRONT
AA BB CC

DD X
REAR
17
Delete from a Queue

REAR
FRONT
AA BB CC X

18
Delete from a Queue

REAR
FRONT
BB CC X

19
Linked Queue

• No need to view it as circular for


efficient management of space
Insertion
[1] NEW -> INFO = ITEM
NEW -> LINK = NULL
[2] If (FRONT = NULL) then
FRONT = REAR = NEW
else
Set REAR -> LINK = NEW
REAR = NEW
[3] Exit
Deletion
[1] If (FRONT = NULL) then
Print: Underflow, and Exit
[2] PTR=FRONT
[3] FRONT = FRONT -> LINK
[4] FREE PTR
[5] Exit
Deque
• A deque is a linear list in which
elements can be added or removed at
either end but not in the middle

• Deque is implemented by a circular


array DEQUE with variables LEFT and
RIGHT which referes to the two end of
the deque
Deque
•LEFT = RIGHT = 0 indicate deque is empty

LEFT: 4 AA BB CC DD
RIGHT: 7
1 2 3 4 5 6 7 8
Variation of deque
• There are two variation of deque
[1] Input-restricted queue: Deque which
allows insertions at only one end of the list
but allows deletion at both ends of the list

[2] Output-restricted queue: Deque which


allows deletion at only one end of the list
but allows insertion at both ends of the
list
Deque
LEFT: 2
A C D
RIGHT: 4
1 2 3 4 5 6
F is added to the right

LEFT: 2 A C D F
RIGHT: 5 1 2 3 4 5 6
Deque
LEFT: 2
A C D F
RIGHT: 5
1 2 3 4 5 6
Two Letters on right is deleted

LEFT: 2 A C
RIGHT: 3 1 2 3 4 5 6
Deque
LEFT: 2
A C
RIGHT: 3
1 2 3 4 5 6
K, L and M are added to the Left

LEFT: 5 K A C M L
RIGHT: 3 1 2 3 4 5 6
Priority Queue
• A priority queue is a collection of
elements such that each element has been
assigned a priority and that the order in
which the elements are deleted and
processed comes from the following rules:
[1] Elements of higher priority is processed
before any elements of lower priority
[2] Two elements with the same priority are
processed according to the order in which
they were added to the queue
Priority Queue
• There are different ways a priority
queue can be represented such as

[1] One-way List

[2] Multiple queue


One-Way List Representation of a
Priority Queue
[1] Each node in the list will contain three
items of information: an information
field INFO, a priority number PRN,
and a link filed LINK

[2] A node X precedes a node Y in the list


(a) when X has higher priority than Y or
(b) when both have same priority but X
was added to the list before Y
Queue

Head
A1 B2 F2 D3

32
Insertion and Deletion
• Deletion : Delete the first node in the list.
• Insertion: Find the location of Insertion

Add an ITEM with priority number N


[a] Traverse the list until finding a node X
whose priority is less than N. Insert ITEM
in front of node X
[b] If no such node is found, insert ITEM as
the last element of the list
Array representation of Priority Queue

• Separate queue for each level of


priority
• Each queue will appear in its own
circular array and must have its own
pair of pointers, FRONT and REAR
• If each queue is given the same amount
space then a 2D queue can be used
QUEUE
FRONT REAR 1 2 3 4 5
2 2 1 AA
1 3 2 BB CC DD
0 0 3
4 1 4 FF DD EE
3 3 5 GG
Deletion Algorithm [outline]
[1] Find the smallest priority K such that
FRONT[K]  0

[2] Delete and process the front element


in row K of QUEUE

[3] Exit
Insertion Algorithm [outline]
Insert an element with priority M
[1] Insert ITEM as the rear element in
row M of QUEUE

[2] Exit

You might also like