Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CSC 205 Lab 12 : Queues Goals After completing this lab, you should be able to: Understand and be able to use the methods of the Queue ADT. Know some of the general applications of queues. Be able to write simple class methods to count the number of elements in a queue and print out a queue. Understand how to implement a queue using a circular array. Be able to simulate queues using the LinkedList class from the Java Collections Framework. Lab Startup Change into your Labs directory, and let's create and change into a Lab12 directory. Now, let's copy over some files by typing : cp /pub/digh/CSC205/Lab12/* . Building and Tracing a Queue Draw the stack that would be created following the code segment below. Use your Queue ADT handout as a guide. char x, y; Queue q = new Queue(); q.enqueue(new Character('W')); x = ((Character) q.front()).charValue(); q.enqueue(new Character('V')); q.enqueue(new Character(x)); q.dequeue(); y = ((Character) q.front()).charValue(); q.dequeue(); q.enqueue(new Character('T')); q.dequeue(); q.enqueue(new Character(x)); q.enqueue(new Character(y)); x = ((Character) q.front()).charValue(); q.enqueue(new Character(x)); Now, compile and run the MyQueue program to check your results. Writing Simple Class and Instance Methods with Queues Add a value-returning class method named letterCount to your QCount program that can be used to return the total number of letters in a queue. For example, if the queue were : b $ E + I y s # the call letterCount(q)would return the integer 5. Your method will have one parameter, a queue object, and should not modify this parameter. You are writing this method assuming you are a user of the Queue class. Here, you’ll merely be using the methods of the Queue class to achieve your solution, and do not worry or care about the fact that it is really built using a circular array. Your method will need to be sure it throws CloneNotSupportedException. Add a call to your QCount program to test your method when you’re done. Now, let’s switch gears and add a letterCount method to the implementation file of our Queue class. So, within the file Queue.java, add a value-returning instance method named letterCount that returns an integer representing the total number of letters stored in your circular array. Here, we do care about the fact that our queue is built as a circular array. The easiest algorithm here is to set up an index that starts at your front postion, and advances through your array n times where n is the size of your queue. Remember to reset front properly so that it circles back to the beginning. Test your method by invoking a call to letterCount from the q object in your main method. Using the LinkedList class from the Java Collections Framework to Simulate Queues In the Java collections framework, you of course will find a built-in LinkedList class. You will not find a built-in Queue class however. The reason why is that you already have everything you need to simulate a queue using methods from the LinkedList class from the Java Collections Framework. Check out the methods that are available in this class from the API specification at java.sun.com. There are a plethora of wonderful methods available to you in the LinkedList class! Notice that you have methods like addLast() for enqueueing, removeFirst() for dequeueing, get(int index) for retrieving an item at a particular index, etc. You could also easily simulate a deque data structure (pronounced “deck”). This is a data structure of values that allows both insertions and deletions from the front and back. Think of a “deck of cards” or a bus with both entrances and exits at the front and back. 2 Now, take a look at the JCFQueue program that has already been set up for you. Two “queue” objects have already been declared for you. Notice they are both of the type LinkedList. The Check for Identical Queues You will want to complete the body of the method identicalCheck that takes two simulated queue objects of type LinkedList, and returns true or false indicating whether these two objects are the same. Two queues would be identical if they are the same size and they each contain the same items in the same position. So, using the methods of the LinkedList class perform this check. The Search and Replace Method Now, uncomment out the body of the call to the findAndReplace method and the call to it within main. We want to complete the body of this method, which takes a queue object simulated using a LinkedList, and replaces all occurrences of the key object which is sent in as a parameter with a newVal object which is sent in as a parameter. For example, if let’s say we send in our queue1 object which is shown below and a key value of ‘$’ and a replace value of ‘*’. b $ E $ Our queue will be returned as : b * E * The Print Method Finally, let’s add a method that can be used to print our queue object that is simulated as LinkedList so we can be sure our previous method works correctly. If the queue is empty, print a message indicating so. Do not modify your queue in any way. A call to test this method has already been set up for you within the main method. 3