Unit Vi (FDS)
Unit Vi (FDS)
[210242]
Prof. K. S. Patil
Professor
Dept. Of Computer Enginerring,
SKNSITS, Lonavala
Kspatil.sknsits@sinhgad.edu
1
Prof. K. S. Patil. Department of Computer Engg, SKNSITS, Lonavala
Unit-VI: QUEUE
Contents: Basic concept, stack Abstract Data Type, Representation of Stacks Using Sequential Organization,
stack operations, Multiple Stacks, Applications of Stack- Expression Evaluation and Conversion, Polish notation
and expression conversion, Need for prefix and postfix expressions, Postfix expression evaluation, Linked Stack
and Operations.
Recursion- concept, variants of recursion- direct, indirect, tail and tree, backtracking algorithmic strategy, use of
stack in backtracking.
Unit Objectives and outcomes: On completion the students will be able to:
1. Understand the queue
2. Understand the Recursion
Outcome Mapping:
PEOs: POs: COs: PSOs:
Books used
•T1: Horowitz and Sahani, “Fundamentals of Data Structures in C++”, University Press, ISBN 10: 0716782928 ISBN
13: 9780716782926.
•T2: Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser, “Data Structures and
•Algorithms in Python”, Wiley Publication, ISBN: 978-1-118-29027-9.
Prof. K. S. Patil. Department of Computer Engg, SKNSITS, Lonavala
Introduction of Queue
“A queue is an ordered list in which all insertions are
done at one end, called rear and deletions at another end
called front“
system simultaneously
6-29
Prof. K. S. Patil. Department of Computer Engg, SKNSITS, Lonavala
Conceptual Example of a Circular Queue
front
After 5
1 After 7 enqueues 1 dequeues
0 0
front
12 12
11 11
rear rear
10 10
rear
1
0
front
12
11
After 8 more enqueues
10
6-30
Prof. K. S. Patil. Department of Computer Engg, SKNSITS, Lonavala
Implementation of a
Queue
3 2 3
1 4
front queue 5
cq 0
8 5 n 6
rear count - 7
1
n-3
n 8
- 9
10
.
2
.
6-31
Prof. K. S. Patil. Department of Computer Engg, SKNSITS, Lonavala
A Queue Straddling
the End of a Circular
98
Array 2 3
1 4
front queue 5
cq 0
2 4 99 6
rear count 98 7
97 8
9
10
.
.
6-32
Prof. K. S. Patil. Department of Computer Engg, SKNSITS, Lonavala
Circular Queue Drawn
Linearly Queue from previous slide
98
cq front queue
0 1 2 3 4 96 97 98 99
2 4 …
rear count
6-33
Prof. K. S. Patil. Department of Computer Engg, SKNSITS, Lonavala
Circular Array Implementation
• When an element is enqueued,
the value of rear is
incremented
• But it must take into account
the need to loop back to index
0:
• rear = (rear+1) %
queue.length;
• Can this array implementation
also reach capacity? 6-34
Prof. K. S. Patil. Department of Computer Engg, SKNSITS, Lonavala
Example: array of length 4
What happens?
0 1 2 3
cq front
queue Suppose we try to add one
1 3
more item to a queue
rear count implemented by an array of
length 4
0 1 2 3
2 The queue is now full. How
can you tell?
front
cq
2
queue 4
rear count
6-35
Prof. K. S. Patil. Department of Computer Engg, SKNSITS, Lonavala
Need to expand
capacity… 0 1 2 3
We can’t just double the
2
size of the array and copy
values to the same
cq front
queue positions as before:
2 4 circular properties of the
rear count queue will be lost
0 1 2 3 4 5 6 7
2
front
cq
2
queue 4
These locations should
rear count be in use
6-36
Prof. K. S. Patil. Department of Computer Engg, SKNSITS, Lonavala
We could build the new array, and copy the queue elements into contiguous
locations beginning at location front:
0 1 2 3 4 5 6 7
2
front queue
cq
6 4
rear count
6-37
Prof. K. S. Patil. Department of Computer Engg, SKNSITS, Lonavala
Better: copy the queue elements in order to the beginning of the new
array
0 1 2 3 4 5 6 7
0
front queue
cq
4 4
rear count
6-38
Prof. K. S. Patil. Department of Computer Engg, SKNSITS, Lonavala
New element is added at rear = (rear+1) % queue.length
See expandCapacity() in CircularArrayQueue.java
0 1 2 3 4 5 6 7
0
front queue
cq
5 5
rear count
6-39
Prof. K. S. Patil. Department of Computer Engg, SKNSITS, Lonavala
Circular Queue