The Queue
The Queue
The Queue
*** ' J:
I2L.
CSE303 ko ii
mmtmm
M
DATA STRUCTURES
*%
V*-* *
r\ j
r~ MIT-Ethiopia
2007 E.C / 2014
E.C/2014
CH#3: The Queue
By Asmelash Girmay
Department of Information Technology (Engineering)
HI
2
Chapter Outline
Y Definition of Queue
T
Y Operations performed on queue
Y Queue Implementation
Algorithm for operations of Queue
Drawback of Queue
Y Other Queues
Circular Queue: Algorithm
Dequ: Operations, Algorithm
Y Applications of Queue
`
3.2. Operations Performed on Queue …
•••
Front
T
Rear = -1
\
1
1. Queue is empty
Front = -1
10 3 41
t
Rear
Rear = 2
Front = 0 b
4
4. Push (41)
Fr Fro,
Rear
10
Rear = 0
Front = 0
10 3 41 70
t
Rear = 3
Front = 0 b
5
Rear
2. Push (10)
5. Push (70)
FT FT
3 10 3
f
Rear
Rear = 1
Front = 0 3 41 70
t
Rear = 3
Front = 1 b
6
Rear
3. Push (3) 6. X = pop () (i.e. X=10)
Data Structures By Asmelash Girmay
III
6
3.2.
c ). 2. Operations Performed on Queue … ••• y
`
FT Front
7 Rear = 4 Rear = 4
9
3 41 70 11 Front = 1 70 11 Front = 3
t
Rear
t
Rear
Fr
8 Rear = 4
41 70 11 Front = 2
t
Rear
r
3.3. Implementation of Queue
Queue can be implemented in two ways:
T
1. Using arrays (static)
2. Using pointers (dynamic)
r Underflow and overflow conditions can occur when a queue is implemented
using arrays. NB: Do you think that overflow and underflow occurs in dynamic?
r If we try to pop (or delete or remove) an element from queue when it is empty,
underflow occurs.
It is not possible to delete (or take out) any element when there is no element in the
queue.
r Suppose maximum size of the queue (when it is implemented using arrays) is 50.
If we try to push (or insert or add) an element to queue, overflow occurs.
When queue is full it is naturally not possible to insert any more elements
67 42 67 42 A
G[3]
Q[3] 42
G[2l 67
Front G[3]
|2]
Rear Rear Front
A circular queue after inserting 18, 7, 42t 67. A circular queue after popping 18h 7 A circular queue after pushing 30, 47, 14
Deletion
DeleLion
—
• Let Q be the array of MAX
1. Input the DATA to be inserted
elements. front (or left) and rear 2. If ((left = 0 && right MAX-1) | | (left -= right + 1))
(or right) are two array index (a) Display * Queue Overflow”
(b) Exit
(pointers), where the addition
3. If (left ==-l)
and deletion of elements (a) left = 0
Ji
f r
I
l
17u
Li
m u
i
I
"3*
acs
t
-v
a-d° I
i