0% found this document useful (0 votes)
15 views73 pages

Queue - Introduction - Operations

The document discusses queues as a data structure. It defines queues as a linear data structure where insertion takes place at one end and deletion takes place at the other end in a first-in, first-out manner. The document outlines the basic operations of queues like enqueue, dequeue, isEmpty and isFull and provides examples of their implementation and use.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views73 pages

Queue - Introduction - Operations

The document discusses queues as a data structure. It defines queues as a linear data structure where insertion takes place at one end and deletion takes place at the other end in a first-in, first-out manner. The document outlines the basic operations of queues like enqueue, dequeue, isEmpty and isFull and provides examples of their implementation and use.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 73

Data Structures and Algorithms

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

2 Dr. Belal Murshed


Data Structures and Algorithms

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

5 Dr. Belal Murshed


Data Structures and Algorithms

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

Queues – Summary front

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

8 Dr. Belal Murshed


Data Structures and Algorithms

Stacks – Basic Operation


Formally:
o Domain: Application dependent
1. Create_Queue ( ) → Q, Allocation
I 2. Clear_Queue (Q) → Reallocation
T 3. IsEmpty(Q) → boolean
o Returns true if Queue is empty else false.
2 4. IsFull(Q) → boolean
o Returns true if Queue is Full else false.
0 5. Enqueue(e,Q) → Q, update
o To insert an element into the queue
8 6. Dequeue(Q) → e, element deleted
o To remove an element from the queue
7. FrontOp(Q) → e, element in Front
8. RearOp(Q) → e, element in Rear back
9 Dr. Belal Murshed
Data Structures and Algorithms

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

Queues – Basic Operation


front
rear

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

Queues – Basic Operation


• Create_Queue () → Q, Allocation, [Storage
I Structure] front

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.

12 Dr. Belal Murshed


Data Structures and Algorithms

Queues – Basic Operation


front
• Enqueue(e,Q) → Q, update rear
I • Inserts an element at the end of the queue
• O(1)
T front rear
2 70 60 50 40 30
• enqueue (80,Q)
0
8 front rear
70 60 50 40 30 80

13 Dr. Belal Murshed


Data Structures and Algorithms

Queues – Basic Operation


front

• 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

Queues – Basic Operation


front
• FrontOp(Q) → e
rear
I • Return the element from the front of the queue
without removing it
T • Return Q[front]
2 • O(1) front rear

0 70 60 50 40 30
• FrontOp(Q) → e
8
e= 70 front rear
70 60 50 40 30

15 Dr. Belal Murshed


Data Structures and Algorithms

Queues – Basic Operation


front
• RearOp(Q) → e rear
I • Return the element from the rear of the queue
without removing it
T • Return Q[rear]
2 • O(1) front rear

0 70 60 50 40 30
• RearOp(Q) → e
8
e= 30 front rear
70 60 50 40 30

16 Dr. Belal Murshed


Data Structures and Algorithms

Queues – Basic Operation


front
• isEmpty
rear
I • Checks to see if the queue is empty
• O(1)
T rear front

2
• isEmpty ()
0
8 Front = 0 True

otherwise False

17 Dr. Belal Murshed


Data Structures and Algorithms

Queues – Basic Operation


front
• isFull
rear
I • Checks to see if the queue is full
• O(1)
T
front rear
2
• isFull () 70 60 50 40 30
0
8 Rear = maxSize True

otherwise False

18 Dr. Belal Murshed


Data Structures and Algorithms

Queues – Basic Operation


front
• Clear_Queue rear
I • Remove all values from the queue
• O(n)
T
2 front rear
• clear_Queue ()
0 70 60 50 40 30
8

front rear
19 Dr. Belal Murshed
Data Structures and Algorithms

Queues
Q
– Basic Operation
START: 2 4 6 8 10 Sequence of operations

I front rear Time Operation


1 enqueue(12)
time 1: 2 4 6 8 10 12
T 2 dequeue
3 enqueue(14)
2 time 2: 4 6 8 10 12 4 enqueue(16)
5 dequeue

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

Dr. Belal Murshed


Data Structures and Algorithms

Queues – Basic Operation


front

• enqueue Modify the content rear

