Upload
gregory-rose
View
217
Download
0
Embed Size (px)
Citation preview
9CM305.31 1
Name :B Vijaya kumari
Designation : Lecturer
Branch :Computer Engineering
Institute :Smt.B.Seetha. Polytechnic, Bhimavaram,
Year/semester :III Sem
Subject :Datastructrues Through C
Sub code : 9CM-305
Topic :stacks& queues
Duration :50 mins
Subtopic :Data structure
Sub topic :Circular queue using array implementation
Teaching aids: :PPT, animations, Photographs
Revised by : Bapuji naik
Department Of Technical Education Andhra Pradesh
9CM305.31 3
Objectives
• On completion of this topic, you would be able to know
• Circular queue
• Operation on circular queue
9CM305.31 4
Recap
• In the previous lesson we have learnt ,
implementation of circular queue using linked list
9CM305.31 5
• Reading queues implementation with array has the drawback
• When an element is inserted , rear pointer is incremented
• Similarly when we delete element, front pointer is incremented
Drawbacks of Queue
9CM305.31 6
• Once we delete the element, there is no way to re use that location of the queue.
In the above diagram, location with index 0 can not be re – used, as the operation manipulations any rear pointer not be front picture.
Drawbacks of QueueContd.,
9CM305.31 7
• The problem can be overcome, if there is a facility to re – use the location indices.
• i.e., Once the maximum value of index in reached for either of front order, nest increment should point to the beginning of the queue.
Drawbacks of QueueContd.,
9CM305.31 8
• For example, if the queue size in 5 the index value range from 0 to 4.
• Once the front of rear value reaches 4, nest of increment of either of them should problem 0.
• This is possible is the queue is arranged circularly
Drawbacks of QueueContd.,
9CM305.31 9
• From the above discussion, it is clean that the location can be re used if we represent the queue circularly
• It make efficient uses if location of queue
• In a circular queue the last element and the first element of the queue are adjacent to each other
Circular queue
9CM305.31 10
2
1
3
4
5 0
Representation of circular queue• diagrammatically circular queue are representation as follows .
The above queue has 6 location, index value range is 0 to 5Once index value is 5, the next index is 0.
9CM305.31 11
Operations on circular queues
• The operations add the element at rear of the queue
• Delete operation deletes an element at front of the queue
9CM305.31 12
• Initially front and rear have 0 value • Diagrammatically ,
2
1
3
4
front rear
All elements are initially 0Also, front = rear= 0
Operations on circular queues Contd.,
5
9CM305.31 13
• By adding an element ‘u‘
Front=0,rear=4
Operations on circular queues Contd.,
front rear
0
1
2
34
5
6
7
u
9CM305.31 14
• By adding some more elements the diagram look like,
In the above diagram , A ,E, S are added to the circular queue. front=0 ,rear=4
Operations on circular queues Contd.,
u
a
e
s
front 0
rear
1
2
3
4
5
6
7
9CM305.31 15
• Both the above scenario if we add two more elements the from are rear value are
front=0, and rear=o
0
2
3
4
1
front
5 p q
u
ae
ss
rear
Operations on circular queues Contd.,
9CM305.31 16
• As, it can be draw ,
• where the queue is empty the value of front are rear
front=rear=0
• Similarly, when the queue is full
front=rear=0
• on queue empty and queue full condition,
front=rear=0
Contd.,Operations on circular queues
9CM305.31 17
• So for add a parathions were discussed • On deleting element from the queue front is incremented
0
2
3
4
1
front
0
0
ea
u
5
rear
front
0
2
3
4
1
0
0
e
a
rear
5
u
Contd.,Operations on circular queues
9CM305.31 18
• Before deletion
• front=0,rear=3
• After deletion
• front=1,rear=3
• The element value at front =1,u ,is a no more usuful
• The location can be re- used later
• Continually with deletion, if the delete some more elements, the queue becomes empty.
• Again, the value of front and rear are
• front=0, rear=0
Contd.,Operations on circular queues
9CM305.31 19
Summary
In this lesson we have learnt • The need for circular queue
• Representation of circular queue
• Operation of circular queue
-add
-delete
• Examples of operator
9CM305.31 20
Quiz
1. Regular queue are more efficient, compared to circular queue in array implementation
a) True
b) false
9CM305.31 21
2. The value of front are rear zero, in case of queue full as well as queue empty
a) True
b) false
QuizContd.,
9CM305.31 22
3. In which situation head and tail pointers are points to same location
a) Queue is full
b) Queue is empty
c) Both a&b
d) None
QuizContd.,
9CM305.31 24
5. If queue is full then
a) Head and tail pointers are points to same location
b) Head and tail pointers are points to different locations
c) a&b
d) none
QuizContd.,
9CM305.31 25
6. If queue is empty then
a) Head and tail pointers are points to different locations
b) Head and tail pointers are points to same location
c) Both head and tail pointers are points to middle point
d) a&c
QuizContd.,
9CM305.31 26
7. In dequeue operation the tail pointer is
a) incremented
b) Decremented
c) a&b
d) None
QuizContd.,
9CM305.31 27
8. In enqueue operation the head pointer is
a) Incremented
a) Decremented
b) Same point
c) b&c
QuizContd.,
9CM305.31 28
Frequently asked questions
1.What is a circular queue?
2.Explain the operations of circular queue?
3.Explain full and empty conditions of a circular queue?