Professional Documents
Culture Documents
Implementasi Link List
Implementasi Link List
83
it (including the implicit reference between the two). Each call to the iterator's next
method simply returns the list element indexed by place, and also increments place.
Observe that the ArrayList class's instance variables elems and length were
declared as private in Program 8.9. The methods in the ArrayList .LRIterator
class can still access these instance variables, since an inner class can access all parts of
the class that contains it.
Program 8.10 Iterator for the implementation of bounded lists using arrays (as an inner class of
ArrayList).
iter
Figure 8.11 An ArrayList object list, and an iterator object iter constructed from it by: iter
= list.iterator().
List Abstract Data !85
It is important to note that the iterator shares the list object. In particular, the iterator
does not copy the list elements. It does not need its own data structure. All the
information needed by the iterator is contained in the list's own data structure, apart
from the place-marker. This is typical of most iterators over most collections: a place-
marker and a reference to the underlying collection (which may be implicit) are quite
sufficient to implement them.
(d) first
last
Figure 8.12 Representation of unbounded lists by SLLs: (a) general case; (b—c) lists of words: (di
empty list.
Table 8.13 Implementation of lists using SLLs: summary of algorithms (where n, n1, and n2 are the
lengths of the lists involved).
ables f i r s t and last are links to the first and last nodes, respectively, and are initially
null. The instance variable length keeps track of the list's length. The nodes them-
selves are of class SLLNode (see Program 4.4).
The 1-parameter add operation has time complexity 0(1), because a direct link to the
last node is available. However, the get, set, 2-parameter add, and remove opera-
tions each has to locate the node with a given index. Unless that node happens to be the
first or last node, no direct link to that node is available, so the operation must count out
nodes from the start of the SLL. The index could be anything between 0 and n, where n is
Program 8.14 Java implementation of unbounded lists using SLLs (continued on next page).
List Abstract Data
the list's length, and will be about n/2 on average. Thus these four operations all have
time complexity O(n).
The linked-list implementation of lists could be used in any application (including the
text editor of Example 8.1). The fact that it supports unbounded lists is a major
advantage. On the other hand, the O(n) time complexity of several operations would
slow down any application that uses these operations frequently (such as the text editor).
In practice, this implementation is not suitable for applications that process very long
lists.
The iterator operation in Program 8.14 constructs a left-to-right iterator over
the LinkedList object. This iterator is an object of the class LinkedList.
LRIterator shown in Program 8.15 (which would be an inner class of the
LinkedList class). The iterator is represented by a place-marker, place. This
188 lava Collections
place-marker is initially a link to the first node of the SLL, but will later be a link to the
other nodes in turn. Figure 8.16 shows a LinkedList object together with a
LinkedList .LRIterator object that has just been constructed from it (including
the implicit reference between the two). Each call to the iterator's next method simply
returns the list element in the node to which place is a link, and also advances place
to the successor node (which may be null).
List Abstract Data Types 189
if (i == 0) {
oldElem = first.element;
if ( f i r s t == last)
last = null;
first = first.succ;
} else {
SLLNode pred = node(i-1);
SLLNode old = pred.succ;
oldElem = old.element;
pred.succ = old.succ;
if (old == last)
last = pred;
}
length--;
return oldElem;
}
111111111111 Iterator 11! 11111 i 11
public Iterator iterator () {
/ / Return an iterator that will visit all elements of this list, in left-to-right
/ / order.
return new LinkedList.LRIterator();
}
//////////// Auxiliary method ///////
private SLLNode node (int i) {
/ / Return a link to the node containing the element with index i in this list.
SLLNode curr = first;
for (int j = 0; j < i; j++)
curr = curr.succ;
return curr;
Program 8.15 Iterator for the implementation of unbounded lists using SLLs (as an inner class of
LinkedList).
iter
Figure 8.16 A LinkedList object list, and an iterator object iter constructed from it by: iter
= list.iterator().
191
iterates over the list list, inspecting and then removing each element in turn.
The java.util .Listlterator interface has additional operations that are
specific to iteration over lists. It extends the java.util . Iterator interface with
operations has Previous and previous (which are analogous to hasNext and
next, and allow right-to-left iteration over the underlying list), set (which replaces,
in the underlying list, the element most recently returned by next or previous), and
add (which adds a new element to the underlying list just before the place-marker). The
j a v a . u t i l .LinkedList class provides a listlterator operation, which
constructs a Listlterator object from a LinkedList object.
site, and includes a suitable user interface. In this section we will confine our
attention to the representation of a videotape and the algorithms needed to add
and erase programs.