I • dequeue Modify the content


T • peek Do not modify
2 • isEmpty Do not modify
0 • isfull Do not modify
• RearOp Do not modify
8
• frontOp Do not modify
• clear Modify the content
21 Dr. Belal Murshed
Data Structures and Algorithms

I
T
2 QUEUES
0 Implementation Logic
8 “Brute Force” method
Front fixed & Rear moveable

22 Dr. Belal Murshed


Data Structures and Algorithms

Implementation – “Brute Force” method

• Real Time approach


I • Service desk doesn’t move
T oAfter customer at front served
2 oHe will leave the queue
oAll customers have to take one step ahead
0
8

23 Dr. Belal Murshed


Data Structures and Algorithms

Implementation – “Brute Force” method


Q
Sequence of operations
start: 2 4 6 8 10

I front rear Time Operation


1 dequeue
time 1: 4 6 8 10
2 enqueue(12)
T front rear
3 dequeue
time 2: 4 6 8 10 12 4 dequeue
2 front rear
5 dequeue
6 dequeue
0 time 3: 6 8 10 12 7 dequeue

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

Implementation – “Brute Force” method


Q
Sequence of operations
start: 2 4 6 8 10
I front rear Time Operation
1 dequeue
T 2 enqueue(12)
time 5: 10 12 3 dequeue
2 front rear
4 dequeue
5 dequeue
6 dequeue
0 time 6: 12 7 dequeue
front/rear
8
time 7:
front rear

25 Dr. Belal Murshed


Data Structures and Algorithms

Implementation – “Brute Force” method

• Insertion will be at rear


I oNo shifting required rear
front
T oO(1) 70 60 50

2 • Removal will be from front


oshifting required
0 oO(n)
60 50
8
front rear

26 Dr. Belal Murshed


Data Structures and Algorithms

I
T
2 QUEUES
0 Implementation Logic
8 “Unrealistic” method
Front & Rear moveable

27 Dr. Belal Murshed


Data Structures and Algorithms

Implementation – “Unrealistic” method

• 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

28 Dr. Belal Murshed


Data Structures and Algorithms

Implementation – “Unrealistic” method


Q
start: 2 4 6 8 10 Sequence of operations
I front rear Time Operation
1 dequeue
T time 1: 4 6 8 10
2 enqueue(12)
3 dequeue
4 dequeue
2 front rear
5 dequeue
time 2: 4 6 8 10 12 6 dequeue
0 front rear
7 dequeue

8 time 3: 6 8 10 12 Notice that the


front rear queue now appears
to be full even
time 4: 8 10 12
though there are
front rear locations available
29 in theDr.array!
Belal Murshed
Data Structures and Algorithms

Implementation – “Unrealistic” method


Q
Sequence of operations
start: 2 4 6 8 10 Time Operation
I front rear 1 dequeue
2 enqueue(12)
T time 5: 10 12 3 dequeue
4 dequeue
front rear 5 dequeue
2 6 dequeue
7 dequeue
time 6: 12
0 front/rear
Even Worse:
8 The queue now
time 7: appears full even
rear front though there are
NO items in the
array!
30 Dr. Belal Murshed
Data Structures and Algorithms

Implementation – “Unrealistic” method


• Insertion will be at rear
oNo shifting required
I oIncrement in rear
Q 2 4 6 8 10
T oO(1)
front rear
• Removal will be from front
2 oNo shifting required
0 oIncrement in front
oO(1)
8 • Problem
oSpace utilization is not optimal
oWasted cell
31 Dr. Belal Murshed
Data Structures and Algorithms

I
T
2 QUEUES
0
8
Implementation Logic
Circular Arrays

32 Dr. Belal Murshed


Data Structures and Algorithms

Implementation – using “Circular Array”


• Common way of implementing an array-based
queue
I
• It is a regular array
T oIncrements will be made circular
oIt will behave like circular array
2 • Queue Empty front
0 ofront== 0 4

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

Implementation – using “Circular Array”


front

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

Implementation – using “Circular Array”


front
2 4 8 6 before 2
front rear
I after
4
2 4 8 6 10
T front rear
10 8
2 normal array implementation
rear 6

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

