1 / 44

Computer Science 1 (Studio) 07B-Queues

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

Télécharger la présentation

Computer Science 1 (Studio) 07B-Queues

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Science 1 (Studio) 07B-Queues 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 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 Queue back front C G D B A H F Queue E J H G F E D C B A I Queue J I H G F E D C B A 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 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 back front B A “Enque B” B A 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 back front B A “Deque A” B A 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 J I H G F E D C B back front 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 J I H G F E D C B back front 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) J I H G F E D C B back front Queue from Front to Back: B, C, D, E, F, G, H, I, J CS1s - 07B-Queues (v1.00)

  10. Other Queue Operations • Give me a reverse queue • Make a copy of a queue J I H G F E D C B B C D E F G H I J back front back front J I H G F E D C B J I H G F E D C B back front back front 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 J I H G F E D C B B C D E F G H I J back front back front Equal queues: false J I H G F E D C B Front Element: B back front CS1s - 07B-Queues (v1.00)

  12. Other Queue Operations • What is the back element in the queue? • Does the queue contain a specific element? J I H G F E D C B Back Element: J back front Queue contains F: true Queue contains X: false J I H G F E D C B back front 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 J I H G F E D C B back front CS1s - 07B-Queues (v1.00)

  14. Double Ended Queues • A double ended queue (deque) adds removal from the back, and insertion at the front insert front remove back J I H G F E D C B remove front insert back back front 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 66 Remove 55 Add 55 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 0 1 2 3 4 size = 5 # elements = 0 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? 0 1 2 3 4 size = 5 # elements = 0 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 0 1 2 3 4 front = 0 size = 5 # elements = 0 back = -1 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 Enque: 10 0 1 2 3 4 10 front = 0 size = 5 # elements = 1 back = 0 CS1s - 07B-Queues (v1.00)

  20. Understanding How To Implement a Queue Enque: 20 0 1 2 3 4 10 20 front = 0 size = 5 # elements = 2 back = 1 CS1s - 07B-Queues (v1.00)

  21. Understanding How To Implement a Queue Enque: 30 0 1 2 3 4 10 20 30 front = 0 size = 5 # elements = 3 back = 2 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 Deque: 10 0 1 2 3 4 20 30 front = 1 size = 5 # elements = 2 back = 2 CS1s - 07B-Queues (v1.00)

  23. Understanding How To Implement a Queue Deque: 20 0 1 2 3 4 30 front = 2 size = 5 # elements = 1 back = 2 CS1s - 07B-Queues (v1.00)

  24. Understanding 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 CS1s - 07B-Queues (v1.00)

  25. Understanding How To Implement a Queue • Continue enqueueing… Enque: 40 0 1 2 3 4 40 front = 3 size = 5 # elements = 1 back = 3 CS1s - 07B-Queues (v1.00)

  26. Understanding How To Implement a Queue Enque: 50 0 1 2 3 4 50 40 front = 3 size = 5 # elements = 2 back = 4 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 Enqueue: 60 0 1 2 3 4 60 50 40 front = 3 size = 5 # elements = 3 back = 0 CS1s - 07B-Queues (v1.00)

  28. Understanding How To Implement a Queue Enque: 70 0 1 2 3 4 60 50 70 40 front = 3 size = 5 # elements = 4 back = 1 CS1s - 07B-Queues (v1.00)

  29. Understanding How To Implement a Queue Deque: 40 0 1 2 3 4 60 50 70 front = 4 size = 5 # elements = 3 back = 1 CS1s - 07B-Queues (v1.00)

  30. Understanding 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 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); 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 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); 0 1 2 3 4 front = 0 size = 5 # elements = 0 back = -1 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 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 Before Enqueue: 60 0 1 2 3 4 50 40 front = 3 size = 5 # elements = 2 back = 4 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: After Enque: 60 0 1 2 3 4 50 40 60 front = 3 size = 5 # elements = 2 back = 0 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 After Enque: 60 Before Enque: 60 0 1 2 3 4 0 1 2 3 4 50 50 40 40 60 front = 3 front = 3 back = 4 back = 0 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 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 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: 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 0 1 2 3 4 50 40 60 front = 3 size = 5 # elements = 2 back = 0 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: 0 1 2 3 4 50 40 60 front = 3 size = 5 # elements = 2 back = 0 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 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 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 CS1s - 07B-Queues (v1.00)

More Related