Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

, - *

*** ' 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

Data Structures By Asmelash Girmay


3
3.1. Definition of Queue
Y A queue is logically a first in first out (FIFO or first come first serve)
>Y
linear data structure.
Y The concept of queue can be understood by our real life problems.
 For example a customer come and join in a queue to take the train ticket at
the end (rear) and the ticket is issued from the front end of queue.
 That is, the customer who arrived first will receive the ticket first.
 It means the customers are serviced in the order in which they arrive at the
service center.
Y It is a homogeneous collection of elements in which new elements are
added at one end called rear, and the existing elements are deleted
from other end called front.
Data Structures By Asmelash Girmay
HI
4
3.2. Operations Performed on Queue
Y The basic operations that can be performed on queue are:
>Y
1. Push: Insert (or add) an element to the queue.
2. Pop: Delete (or remove) an element from a queue.
Y Push operation will insert (or add) an element to queue, at the rear
end, by incrementing the array index or Incrementing rear.
Y Pop operation will delete (or remove) from the front end by
decrementing the array index (or Incrementing front) and will assign
the deleted value to a variable.
Y Total number of elements present in the queue is front-rear+1, when
implemented using arrays.

Data Structures By Asmelash Girmay


5

 `
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

7. Push (11) 9. X= pop() ( i.e. X=41)

Fr
8 Rear = 4
41 70 11 Front = 2

t
Rear

8. X = Pop () ( i.e. X=3)

Data Structures By Asmelash Girmay


HI
7

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

Data Structures By Asmelash Girmay


HI
8
3.3.1 Algorithm
i Ylgorithm for Queue Operations
f
Y Let Q be the array of some specified size say SIZE

INSERTING AN ELEMENT INTO THE QUEUE DELETING AN ELEMENT FROM QUEUE


i. Initialize front=0 rear = -1 1. If (rearc front)
2. Input the value to be inserted and assign to variable “data" (a) Front = 0, rear = -I
3. If (rear >= SIZE)
(b) Display hThe queue is empty"
(a) Display "Queue overflow"
(b) Exit (c) Exit
4. Else 2. Else
(a) Rear = rear +1 (a) Data = Q[front]
5. Q[rear] - data 3, Front = front +1
6. Exit 4. Exit

Data Structures By Asmelash Girmay


9
3.3.2. Drawback of Queue …
•••

T Suppose a queue Q has maximum size 5, say 5 elements pushed and 2


Y
elements popped.
T Now if we attempt to add more elements, even though 2 queue cells are
free, the elements cannot be pushed.
T Because in a queue, elements are always inserted at the rear end and hence
rear points to last location of the queue array Q[4].
T That is queue is full (overflow condition) though it has is empty location.
T This limitation can be overcome if we use Front Rear
circular queue. \
70 11 13
0(0] Q[1] G[2] Q3] 0(4]

Data Structures By Asmelash Girmay


10
3.4. Other Queues
t There are three major variations in a simple queue. They are:
>Y
1. Circular queue
2. Double ended queue (de-queue)
3. Priority queue
t Priority queue is generally implemented using linked list, which is
discussed in chapter#4 as an application Linked List.
t The other two queue variations are discussed in the following sections.

Data Structures By Asmelash Girmay


HI
11
3.4.1. Circular Queue
Y In circular queues the elements Q[0], Q[1], Q[2] .... Q[n – 1] is
T
represented in a circular fashion with Q[1] following Q[n].
Y A circular queue is one in which the insertion of a new element is done
at the very first location of the queue if the last location at the queue is
full.
Y Suppose Q is a queue array of 6 elements.
Y Push and pop operation can be performed on circular.
Y The following figures will illustrate the same.

Data Structures By Asmelash Girmay


HI
12
3.4.1. Circular Queue …
•••

Q[0] Q[5] Q[0] Rear


Q[ Front
18 Q[S] Q[0]
14
47
Q[11 Q[1]
Q[4] 7 Q[4;
Q[1]
30

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

Data Structures By Asmelash Girmay


HI
13
3.4.1. Circular Queue …
•••

Y At any time the position of the element to be inserted will be calculated


>Y
by the relation Rear = (Rear + 1) % SIZE.
Y After deleting an element from circular queue the position of the front
end is calculated by the relation Front= (Front + 1) % SIZE
Y After locating the position of the new element to be inserted, rear,
compare it with front.
Y If (rear = front), the queue is full and cannot be inserted anymore.

Data Structures By Asmelash Girmay


14
3.4.1. Circular Queue> Algorithm

Y Let Q be the array of


Aigc)rithm

Inserting an element to circular Queue


If
1. Initialize FRONT = - 1; REAR = I
some specified size 2. REAR = (REAR + 1) % SIZE
say SIZE. 3. If (FRONT is equal to REAR)
Y FRONT and REAR (a) Display “Queue is full”
(b) Exit
are two pointers where
4. Else
the elements are (a) Input the value to be inserted and assign to variable UDATA”
deleted and inserted at -
5. If (FRONT is equal to 1)
two ends of the (a) FRONT =0
circular queue.
(b) REAR =0
6. QIREAR] = DATA
Y DATA is the element 7. Repeat steps 2 to 5 if we want to insert more elements
to be inserted S. Exit

Data Structures By Asmelash Girmay


15
3.4.1. Circular Queue> Algorithm …•••

Deleting an element from a circular queue


>Y
1. If (FRONT is equal to - 1)
(a) Display “Queue is empty"
(Ib) Exit
2. Else
(a) DATA = Q[FRONT|
3. If (REAR is equal to FRONT)
(a) FRONT = -1
(b) REAR = -I
4. Else
(a) FRONT = (FRONT +1) % SIZE
5. Repeat the steps 1T 2 and 3 if we want to delete more elements
6. Exit

Data Structures By Asmelash Girmay


16
o
3.4.2.
c .4.2. Deque
)

t A deque is a homogeneous list in which elements can be added or


if
inserted (called push operation) and deleted or removed from both the
ends (which is called pop operation).
T i.e. we can add a new element at the rear or front end and also we can
remove an element from both front and rear end.
Y Hence it is called Double Ended Queue.
Front Rear
Addition
J71 44
L
51 14 18 67
Addition

Deletion
DeleLion

Data Structures By Asmelash Girmay


HI
17
3.4.2. Deque …
•••

Y There are two types of deque depending upon the restriction to


>Y
perform insertion or deletion operations at the two ends. They are:
1. Input restricted deque
2. Output restricted deque
Y An input restricted deque: is a deque, which allows insertion at only 1
end, rear end, but allows deletion at both ends, rear and front end of
the lists.
Y An output-restricted deque: is a deque, which allows deletion at only
one end, front end, but allows insertion at both ends, rear and front
ends, of the lists.

Data Structures By Asmelash Girmay


18
3.4.2. Deque> Operations
Y The possible operation performed on deque is:
>Y
1. Add an element at the rear end
2. Add an element at the front end
3. Delete an element from the front end
4. Delete an element from the rear end
Y Only 1st, 3rd and 4th operations are performed by input-restricted
deque, and
Y 1st, 2nd and 3rd operations are performed by output-restricted deque.

Data Structures By Asmelash Girmay


19
3.4.2. Deque> Algorithm
Y Algorithm for Inserting an Element:
Y
INSERT AN ELEMENT AT THE RIGHT SIDE OF THE DE-QUEUE


• 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

occurred. (b) right - 0


4. Else
• Let DATA be the element to be (a) if (right == MAX -1)
inserted. (i) left = 0
(b) else
• Before inserting any element to
(i) right = rights1
the queue left and right pointer 5. Q[right] = DATA
will point to the – 1. 6. Exit

Data Structures By Asmelash Girmay


20
3.4.2. Deque> Algorithm …•••
INSERT AN ELEMENT AT THE LEFTSIDE OF THE DE-QUEUE
if
1. Input the DATA to be inserted
2. If ((left == 0 && right == MAX-1) | | (left == rights1))
(a) Display Queue Overflow”
ib) Exit
3. If (left == - 1)
(a) Left = 0
(b) Right = 0
4. Else
(a) if (left == 0)
(9 left = MAX - 1
(b) else
(i) left = left - 1
5. Qlleftj = DATA
6. Exit

Data Structures By Asmelash Girmay


21
3.4.2. Deque> Algorithm …•••
Y Algorithm for Deleting an Element:
Y
DELETE AN ELEMENT FROM THE RIGHT SIDE OF THE DE-QUEUE
• Let Q be the array of MAX
elements.
-
1. If (left == 1)
(a) Display ‘‘Queue Underflow*
• front (or left) and rear (or right) (b) Exit
are two array index (pointers), 2. DATA = Q (right j
3. If (left == right)
where the addition and deletion
-
(a) left = 1
of elements occurred. (h) right = - 1
• DATA will contain the element 4. Else
(a) iffright == 0)
just deleted.
(i) right = MAX-1
(h) else
(J) right = right- 1
5. Exit

Data Structures By Asmelash Girmay


22
3.4.2. Deque> Algorithm …•••
DELETE AN ELEMENT FROM THE LEFT SIDE OF THE DE-QUEUE
if
1. If (left == - 1)
(a) Display Queue Underflow*
(b) Exit
2. DATA = Q [Left]
3. Iftleft == right)
(a) left = - 1
(b) right = - 1
4. Else
(a) if (left == MAX-1)
(i) left = 0
(b) Else
(i) left = left +1
5. Exit

Data Structures By Asmelash Girmay


HI
23
3.5. Applications of Queue
T
Y Round robin techniques for processor scheduling is implemented
using queue.
Y Printer server routines (in drivers) are designed using queues.
Y All types of customer service software (like Railway/Air ticket
reservation) are designed using queue to give proper service to the
customers.

Data Structures By Asmelash Girmay


¥

Ji
f r
I

l
17u
Li
m u
i
I

"3*
acs
t

-v
a-d° I
i

You might also like