Queue - Introduction - Operations
Queue - Introduction - Operations
I
T
2
0 Queue Data Structure
Introduction
8
Lecture 6
1 Dr. Belal Murshed
Data Structures and Algorithms
I
T
2 QUEUES
0
8
Introduction
Queues - Introduction
• Arrays
I • Linked List
T
2 • Stacks
0 • Queues
8 • Tree
• Graph
3 Dr. Belal Murshed
Data Structures and Algorithms
Queues - Introduction
• Definition
I oAn order collection of homogenous data element
in which insertion operation is defined at one end
T and deletion operation is defined at the other end.
oThe end at which deletion takes place is called
2 frond end and the end at which insertion takes
place is called Rear end.
0 oA data structure that stores information in the
8 form of a typical waiting line
oPlacing a value one after the other
• Example
oQueue at Airport
4
oQueue at Bus Stop Dr. Belal Murshed
Data Structures and Algorithms
Queues - Introduction
• Add to the rear of queue
I • Remove from front
T • Abstract Data Type (ADT)
2 • Logical data structure
0 • Implementation
8 oArrays
oLinked List
Queues - Introduction
• Access Policy
I oThe first element that is inserted into the
queue is the first element that will leave the
T queue
2 • First In First Out front
rear
0 oFIFO
8 • Processing End
oFront (to remove)
oRear (to add)
6 Dr. Belal Murshed
Data Structures and Algorithms
rear
• Abstract Data Type (ADT)
I • Logical Data Structure
T • Linear Data Structure
2 • Homogeneous
0 • Accessible only from both ends i. e. front &
rear
8 • Access policy is FIFO
• Implemented using Arrays or Linked List
7 Dr. Belal Murshed
Data Structures and Algorithms
I
T
2 QUEUES
0 Basic Operations
&
8
Its Use
Queues– Axioms
Axioms :
I 1. IsEmpty(Create_Queue () ) → True
T 2. IsEmpty(Enqueue(e,Q)) → False
3. IsFull(Create_Queue () ) → False
2
4. IsFull(dequeue(Q)) → false
0
5. RearOp(Enqueue(e,Q)) → e
8
6. If FrontOp(Q) == e Then
odequeue(Q) → e
10 Dr. Belal Murshed
Data Structures and Algorithms
I 1. Create_Queue ( ) → Q, Allocation
T 2. Clear_Queue (Q) → Reallocation
3. IsEmpty(Q) → boolean
2
4. IsFull(Q) → boolean
0
5. Enqueue(e,Q) → Q, update
8 6. Dequeue(Q) → e, element deleted
7. FrontOp(Q) → e, element in Front
8. RearOp(Q) → e, element in Rear back
11 Dr. Belal Murshed
Data Structures and Algorithms
T { rear
Declare Q[Maxsize]
2 Declare front=0
Declare rear=0 front rear
0 }
8
• Create Queue called Q, and declare and initialize
two pointers named front=0 and rear=0.
• Dequeue(Q) → e rear
I • Removes the element from the front of the queue
• O(1)
T
front rear
2
• dequeue (Q) 70 60 50 40 30
0
8 e= 70
front rear
60 50 40 30
14 Dr. Belal Murshed
Data Structures and Algorithms
0 70 60 50 40 30
• FrontOp(Q) → e
8
e= 70 front rear
70 60 50 40 30
0 70 60 50 40 30
• RearOp(Q) → e
8
e= 30 front rear
70 60 50 40 30
2
• isEmpty ()
0
8 Front = 0 True
otherwise False
otherwise False
front rear
19 Dr. Belal Murshed
Data Structures and Algorithms
Queues
Q
– Basic Operation
START: 2 4 6 8 10 Sequence of operations
0 time 3: 4 6 8 10 12 14
8 time 4: 4 6 8 10 12 14 16
time 5: 6 8 10 12 14 16
I
T
2 QUEUES
0 Implementation Logic
8 “Brute Force” method
Front fixed & Rear moveable
front rear
8 time 4: 8 10 12
Notice that the
array now has room
front rear
to add elements to
time 5: 10 12 the queue.
front rear
24 Dr. Belal Murshed
Data Structures and Algorithms
I
T
2 QUEUES
0 Implementation Logic
8 “Unrealistic” method
Front & Rear moveable
• Unrealistic approach
I • Make service desk moveable
T oAfter customer at front served
2 oService desk will move to next person
oNo need to move the customers
0
8
I
T
2 QUEUES
0
8
Implementation Logic
Circular Arrays
8 • Queue Full 2
o (rear % maxsize+1 == front)
10 15
or rear 11
o (front == 1 && rear == maxsize) visualization of
circular array
33 Dr. Belal Murshed
Data Structures and Algorithms
2
2 4 8 before
I front rear
4
2 4 8 6 after
T front rear
8
2 normal array implementation 6
0 rear
2 4 8 before visualization of a queue
8 front rear implemented as a circular array
after insertion of element 6
2 4 8 6 after
front rear
circular array implementation
Enqueue element 6 Dr. Belal Murshed
Data Structures and Algorithms
0 2 4 8 6 before
visualization of a queue
front rear
8 implemented as a circular array
10 2 4 8 6 after
after insertion of element 10
rear front
circular array implementation
T front rear
10 8
2 normal array implementation
rear 6
0 10 2 4 8 6 before
visualization of a queue
8 rear front implemented as a circular array
after dequeue operation
10 4 8 6 after
rear front
circular array implementation
dequeue Dr. Belal Murshed
Data Structures and Algorithms
T front rear
10 8
2 normal array implementation
6
0 10 4 8 6 before
visualization of a queue
8 rear front implemented as a circular array
after insertion of element 18
10 18 4 8 6 after
rear front
circular array implementation
10 8
normal array implementation
2 6
front
0 10 18 4 8 6 before
visualization of a queue
rear front
8 implemented as a circular array
10 18 8 6 after
after dequeue operation
rear front
circular array implementation
0 10 18 8 6 before
front
visualization of a queue
8 rear front implemented as a circular array
after dequeue operation
10 18 6 after
rear front
circular array implementation
dequeue Dr. Belal Murshed
Data Structures and Algorithms
front rear
I 18
10 18 after
T front rear
rear
10
2 normal array implementation
front
0 10 18 6 before
visualization of a queue
8 rear front
implemented as a circular array
after dequeue operation
10 18 after
front rear
circular array implementation
10 18 9
before
I front rear
18
10 18 9 after
T front rear
10
2 normal array implementation
front
0 10 18 before
visualization of a queue
8 front rear implemented as a circular array
after insertion of element 9
10 18 9 after
front rear
circular array implementation
Enqueue element 9 Dr. Belal Murshed
Data Structures and Algorithms
front rear
visualization of a queue
8 implemented as a circular array
18 9 after
after dequeue operation
front rear
circular array implementation
9 after
T front/rear
0 18 9 before
8 front rear
visualization of a queue
9 implemented as a circular array
after
front/rear rear
I front
after
T front/rear
0 9 before
8 front/rear
I
T
Queues
2 Implementation
0
8
Arrays
Array Implementation
• Array as Queue
• Array o enqueue
I o Insertion • Only at rear
o dequeue
• first, last,
T Anywhere o front
• Only from front
Queue Array
Create_Qeueue()
I {
Declare Q[Maxsize]; Q
T front = 0;
maxSize 5
rear = 0; front
2 }
0 1 2 3 4 5
0 rear
8 front 0 rear 0
I • bool isFull()
T • void enqueue(int e)
2 • int dequeue()
0 • int peek()
8 • int rearOp()
• int rearOp()
0
8
T Algend
return false;
rear 0 1 2 3 4 5
Q
2
maxSize 5 front 0 rear 0
queue
0
0 1 2 3 4 5
8 7 3 8
front 1 rear 3
True
Algend
T 0 1 2 3 4 5
Q
2 7 3 8 10 2
maxSize 5 front 1 Rear 5
queue
0
0 1 2 3 4 5
8 7 3 8
front 1 rear 3
True
• Circular Array
0 1 2 3 4 5
I • Check the queue is full or not 8 10
T • Value will be store at “rear” front 2 rear 3
rear = 3%5+1
2 • Circular increment in “rear” rear = 3+1 = 4
0 queue orear = rear % maxSize+1; queue
queue
0 1 2 3 4 5
8 0 1 2 3 4 5 0 1 2 3 4 5
8 10 7
6 8 10 7 5 8 10 7 5
front 2 rear 4
front 2 rear 5
rear 1 2 front
rear = 4%5+1
rear = 5%5+1 rear = 5%5+1 rear = 4+1 = 5
53 rear = 0+1 = 1 rear = 0+1 = 1 Dr. Belal Murshed
Data Structures and Algorithms
Queues Methods - enqueue(e)
Algorithm: enqueue
Input: 1. Q, Queue
2. e, element to be selected
Output: Q, Updated
I Method:
T if isFull() then
Queue is full
2 “No insertion is possible (Overflow”)
else
0 rear = rear % maxSize+1
Q[rear] = e;
8 if front == 0 then
rear=1
ifend
ifend
54 AlgEnd Dr. Belal Murshed
Data Structures and Algorithms
T 0 1 2 3 4 5
2 3 8
Q maxSize 5
front 1 rear 2
0 queue
0 1 2 3 4 5 True
8 3 8 10
front 1 rear 3
• Circular Array
0 1 2 3 4 5
I • Check the queue is empty or not 8 10
T • Value will be removed from “front” front 2 rear 3
8 0 1 2 3 4
10
front 3 rear 3
T 0 1 2 3 4 5
2 3 8 10
Q maxSize 5
front 1 rear 3
0 queue
0 1 2 3 4 5 True
8 8 10
front 2 rear 3
Value = 3
58 Dr. Belal Murshed
Data Structures and Algorithms
I Algorithm :peek( )
Method
T if isEmpty() then
2 queue is empty
else
0 return queue[front]
ifend
8
Algend
return queue[front]
T ifend 0 1 2 3 4 5
Algend
2 3 8 10
Q maxSize 5
front 1 rear 3
0 queue
0 1 2 3 4 5 True
8 3 8 10
front 1 rear 3
Value = 3
61 Dr. Belal Murshed
Data Structures and Algorithms
I Algorithm clear( )
Method:
T while !isEmpty() do
2 int e = dequeue()
print e
0 whilend
Algend
8
Method:
while !isEmpty() do 0 1 2 3 4 5
I int e = dequeue() 3 8 10
print e
T whilend True
front 1 rear 3
Algend
2 Q Values
Q
front count Printing
0 1 maxSize 5
queue
maxSize 5
queue
8 2 3 8 10 0 1 2 3 4 5
0 1 2 3 4 5
front rear
4
front rear
rear 3 front 3
rear 0 front 0
64 Dr. Belal Murshed
Data Structures and Algorithms
Solution
1. rear =3, front=1, maximum capacity=maxsize =10
I o Here rear>front then
T Number of element present in Queue (N) = rear-
front+1
2 N = 3-1+1 =3
0 N=3 1 2 3 4 5 6 7 8 9 10
50 30 70
8
front rear
Solution
2. rear =2, front=8, maximum capacity=maxsize =10
I o Here front>rear then
T Number of element present in Queue (N) =
N= maxsize-(front-rear-1)
2
N = maxsize+rear-front+1
0 N = 10+2-8+1 =5
8 N= 5 1 2 3 4 5 6 7 8 9 10
50 30 80 35 90
rear front
Number of element present in Queue (N) = 5
70 Dr. Belal Murshed
Data Structures and Algorithms
Solution
3. rear =8, front=8, maximum capacity=maxsize =10
I o Here front=rear then
T Number of element present in Queue (N) = rear-front+1
N= rear-front+1
2
N = 8-8+1= 1
0 N= 1
8 1 2 3 4 5 6 7 8 9 10
80
front rear
Solution
3. rear =0, front=0, maximum capacity=maxsize =10
I o Here when front=rear =0 then this is empty
T o So this case is exception
0 1 2 3 4 5 6 7 8 9 10
2
0
front rear
I
T
2
0
8