Summary: In this laboratory, you will have an opportunity to ground your understanding of the linked implementation of queues. The focus of this lab will be on correcting a partial implementation.
Contents:
Required Code Files:
a. Start Eclipse.
b. Verify that you already have a package named
username.linear
and that the package
contains copies of
If you lack any of those files, add them to the package.
Make sure to do this exercise with at least one colleague!
a. Read through
LinkedQueue.java
and
QueueNode.java
.
b. There is a significant bug in the put
method for empty
lists. What is that bug?
c. There is a significant bug in the get
method for
one-element lists. What is that bug?
a. Compile and execute TestLinkedQueue
. It should crash
almost immediately, because of the bug you may have figured out in the
put
method.
b. In case you haven't figured it out, the problem is that it's
difficult to refer to back.next
when back
is null
. The solution is to check whether or not
back
is null
, and, if so, do something other than
put the new node after the back. Figure out what you should do in the
special case and implement it.
c. Recompile LinkedQueue
and execute TestLinkedQueue
. If you solved part b correctly, you should be able to put an element
and get it back, but it is unlikely the rest of your program will work
correctly. (Depending on how you solved part b, your code may still
work correctly.)
a. One solution to exercise 2 is as follows
if (this.back == null) { this.back = new QueueNode(val); this.front = this.back; } else { // Old code }
Update your code to use my solution (sorry), recompile
LinkedQueue
, and then execute TestLinkedQueue
.
Your program should put and get the first element successfully, but fail
afterwards.
b. Summarize, as best you can, what seems to be wrong now. (Describe the problem with the output, you need not analyze the underlying problem.)
c. Think about the problem again and see if you can determine the error.
d. Fix the error.
In many applications, it turns out to be helpful to remove values from queues. For example, if I have a queue of email messages from students to respond to and a student suddenly drops my class, I should remove all email from that student from the class.
While it is possible to add a remove
method to
LinkedQueue
, it is also possible to write such a
method using only the basic queue operations (and, in fact, the
basic stack operations).
a. Create a class, QueueHelper
, that provides one
method, remove(Object val, Queue q)
. This method
should remove all instances of val
from the
queue.
b. Test your method.
c. What do you expect to have happen if you use the same strategy to remove values from a stack?
d. Confirm your answer to c experimentally. (You'll need to change
the parameter type to use remove
.)
Monday, 7 March 2005 [Samuel A. Rebelsky]
Wednesday, 8 March 2006 [Samuel A. Rebelsky]
This page was generated by
Siteweaver on Wed Mar 8 09:14:45 2006.
The source to the page was last modified on Wed Mar 8 09:14:43 2006.
This page may be found at http://www.cs.grinnell.edu/~rebelsky/TAO/Labs/queues.html
.
You may wish to
validate this page's HTML
;
;
Check with Bobby