computer science 1 (studio) 07b-queues
DESCRIPTION
Computer Science 1 (Studio) 07B-Queues. What 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 You’ve already been exposed to several data structures in Java - PowerPoint PPT PresentationTRANSCRIPT
04/19/23 CS1s - 07B-Queues (v1.00) 1
Computer Science 1 (Studio) 07B-Queues
04/19/23 CS1s - 07B-Queues (v1.00) 2
What 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
• You’ve 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
04/19/23 CS1s - 07B-Queues (v1.00) 3
What 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
GABF
CD
EH
Queue
Queue
ABCDEFGH
Queue
ABCDEFGHI
IJ
J
frontback
04/19/23 CS1s - 07B-Queues (v1.00) 4
What is a Queue?
• Examples of real life queues:– Amusement park line– Concert ticket line– Bursar’s 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 mp3’s to download off Kazaa before the RIAA
shuts it down
04/19/23 CS1s - 07B-Queues (v1.00) 5
Queue 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
A
“Enque B”
B
AB
frontback
04/19/23 CS1s - 07B-Queues (v1.00) 6
Queue 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
AB
B A
“Deque A”
frontback
04/19/23 CS1s - 07B-Queues (v1.00) 7
Queue 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
BCDEFGHIJ
frontback
04/19/23 CS1s - 07B-Queues (v1.00) 8
Queue 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
BCDEFGHIJ
frontback
04/19/23 CS1s - 07B-Queues (v1.00) 9
Queue Operations
• We should also be able to ask a queue to print out its current contents (useful for debugging)
BCDEFGHIJ
frontback
Queue from Front to Back:B, C, D, E, F, G, H, I, J
04/19/23 CS1s - 07B-Queues (v1.00) 10
Other Queue Operations
• Give me a reverse queue
• Make a copy of a queue
BCDEFGHIJ
frontback
JIHGFEDCB
frontback
BCDEFGHIJ
frontback
BCDEFGHIJ
frontback
04/19/23 CS1s - 07B-Queues (v1.00) 11
Other Queue Operations
• Are these two queues equal?
• What is the front element in the queue?– We’re peeking at it, not actually dequeing it
BCDEFGHIJ
frontback
JIHGFEDCB
frontback
BCDEFGHIJ
frontback
Front Element: B
Equal queues: false
04/19/23 CS1s - 07B-Queues (v1.00) 12
Other Queue Operations
• What is the back element in the queue?
• Does the queue contain a specific element?
BCDEFGHIJ
frontback
Back Element: J
BCDEFGHIJ
frontback
Queue contains F: trueQueue contains X: false
04/19/23 CS1s - 07B-Queues (v1.00) 13
Queue Caveats
• A queue contains elements of the same type
• A queue does not support random access– We can’t get at the fourth element from the front of the
queue and change it directly
BCDEFGHIJ
frontback
04/19/23 CS1s - 07B-Queues (v1.00) 14
Double Ended Queues
• A double ended queue (deque) adds removal from the back, and insertion at the front
BCDEFGHIJ
frontback
insert front
insert back remove frontremove back
04/19/23 CS1s - 07B-Queues (v1.00) 15
Other 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 55 Add 66 Remove 55
04/19/23 CS1s - 07B-Queues (v1.00) 16
Understanding How To Implement a Queue
• We’re going to implement our first queue using an array as the underlying collection
• The queue will be circular so that we don’t have to shift elements around
• Let’s look at the mechanics of this before we dive into the implementation
• Consider a queue whose maximum size is 5
size = 5
0 1 2 3 4
# elements = 0
04/19/23 CS1s - 07B-Queues (v1.00) 17
Understanding 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?
size = 5
0 1 2 3 4
# elements = 0
04/19/23 CS1s - 07B-Queues (v1.00) 18
Understanding 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
front = 0
back = -1
size = 5
0 1 2 3 4
# elements = 0
04/19/23 CS1s - 07B-Queues (v1.00) 19
Understanding 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
front = 0
back = 0
size = 5
0 1 2 3 4
# elements = 1
Enque: 10
10
04/19/23 CS1s - 07B-Queues (v1.00) 20
Understanding How To Implement a Queue
front = 0
back = 1
size = 5
0 1 2 3 4
# elements = 2
Enque: 20
10 20
04/19/23 CS1s - 07B-Queues (v1.00) 21
Understanding How To Implement a Queue
front = 0
back = 2
size = 5
0 1 2 3 4
# elements = 3
Enque: 30
10 20 30
04/19/23 CS1s - 07B-Queues (v1.00) 22
Understanding How To Implement a Queue
• Whenever an element is dequed, the element at the front index is returned
• The front index is then incremented
front = 1
back = 2
size = 5
0 1 2 3 4
# elements = 2
Deque: 10
20 30
04/19/23 CS1s - 07B-Queues (v1.00) 23
Understanding How To Implement a Queue
front = 2
back = 2
size = 5
0 1 2 3 4
# elements = 1
Deque: 20
30
04/19/23 CS1s - 07B-Queues (v1.00) 24
Understanding How To Implement a Queue
• The queue is now empty again
front = 3
back = 2
size = 5
0 1 2 3 4
# elements = 0
Deque: 30
04/19/23 CS1s - 07B-Queues (v1.00) 25
Understanding How To Implement a Queue
• Continue enqueueing…
front = 3
back = 3
size = 5
0 1 2 3 4
# elements = 1
Enque: 40
40
04/19/23 CS1s - 07B-Queues (v1.00) 26
Understanding How To Implement a Queue
front = 3
back = 4
size = 5
0 1 2 3 4
# elements = 2
Enque: 50
40 50
04/19/23 CS1s - 07B-Queues (v1.00) 27
Understanding How To Implement a Queue
• On the next enque, the back index needs to wrap around to the start
front = 3
back = 0
size = 5
0 1 2 3 4
# elements = 3
Enqueue: 60
60 40 50
04/19/23 CS1s - 07B-Queues (v1.00) 28
Understanding How To Implement a Queue
front = 3
back = 1
size = 5
0 1 2 3 4
# elements = 4
Enque: 70
60 40 5070
04/19/23 CS1s - 07B-Queues (v1.00) 29
Understanding How To Implement a Queue
front = 4
back = 1
size = 5
0 1 2 3 4
# elements = 3
Deque: 40
60 5070
04/19/23 CS1s - 07B-Queues (v1.00) 30
Understanding How To Implement a Queue
• On the next deque, the front index needs to wrap around
front = 0
back = 1
size = 5
0 1 2 3 4
# elements = 2
Deque: 50
60 70
04/19/23 CS1s - 07B-Queues (v1.00) 31
Implementing 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);
04/19/23 CS1s - 07B-Queues (v1.00) 32
Implementing 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 don’t need to save the size of the array. Once the constructor creates the array, you can tell its size by accesing the arrays length
04/19/23 CS1s - 07B-Queues (v1.00) 33
Implementing 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);
front = 0
back = -1
size = 5
0 1 2 3 4
# elements = 0
04/19/23 CS1s - 07B-Queues (v1.00) 34
Implementing a Queue – Accesors
• In Class Activity– The next step is to implement the accessor’s 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
04/19/23 CS1s - 07B-Queues (v1.00) 35
Implementing 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
front = 3
back = 4
size = 5
0 1 2 3 4
# elements = 2
Before Enqueue: 60
40 50
04/19/23 CS1s - 07B-Queues (v1.00) 36
Implementing 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:
front = 3
back = 0
size = 5
0 1 2 3 4
# elements = 2
After Enque: 60
40 5060
04/19/23 CS1s - 07B-Queues (v1.00) 37
Implementing 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
front = 3
back = 4
0 1 2 3 4
Before Enque: 60
40 50
front = 3
back = 0
0 1 2 3 4
40 5060
After Enque: 60
04/19/23 CS1s - 07B-Queues (v1.00) 38
Implementing a Queue - Deque
• With deque, the element must be added into the array before incrementing the front index
front = 1
back = 2
0 1 2 3 4
Before deque: 20
20 30
front = 2
back = 2
0 1 2 3 4
After deque: 20
30
04/19/23 CS1s - 07B-Queues (v1.00) 39
Implementing a Queue - Preconditions
• There’s 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:
04/19/23 CS1s - 07B-Queues (v1.00) 40
Implementing 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
front = 3
back = 0
0 1 2 3 4
40 5060
size = 5
# elements = 2
04/19/23 CS1s - 07B-Queues (v1.00) 41
Implementing a Queue - Print
• Here we want to print out indexes 3, 4 and 5:myQueue[3]
myQueue[4]
myQueue[0]
• Write down the algorithm here:
front = 3
back = 0
0 1 2 3 4
40 5060
size = 5
# elements = 2
04/19/23 CS1s - 07B-Queues (v1.00) 42
Implementing 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– Don’t 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 doesn’t blow up
04/19/23 CS1s - 07B-Queues (v1.00) 43
Implementing 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
04/19/23 CS1s - 07B-Queues (v1.00) 44
Revision History
• Revision History– v1.00, 10/21/2004 11:42 AM, sps
Initial revision.-- v2.00, 10/24/2005, chr Minor correction