Enqueue element 10 Dr. Belal Murshed


Data Structures and Algorithms

Implementation – using “Circular Array”


2 4 8 6 10 before
front
front rear
I 4
4 8 6 10 after

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

Implementation – using “Circular Array”


4 8 6 10 before
front
front rear
I rear
18 4
4 8 6 10 18 after

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

Enqueue element 18 Dr. Belal Murshed


Data Structures and Algorithms

Implementation – using “Circular Array”


4 8 6 10 18 before

front rear rear


I after 18
8 6 10 18
T front rear

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

dequeue Dr. Belal Murshed


Data Structures and Algorithms

Implementation – using “Circular Array”


8 6 10 18 before

I front rear rear


18
6 10 18 after
T front rear
10
2 normal array implementation
6

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

Implementation – using “Circular Array”


6 10 18 before

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

dequeue Dr. Belal Murshed


Data Structures and Algorithms

Implementation – using “Circular Array”


rear

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

Implementation – using “Circular Array”


rear
10 18 9 before 9
front rear
I after 18
18 9
T front rear front

normal array implementation


2
0 10 18 9 before

front rear
visualization of a queue
8 implemented as a circular array
18 9 after
after dequeue operation
front rear
circular array implementation

dequeue Dr. Belal Murshed


Data Structures and Algorithms

Implementation – using “Circular Array”


18 9 before rear
front
I front rear 9

9 after
T front/rear

2 normal array implementation

0 18 9 before

8 front rear
visualization of a queue
9 implemented as a circular array
after

front/rear after dequeue operation


circular array implementation
dequeue Dr. Belal Murshed
Data Structures and Algorithms

Implementation – using “Circular Array”


9 before

front/rear rear
I front
after

T front/rear

2 normal array implementation

0 9 before

8 front/rear

after visualization of a queue


front/rear implemented as a circular array
circular array implementation after dequeue operation
dequeue Dr. Belal Murshed
Data Structures and Algorithms

I
T
Queues
2 Implementation
0
8
Arrays

45 Dr. Belal Murshed


Data Structures and Algorithms

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

• Return the value at front


2 o Deletion o isEmpty
• front== 0
• first, last,
0 Anywhere
o isFull
 For Normal Queue
o rear== maxSize
8 o Traversal  For Circular Queue
o Searching o (rear % maxsize+1 == front)
or
o Sorting o (front == 1 && rear == maxsize)
o Merging o clear
• Remove all values of queues
46 Dr. Belal Murshed
Data Structures and Algorithms

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

47 Dr. Belal Murshed


Data Structures and Algorithms

Queues Array - Methods


• bool isEmpty()

I • bool isFull()

T • void enqueue(int e)

2 • int dequeue()

0 • int peek()

8 • int rearOp()

• int rearOp()

48 • void clear() Dr. Belal Murshed


Data Structures and Algorithms

Queues Methods - isEmpty()


