Computer Science 1 Studio 07 BQueues 9292020 CS
Computer Science 1 (Studio) 07 B-Queues 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) 1
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 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) 2
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 G DC H F B E Queue back Queue J I front A HGF E DC B A Queue J I HGF E DC B 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) A 3
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 mp 3’s to download off Kazaa before the RIAA shuts it down 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) 4
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 back B front A “Enque B” B A 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) 5
Queue Operations - Deque • The process of removing an element from the front of the queue is known as dequeing back front B A “Deque A” B A • The visual effect of dequeing is that the front element disappears and all other elements in the queue shift forward a slot 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) 6
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: J IHGF E DC B back front • We can ask a queue what its size is • We can ask a queue how many elements it has in it 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) 7
Queue Operations • We can also ask a queue if it is full or empty J IHGF E DC B back front • 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 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) 8
Queue Operations • We should also be able to ask a queue to print out its current contents (useful for debugging) J IHGF E DC B back front Queue from Front to Back: B, C, D, E, F, G, H, I, J 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) 9
Other Queue Operations • Give me a reverse queue J IHGF E DC B back B CD E F G H I J front back front • Make a copy of a queue J IHGF E DC B back 9/29/2020 J IHGF E DC B front back CS 1 s - 07 B-Queues (v 1. 00) front 10
Other Queue Operations • Are these two queues equal? J IHGF E DC B back front B CD E F G H I J back front Equal queues: false • What is the front element in the queue? – We’re peeking at it, not actually dequeing it J IHGF E DC B back 9/29/2020 Front Element: B front CS 1 s - 07 B-Queues (v 1. 00) 11
Other Queue Operations • What is the back element in the queue? J IHGF E DC B back Back Element: J front • Does the queue contain a specific element? J IHGF E DC B back 9/29/2020 Queue contains F: true Queue contains X: false front CS 1 s - 07 B-Queues (v 1. 00) 12
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 J IHGF E DC B back 9/29/2020 front CS 1 s - 07 B-Queues (v 1. 00) 13
Double Ended Queues • A double ended queue (deque) adds removal from the back, and insertion at the front remove back insert back J IHGF E DC B back 9/29/2020 insert front remove front CS 1 s - 07 B-Queues (v 1. 00) 14
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 9/29/2020 Add 66 CS 1 s - 07 B-Queues (v 1. 00) Remove 55 15
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 0 1 2 3 4 size = 5 # elements = 0 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) 16
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? 0 1 2 3 4 size = 5 # elements = 0 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) 17
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 0 1 2 3 4 front = 0 size = 5 # elements = 0 back = -1 • Initially the back of the queue is at index -1 and the front of the queue is at index 0 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) 18
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 Enque: 10 0 1 2 3 4 10 front = 0 size = 5 # elements = 1 back = 0 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) 19
Understanding How To Implement a Queue Enque: 20 0 1 10 20 2 3 4 front = 0 size = 5 # elements = 2 back = 1 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) 20
Understanding How To Implement a Queue Enque: 30 0 1 2 10 20 30 3 4 front = 0 size = 5 back = 2 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) # elements = 3 21
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 Deque: 10 0 1 2 20 30 3 4 front = 1 size = 5 back = 2 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) # elements = 2 22
Understanding How To Implement a Queue Deque: 20 0 1 2 3 4 30 front = 2 back = 2 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) size = 5 # elements = 1 23
Understanding How To Implement a Queue • The queue is now empty again Deque: 30 0 1 2 3 4 front = 3 back = 2 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) size = 5 # elements = 0 24
Understanding How To Implement a Queue • Continue enqueueing… Enque: 40 0 1 2 3 4 40 front = 3 back = 3 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) size = 5 # elements = 1 25
Understanding How To Implement a Queue Enque: 50 0 1 2 3 4 40 50 front = 3 back = 4 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) size = 5 # elements = 2 26
Understanding How To Implement a Queue • On the next enque, the back index needs to wrap around to the start Enqueue: 60 0 1 60 2 3 4 40 50 front = 3 # elements = 3 back = 0 9/29/2020 size = 5 CS 1 s - 07 B-Queues (v 1. 00) 27
Understanding How To Implement a Queue Enque: 70 0 1 60 70 2 3 4 40 50 front = 3 # elements = 4 back = 1 9/29/2020 size = 5 CS 1 s - 07 B-Queues (v 1. 00) 28
Understanding How To Implement a Queue Deque: 40 0 1 60 70 2 3 4 50 front = 4 # elements = 3 back = 1 9/29/2020 size = 5 CS 1 s - 07 B-Queues (v 1. 00) 29
Understanding How To Implement a Queue • On the next deque, the front index needs to wrap around Deque: 50 0 1 60 70 2 3 4 front = 0 size = 5 # elements = 2 back = 1 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) 30
Implementing a Queue - Javadoc • Look at the javadoc for the queue class we are implementing: – http: //www. cs. rit. edu/~cs 1 s/week 7/Int. Queue. html • The queue will only work with integers and it will be called Int. Queue • The size of the queue will be based on how Int. Queue is constructed // If no size is specified, the default size is 25 Int. Queue q 1 = new Int. Queue(); // Here the size is 10 Int. Queue q 2 = new Int. Queue(10); 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) 31
Implementing a Queue – First Step • In Class Activity – Start by creating a class, Int. Queue. 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 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) 32
Implementing a Queue – First Step • The end result of your code so far should be to create the following diagram in memory: Int. Queue my. Q = new Queue (5); 0 1 2 3 4 front = 0 size = 5 # elements = 0 back = -1 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) 33
Implementing a Queue – Accesors • In Class Activity – – – The next step is to implement the accessor’s and simple methods Implement get. Number. Of. Elements() Implement get. Size() Implement is. Full() Implement is. Empty() – Once this is complete, write another program, Test. Queue. java, which tests the creation and accessors for Int. Queue 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) 34
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 Before Enqueue: 60 0 1 2 3 4 40 50 front = 3 back = 4 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) size = 5 # elements = 2 35
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: After Enque: 60 0 1 60 2 3 4 40 50 front = 3 # elements = 2 back = 0 9/29/2020 size = 5 CS 1 s - 07 B-Queues (v 1. 00) 36
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 After Enque: 60 Before Enque: 60 0 1 2 3 4 40 50 0 1 60 front = 3 back = 4 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) 2 3 4 40 50 front = 3 back = 0 37
Implementing a Queue - Deque • With deque, the element must be added into the array before incrementing the front index Before deque: 20 0 After deque: 20 1 2 20 30 3 4 1 2 3 4 30 front = 1 front = 2 back = 2 9/29/2020 0 CS 1 s - 07 B-Queues (v 1. 00) back = 2 38
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: 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) 39
Implementing the Queue - Print • For debugging purposes, we need a way to print out the contents of the queue • Consider this example: 0 1 60 2 3 4 40 50 front = 3 back = 0 size = 5 # elements = 2 • 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 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) 40
Implementing a Queue - Print • Here we want to print out indexes 3, 4 and 5: my. Queue[3] my. Queue[4] my. Queue[0] 0 1 60 2 3 4 40 50 front = 3 back = 0 size = 5 # elements = 2 • Write down the algorithm here: 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) 41
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 print. Que method so the elements that are enqueued can be printout – Finally, in your Test. Queue 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 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) 42
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 Test. Queue 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 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) 43
Revision History • Revision History – v 1. 00, 10/21/2004 11: 42 AM, sps Initial revision. -- v 2. 00, 10/24/2005, chr Minor correction 9/29/2020 CS 1 s - 07 B-Queues (v 1. 00) 44
- Slides: 44