Java Reference
In-Depth Information
3.
Listing 3-5 shows
Node
as a class within a package containing
LinkedBag
. Revise
LinkedBag
to use this version of
Node
.
4.
Revise
Node
and
LinkedBag
as described in Segment 3.29.
5.
Define a class
LinkedSet
that represents a set and implements the interface described in Project 1a of
Chapter 1. Use the class
LinkedBag
in your implementation. Then write a program that adequately demon-
strates your implementation.
6.
Repeat the previous project, but use a chain of linked nodes instead of the class
LinkedBag
.
7.
Define a class
DoublyLinkedBag
that implements the ADT bag by using a doubly linked chain, as shown in
Figure 3-11. Use the inner class of nodes that Exercise 12 defines.
8.
Repeat the previous project, but define set and get methods in the inner class of nodes.
9.
Use the classes for a set or a bag, as defined in this chapter or described in the previous projects, to create a spell
checker. Consult the details given in Projects 5 and 6 of the previous chapter.
A
NSWERS TO
S
ELF
-T
EST
Q
UESTIONS
1.
a.
First.
b.
The student who arrived last (most recently).
2.
At the beginning.
3.
When the chain is empty,
firstNode
is
null
. Setting
newNode.next
to
firstNode
sets it to
null
. Since
newNode.next
already is
null
, no harm is done by the additional assignment.
4.
Testing the values of both
index
and
currentNode
is not necessary. Although testing either one of these values is
sufficient, testing both values provides a check against mistakes in your code.
5.
The method returns false. If
currentNode
becomes
null
, the entire chain has been searched without success.
6.
Since the bag is empty,
firstNode
—and hence
currentNode
—is
null
. The
while
loop ends immediately and the
method returns false.
•
7.
Locate the first desk by asking the instructor for its address.
•
Give the address that is written on the first desk's paper to the instructor. This is the address of the second
desk in the chain.
•
Return the first desk to the hallway.
•
8.
The student in the first desk moves to the third desk.
•
Remove the first desk using the three steps given as the answer to the previous question.
9.
No. The method
contains
returns either true or false. Although
remove
would be able to tell whether
anEntry
is
in the bag, it would not have a reference to
anEntry
. Thus, it would not be able to remove
anEntry
without doing
its own search.
10.
public boolean
contains(T anEntry)
{
return
getReferenceTo(anEntry) !=
null
;
}
// end contains