Algorithm: isEmpty() { Algorithm: isEmpty()
Method: Method:
I if (Front== 0) return (front == 0);
return true; Algend
T else
return false;
2 Algend

0
8

49 Dr. Belal Murshed


Data Structures and Algorithms

Queues Methods - isEmpty()


Algorithm: isEmpty()
Method:
if (Front== 0) Q
I return true; maxSize 5
else rear

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

50 False Dr. Belal Murshed


Data Structures and Algorithms

Queues Methods - isFull()


Normal Queue
Algorithm: isFull()
method:
I if rear == maxSize then
return true; //print “Queue is Full"
T else
return false;
Algend
2
Circular Queue
0 Algorithm: isFull()
method:
8 if (rear % maxsize+1 == front) or
(front == 1 && rear == maxsize) Then
return true //printf("\nQueue is Full");
else
return false
51 Algend Dr. Belal Murshed
Data Structures and Algorithms

Queues Methods - isFull()


Q
Algorithm: isFull()
I return (rear== maxSize); queue
maxSize 5

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

52 False Dr. Belal Murshed


Data Structures and Algorithms

Queues Methods - enqueue(e)


queue

• 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

Queues Methods - enqueue(e)


Q
I queue
maxSize 5

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

55 Dr. Belal Murshed


Data Structures and Algorithms

Queues Methods - dequeue()


queue

• 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

2 • Circular increment in “front” front = 2%5+1


o front = front % maxSize+1; front = 2+1 = 3
0 queue

8 0 1 2 3 4
10
front 3 rear 3

56 Dr. Belal Murshed


Data Structures and Algorithms
Queues Methods - dequeue()
Algorithm: dequeue
Input: Q, Queue
Output: e, element to be deleted
Method:
I if isempty(Q) then
Queue is empty “No Deletion is possible”
T else
e = Q[front]
2 if front == rear then
rear = 0
0 front = 0
else
8 front = front % maxsize+1
ifend
ifend
AlgEnd
57 Dr. Belal Murshed
Data Structures and Algorithms

Queues Methods - dequeue()


Q
I queue
maxSize 5

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

Queues Methods - peek()


• Circular Array
I
T • Check the queue is empty or not
2
0 • Value will be returned from “front”
8

59 Dr. Belal Murshed


Data Structures and Algorithms

Queues Methods - peek()

I Algorithm :peek( )
Method
T if isEmpty() then
2 queue is empty
else
0 return queue[front]
ifend
8
Algend

60 Dr. Belal Murshed


Data Structures and Algorithms

Queues Methods - peek()


Algorithm :peek( )
Method
if isEmpty() then Q
I queue is empty
else queue
maxSize 5

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

Queues Methods - clear()


• Circular Array
I
T • Check the queue is empty or not
2
0 • Apply “dequeue” repeatedly until queue
8 become empty

62 Dr. Belal Murshed


Data Structures and Algorithms

Queues Methods - clear()

I Algorithm clear( )
Method:
T while !isEmpty() do
2 int e = dequeue()
print e
0 whilend
Algend
8

63 Dr. Belal Murshed


Data Structures and Algorithms

Queues Methods - clear() Q


Algorithm clear( ) queue
maxSize 5

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

Determining the Size of Circular Queue


• Size of Circular Queue
Rules
I • If Rear > front
T oThen the size of circular array is
Size= rear-front+1
2 • If front>R then
oThen the size of circular array is
0 Size = maxsize-(front-rear-1)
8 • = maxsize+rear-front+1
• If Rear== front
o Size=1

65 Dr. Belal Murshed


Data Structures and Algorithms

Determining the Size of Circular Queue


Size of Circular Queue
Rules
I 1) If Rear > front
o Then the size of circular array is
T Number of element present(N) in Queue= rear-front+1
2 2) If front>R then
o Then the size of circular array is
0 Number of element present(N) in Queue =
= maxsize-(front-rear-1)
8 = maxsize+rear-front+1
1) If Rear== front
o Number of element present(N) in Queue = rear-front+1=1

66 Dr. Belal Murshed


Data Structures and Algorithms

Determining the Size of Circular Queue

• Suppose a queue of size 40 is implemented


I using a circular array. How many elements
are in the queue of the front is index 38
T and the rear is index 9?
2 solution
0 ofront>rear then
8 oNumber of element present in Queue(N) =
maxsize+rear-front+1
= 40+9-38+1 = 12
67
o N=12 Dr. Belal Murshed
Data Structures and Algorithms

Determining the Size of Circular Queue

• Consider a modular implementation of


I Queue data structure which is of maximum
Capacity 10. Compute the number of data
T elements present in the queue when :
2 1. rear = 3 , front =1
0 2. rear = 2, front = 8
3. rear = 8 , front = 8
8 4. rear = 10, front = 1
5. rear = 0, front = 0
Solution
68 Dr. Belal Murshed
Data Structures and Algorithms

Determining the Size of Circular Queue

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

Number of element present in Queue (N) = 3


69 Dr. Belal Murshed
Data Structures and Algorithms

Determining the Size of Circular Queue

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

Determining the Size of Circular Queue

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

71 Number of element present in Queue (N) = 1Dr. Belal Murshed


Data Structures and Algorithms

Determining the Size of Circular Queue

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

8 There is not any element present in Queue So this case is


exception

72 Dr. Belal Murshed


Data Structures and Algorithms

I
T
2
0
8

73 Dr. Belal Murshed

You might also like