Title: Computer Science 1 (Studio) 07B-Queues
1Computer Science 1 (Studio) 07B-Queues
2What is a Data Structure?
- In computer science, a data structure is a way of
storing data so that it can be used efficiently
by an algorithm - Youve already been exposed to several data
structures in Java - Classes are collections of attributes and
behaviors which represent an object - Arrays are a contiguous collection of similar
primitives/objects - The choice of data structure in a program is of
primary importance because it effects difficulty
of implementation and performance
3What is a Queue?
- A queue is a data structure which has the unique
characteristic of being a First In First Out
collection - Another characteristic of a queue is that, in
theory, it has infinite capacity - Regardless of how many elements are currently
present in the queue, another can always be added
Queue
front
back
C
G
D
A
B
H
F
Queue
E
J
A
B
C
D
E
F
G
H
I
Queue
A
B
C
D
E
F
G
H
I
J
4What is a Queue?
- Examples of real life queues
- Amusement park line
- Concert ticket line
- Bursars office line
- Examples of queues in computer applications
- Processes or users waiting for a system resource
- A printing queue for spooling print jobs
- A queue of mp3s to download off Kazaa before the
RIAA shuts it down
5Queue Operations - Enque
- A queue supports two fundamental operations for
storing and retrieving data elements - The process of adding an element to the end of a
queue is known as enqueing
front
back
A
B
Enque B
A
B
6Queue Operations - Deque
- The process of removing an element from the front
of the queue is known as dequeing - The visual effect of dequeing is that the front
element disappears and all other elements in the
queue shift forward a slot
front
back
A
B
Deque A
B
A
7Queue Operations
- In practice, most queues are implemented with a
maximum size - A queue of size 10 which currently has 9 elements
stored in it - We can ask a queue what its size is
- We can ask a queue how many elements it has in it
B
C
D
E
F
G
H
I
J
front
back
8Queue Operations
- We can also ask a queue if it is full or empty
- Describe a scenario where you want to know if the
queue is currently full - Describe another scenario where you want to know
if the queue is currently empty
B
C
D
E
F
G
H
I
J
front
back
9Queue Operations
- We should also be able to ask a queue to print
out its current contents (useful for debugging)
B
C
D
E
F
G
H
I
J
front
back
Queue from Front to Back B, C, D, E, F, G, H, I,
J
10Other Queue Operations
- Give me a reverse queue
- Make a copy of a queue
B
C
D
E
F
G
H
I
J
J
I
H
G
F
E
D
C
B
front
back
front
back
B
C
D
E
F
G
H
I
J
B
C
D
E
F
G
H
I
J
front
back
front
back
11Other Queue Operations
- Are these two queues equal?
- What is the front element in the queue?
- Were peeking at it, not actually dequeing it
B
C
D
E
F
G
H
I
J
J
I
H
G
F
E
D
C
B
front
back
front
back
Equal queues false
B
C
D
E
F
G
H
I
J
Front Element B
front
back
12Other Queue Operations
- What is the back element in the queue?
- Does the queue contain a specific element?
B
C
D
E
F
G
H
I
J
Back Element J
front
back
Queue contains F true Queue contains X false
B
C
D
E
F
G
H
I
J
front
back
13Queue Caveats
- A queue contains elements of the same type
- A queue does not support random access
- We cant get at the fourth element from the front
of the queue and change it directly
B
C
D
E
F
G
H
I
J
front
back
14Double Ended Queues
- A double ended queue (deque) adds removal from
the back, and insertion at the front
insert front
remove back
B
C
D
E
F
G
H
I
J
remove front
insert back
front
back
15Other Queues
- A priority queue constantly organizes the
elements in the queue based on increasing
priority - A circular queue avoids shifting by repositioning
the front and back locations
Add 66
Remove 55
Add 55
16Understanding How To Implement a Queue
- Were going to implement our first queue using an
array as the underlying collection - The queue will be circular so that we dont have
to shift elements around - Lets look at the mechanics of this before we
dive into the implementation - Consider a queue whose maximum size is 5
0
1
2
3
4
size 5
elements 0
17Understanding How To Implement a Queue
- Based on this information, how can we tell when
the queue is full or empty? - How can we tell what the size of the queue is?
- How can we tell how many elements are in the
queue?
0
1
2
3
4
size 5
elements 0
18Understanding How To Implement a Queue
- In order to track the front and the back of the
queue as elements get enqued and dequed, we will
rely on integer indexes - Initially the back of the queue is at index -1
and the front of the queue is at index 0
0
1
2
3
4
front 0
size 5
elements 0
back -1
19Understanding How To Implement a Queue
- Whenever an element is enqueued, the back index
is incremented - The element is then added into the array at the
back index
Enque 10
0
1
2
3
4
10
front 0
size 5
elements 1
back 0
20Understanding How To Implement a Queue
Enque 20
0
1
2
3
4
10
20
front 0
size 5
elements 2
back 1
21Understanding How To Implement a Queue
Enque 30
0
1
2
3
4
10
20
30
front 0
size 5
elements 3
back 2
22Understanding How To Implement a Queue
- Whenever an element is dequed, the element at the
front index is returned - The front index is then incremented
Deque 10
0
1
2
3
4
20
30
front 1
size 5
elements 2
back 2
23Understanding How To Implement a Queue
Deque 20
0
1
2
3
4
30
front 2
size 5
elements 1
back 2
24Understanding How To Implement a Queue
- The queue is now empty again
Deque 30
0
1
2
3
4
front 3
size 5
elements 0
back 2
25Understanding How To Implement a Queue
Enque 40
0
1
2
3
4
40
front 3
size 5
elements 1
back 3
26Understanding How To Implement a Queue
Enque 50
0
1
2
3
4
40
50
front 3
size 5
elements 2
back 4
27Understanding How To Implement a Queue
- On the next enque, the back index needs to wrap
around to the start
Enqueue 60
0
1
2
3
4
60
50
40
front 3
size 5
elements 3
back 0
28Understanding How To Implement a Queue
Enque 70
0
1
2
3
4
60
50
40
70
front 3
size 5
elements 4
back 1
29Understanding How To Implement a Queue
Deque 40
0
1
2
3
4
60
50
70
front 4
size 5
elements 3
back 1
30Understanding How To Implement a Queue
- On the next deque, the front index needs to wrap
around
Deque 50
0
1
2
3
4
60
70
front 0
size 5
elements 2
back 1
31Implementing a Queue - Javadoc
- Look at the javadoc for the queue class we are
implementing - http//www.cs.rit.edu/cs1s/week7/IntQueue.html
- The queue will only work with integers and it
will be called IntQueue - The size of the queue will be based on how
IntQueue is constructed - // If no size is specified, the default size is
25 - IntQueue q1 new IntQueue()
- // Here the size is 10
- IntQueue q2 new IntQueue(10)
32Implementing a Queue First Step
- In Class Activity
- Start by creating a class, IntQueue.java
- Using the javadoc, make a compilable skeleton
- Remember to return dummy values for methods that
require them - Write the code for the two constructors
- You will have to create several private data
members to achieve this - An integer array reference variable (the actual
collection) - The number of elements currently in the queue
- The front index
- The back index
- A constant for the default size of the queue
(default 25) - You dont need to save the size of the array.
Once the constructor creates the array, you can
tell its size by accesing the arrays length
33Implementing a Queue First Step
- The end result of your code so far should be to
create the following diagram in memory - IntQueue myQ new Queue (5)
0
1
2
3
4
front 0
size 5
elements 0
back -1
34Implementing a Queue Accesors
- In Class Activity
- The next step is to implement the accessors and
simple methods - Implement getNumberOfElements()
- Implement getSize()
- Implement isFull()
- Implement isEmpty()
- Once this is complete, write another program,
TestQueue.java, which tests the creation and
accessors for IntQueue
35Implementing a Queue - Enque
- The final steps for implementing the queue are
writing the methods for enqueing and dequeing
elements - Both of these methods require a way for the
indexes to wrap around when they reach the end of
the array
Before Enqueue 60
0
1
2
3
4
50
40
front 3
size 5
elements 2
back 4
36Implementing a Queue - Enque
- In this example, how can I change the back index
to go from 4 to 0? - Hint you always know the size of the array
- Write down generic algorithm here
After Enque 60
0
1
2
3
4
50
40
60
front 3
size 5
elements 2
back 0
37Implementing a Queue - Enque
- Remember this
- Enqueue works with the back index
- Dequeue works with the front index
- With enque, the back index must be incremented
first before adding the element into the array
After Enque 60
Before Enque 60
0
1
2
3
4
0
1
2
3
4
40
50
40
50
60
front 3
front 3
back 4
back 0
38Implementing a Queue - Deque
- With deque, the element must be added into the
array before incrementing the front index
Before deque 20
After deque 20
0
1
2
3
4
0
1
2
3
4
20
30
30
front 1
front 2
back 2
back 2
39Implementing a Queue - Preconditions
- Theres two preconditions we must worry about
before we can write enqueue and dequeue - What should happen if we try to enque an element
and the array is already full? - Write our solution here
- What should happen if we try to deque an element
and the array is empty? - Write our solution here
40Implementing the Queue - Print
- For debugging purposes, we need a way to print
out the contents of the queue - Consider this example
- We need to create a loop variable which starts at
the front index and moves until all the elements
in the queue have been printed
0
1
2
3
4
50
40
60
front 3
size 5
elements 2
back 0
41Implementing a Queue - Print
- Here we want to print out indexes 3, 4 and 5
- myQueue3
- myQueue4
- myQueue0
- Write down the algorithm here
0
1
2
3
4
40
50
60
front 3
size 5
elements 2
back 0
42Implementing a Queue Enqueue Print
- In Class Activity
- Write the enqueue method
- Be sure to include the precondition discussed
earlier - Remember that the back index must be incremented
before adding the element into the array - Dont forget to change the number of elements in
the queue if the element was added - Next write the printQue method so the elements
that are enqueued can be printout out - Finally, in your TestQueue class, write a test
program which adds elements to the queue and
prints them out - Make sure you check that adding elements to a
full queue doesnt blow up
43Implementing a Queue - Deque
- In Class Activity
- The final activity is to write the deque method
- Be sure to handle the precondition where the
queue is empty (return 0) - Remember that deque uses the front index
- The element must be accessed in the array using
the front index first (before incrementing the
front index) - Write some tests in your TestQueue class which
tests that you can correctly deque elements - Make sure you can handle the case where the queue
is empty and a deque is requested
44Revision History
- Revision History
- v1.00, 10/21/2004 1142 AM, sps
- Initial revision.
- -- v2.00, 10/24/2005, chr
- Minor correction