Unit 5: Linear Data Structure - Queues
Unit 5: Linear Data Structure - Queues
UNIT 5
6 HOURS
SYLLABUS CONTENT
Queue as an ADT
Operations
Array and Linked List representation of Queue with corresponding analysis
Linear Queue
Circular Queue
Double Ended Queue (DEQUE)
Priority Queue
Applications of Queue
QUEUES
QUEUES
Queue: a collection whose elements are added at one end (the rear or tail of the
queue) and removed from the other end (the front or head of the queue)
A queue is a FIFO (first in, first out) data structure
Any waiting line is a queue:
The check-out line at a grocery store
The cars at a stop light
An assembly line
Removing an element
New front element of queue
Element is removed
from the front of the
DR. NEEPA SHAH queue 6
int dequeue(): removes and returns the element at the front of the queue
QUEUE EXAMPLE
Operation Output
enqueue(5) – (5)
enqueue(3) – (5, 3)
dequeue() 5 (3)
enqueue(7) – (3, 7)
dequeue() 3 (7)
front() 7 (7)
dequeue() 7 ()
dequeue() “error” ()
isEmpty() true
enqueue(9) – (9)
enqueue(7) – (9, 7)
size() 2 (9, 7)
enqueue(3) – (9, 7, 3)
enqueue(5) – (9, 7, 3, 5)
dequeue() 9 (7, 3, 5)
DR. NEEPA SHAH 8
APPLICATIONS OF QUEUE
1. As waiting lists for a single shared resource like printer, disk, CPU. (Web, file, ftp, database, mail, etc.)
4. In Playlist for jukebox to add songs to the end, play from the front of the list.
Shared
Service
TYPES OF QUEUE
ARRAY-BASED QUEUE
normal configuration
Q
0 1 2 f r
12 9 7 18 14 36
0 1 2 3 4 5 6 7 8 9
12 9 7 18 14 36 45
0 1 2 3 4 5 6 7 8 9
DR. NEEPA SHAH 14
Now, if we want to delete an element from the queue, then the value of front will be incremented.
9 7 18 14 36 45
0 1 2 3 4 5 6 7 8 9
Before inserting an element in the queue we must check for overflow conditions. (i.e.
when rear = MAX – 1)
Similarly, before deleting an element from the queue, we must check for underflow
condition. (If front = -1 and rear = -1)
normal configuration
Q
0 1 2 f r
wrapped-around configuration
Q
DR. NEEPA SHAH 0 1 2 r f 19
7 18 14 36 45 21 99 72
0 1 2 3 4 5 6 7 8 9
In a circular queue, the first index comes right after the last index.
A circular queue is full, only when front=0 and rear = Max – 1.
90 49 7 18 14 36 45 21 99 72
▪ If rear != MAX – 1, then the rear will be incremented and value will be inserted
front=0 1 2 3 4 5 6 7 rear= 8 9
90 49 7 18 14 36 45 21 99
▪ 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.
front=1 2 3 4 5 6 7 8 rear= 9
49 7 18 14 36 45 21 99 72
9 10 7 18 14 36 45 21 99 72
rear=1 front=2 3 4 5 6 7 8 9
▪ If front = -1, then it means there are no elements in the queue. So an underflow condition will be reported.
0 1 2 3 4 5 6 7 8 9
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.
Delete this element and set
81
rear = front = -1
0 1 2 3 4 5 6 7 8 front=rear= 9
If the queue is not empty and after returning the value on front, if front = MAX -1, then front is set to 0.
72 63 9 18 27 39 81
0 1 2 3 4 rear= 5 6 7 8 front= 9
DR. NEEPA SHAH 25
front
12 12
11 11
rear rear
10 10
rear
1
0
front
12
11
DR. NEEPA SHAH After 8 more enqueues 27
10
3 2 3
1 4
front queue 5
cq 0
8 5 n-1 6
rear count n-2 7
n-3 8
9
98 2 3
1 4
front queue 5
cq 0
2 4 99 6
rear count 98 7
97 8
9
29
98
cq front queue
0 1 2 3 4 96 97 98 99
2 4 …
rear count
Algorithm isEmpty()
return (size = 0)
We use the modulo operator (remainder of division)
Q
0 1 2 f r
Q
0 1 2 r f
Algorithm enqueue(o)
if size() = N - 1 then
error
else
Q[r] o
r (r + 1) mod N
Q
0 1 2 f r
Q
0 1 2 r f
Algorithm dequeue()
if isEmpty() then
Algorithm front()
error
if isEmpty() then
else
error
temp Q[f]
return Q[f]
f (f + 1) mod N
return temp
Fundamental Methods:
addFirst(e)
addLast(e)
removeFirst()
removeLast()
Support methods:
getFirst()
getLast()
size(): rear-front+1
isEmpty()
INSERTION IN DEQ
Front -- Rear++
Queue[front] = element Queue[rear] = element
DELETION IN DEQ
front=rear=-1(UNDERFLOW) front=rear=-1(UNDERFLOW)
Front ++ Rear--
deletion
10 20 30 40 50 insertion
deletion
Q[0] Q[1] Q[2] Q[3] Q[4]
Output-restricted Deque: restricts the deletion of elements at one end only, and allows insertion to be done at both the
ends of a Deque.
insertion
deletion
10 20 30 40 50 insertion
INTERFACE
1. input-restricted Q
2. output-restricted Q
3. Exit
PRIORITY QUEUE
6 2
15 23
insert deleteMin
12 18
45 3 7
PQueue property: for two elements in the queue, x and y, if x has a lower priority value
than y, x will be deleted before y
DR. NEEPA SHAH 41
Min Priority Queue: In min priority Queue minimum number of value gets the highest priority and maximum
number of element gets the lowest priority.
Max Priority Queue: Max priority Queue is the opposite of min priority Queue. Maximum number value gets
the highest priority and minimum number value gets the minimum priority.
Additional methods
int min()
int size()
int isEmpty()
EXAMPLE
Operation Output Priority Queue
insert(5, A) [= (5, A)] {(5, A)}
Insert(9, C) [= (9, C)] {(5, A), (9, C)}
Insert(3, B) [= (3, B)] {(3, B), (5, A), (9, C)}
Insert(7, D) [= (7, D)] {(3, B), (5, A), (7, D), (9, C)}
min() e3 {(3, B), (5, A), (7, D), (9, C)}
removeMin() e3 {(5, A), (7, D), (9, C)}
size() 3 {(5, A), (7, D), (9, C)}
removeMin() e1 {(7, D), (9, C)}
removeMin() e4 {(9, C)}
removeMin() e2 {}
removeMin() “error” {}
isEmpty()
DR. NEEPA SHAH true {} 47
The running time of this sorting method depends on the priority queue
implementation
SELECTION SORT
We repeatedly find the next largest (or smallest) element in the array and
move it to its final position in the sorted array.
We begin by selecting the largest element and moving it to the highest index
position. We can do this by swapping the element at the highest index and the
largest element. The process stops when the effective size of the array
becomes 1
INSERTION SORT
Performance:
insert takes O(1) time since we can insert
Performance:
the item at the beginning or end of the insert takes O(n) time since we have to find
sequence the place where to insert the item
removeMin and min take O(n) time since we removeMin and min take O(1) time, since
have to traverse the entire sequence to find
the smallest key the smallest key is at the beginning
COMPARISON
SELECTION-SORT
1 + 2 + …+ n
Selection-sort runs in O(n2) time
SELECTION-SORT EXAMPLE
Sequence S Priority Queue P
Input: (7,4,8,2,5,3,9) ()
Phase 1
(a) (4,8,2,5,3,9) (7)
(b) (8,2,5,3,9) (7,4)
.. .. ..
. . .
(g) () (7,4,8,2,5,3,9)
Phase 2
(a) (2) (7,4,8,5,3,9)
(b) (2,3) (7,4,8,5,9)
(c) (2,3,4) (7,8,5,9)
(d) (2,3,4,5) (7,8,9)
(e) (2,3,4,5,7) (8,9)
(f) (2,3,4,5,7,8) (9)
DR. NEEPA SHAH 55
(g) (2,3,4,5,7,8,9) ()
INSERTION-SORT
INSERTION-SORT EXAMPLE
Sequence S Priority queue P
Input: (7,4,8,2,5,3,9) ()
Phase 1
(a) (4,8,2,5,3,9) (7)
(b) (8,2,5,3,9) (4,7)
(c) (2,5,3,9) (4,7,8)
(d) (5,3,9) (2,4,7,8)
(e) (3,9) (2,4,5,7,8)
(f) (9) (2,3,4,5,7,8)
(g) () (2,3,4,5,7,8,9)
Phase 2
(a) (2) (3,4,5,7,8,9)
(b) (2,3) (4,5,7,8,9)
.. .. ..
. . .
(g) (2,3,4,5,7,8,9) ()
DR. NEEPA SHAH 57
IN-PLACE INSERTION-SORT
Instead of using an external data structure, 5 4 2 3 1
we can implement selection-sort and
insertion-sort in-place 5 4 2 3 1
A portion of the input sequence itself
serves as the priority queue 4 5 2 3 1
For in-place insertion-sort
We keep sorting the initial portion of the
2 4 5 3 1
sequence
We can use swaps instead of modifying the 2 3 4 5 1
sequence
1 2 3 4 5
DR. NEEPA SHAH 58
1 2 3 4 5
findMax