• Tidak ada hasil yang ditemukan

Run) (Run)

Dalam dokumen PPT Slide 1 (Halaman 39-57)

Program 11.1 (Run)

Run:

How many words will you enter? 3 Word: before

Word/Jumbled Word: before erofeb Word: java

Word/Jumbled Word: java vjaa Word: link

Word/Jumbled Word: link knli

Graphical Version of Graphical Version of

Program 11.1 Program 11.1

 Program11_1G.java in Chapter 11 of the Program11_1G.java in Chapter 11 of the software supplement is a graphical version software supplement is a graphical version

of Program 11.1. Here is a sample run.

of Program 11.1. Here is a sample run.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

LinkedList Class Private LinkedList Class Private

Members Members

 The DNode reference header identifies the The DNode reference header identifies the doubly-linked list that stores the elements.

doubly-linked list that stores the elements.

 The integer listSize maintains a count of The integer listSize maintains a count of the number of elements in the list.

the number of elements in the list.

 A class method increments modCount A class method increments modCount whenever its execution updates the list.

whenever its execution updates the list.

The variable is used in the implementation The variable is used in the implementation

of iterators (Chapter 13).

of iterators (Chapter 13).

LinkedList Class Private LinkedList Class Private

Members (continued)

Members (continued)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

LinkedList Class Outline LinkedList Class Outline

public class LinkedList<T> implements List<T>

{

// number of elements in the list private int listSize;

// the doubly-linked list header node private DNode<T> header;

// maintains a count of the number of list updates private int modCount;

< private utility methods >

< constructor, List interface, and special purpose methods >

}

Index Operations Index Operations

 For add() and remove() at an index, we For add() and remove() at an index, we need to first determine if the index is in need to first determine if the index is in

range and then identify the reference that range and then identify the reference that

points to the node at the index position.

points to the node at the index position.

The private methods rangeCheck() and The private methods rangeCheck() and

nodeAtIndex() handle these tasks.

nodeAtIndex() handle these tasks.

 rangeCheck() throws an rangeCheck() throws an

IndexOutOfBoundsException if an index is IndexOutOfBoundsException if an index is

not in range.

not in range.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Index Operations (continued) Index Operations (continued)

nodeAtIndex() returns a DNode reference nodeAtIndex() returns a DNode reference

that points at position index using a loop from that points at position index using a loop from

0 to index that tracks a node reference 0 to index that tracks a node reference

variable which starts at header and moves variable which starts at header and moves

forward using the next reference field.

forward using the next reference field.

Index Operations (concluded) Index Operations (concluded)

// return the DNode reference that points at // an element at position index

private DNode<T> nodeAtIndex(int index) {

// check if index is in range rangeCheck(index);

// start at the header DNode<T> p = header;

// go to index either by moving forward // from the front of the list; see

// Programming Exercise 11.16 for a way

// to improve the performance of this method for (int j = 0; j <= index; j++)

p = p.next;

// return reference to node at position p = index return p;

}

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

LinkedList Class Constructor LinkedList Class Constructor

// construct an empty list public LinkedList()

{

header = new DNode<T>();

listSize = 0;

modCount = 0;

}

Indexed List Access Indexed List Access

The indexed access methods get() and set() The indexed access methods get() and set() are implemented by using the private method are implemented by using the private method

nodeAtIndex().

nodeAtIndex().

These methods are not efficient if they are These methods are not efficient if they are used repeatedly in a program, because

used repeatedly in a program, because

nodeAtIndex() must sequence through the nodeAtIndex() must sequence through the

doubly-linked list elements until it reaches the doubly-linked list elements until it reaches the

node at position index. A LinkedList collection node at position index. A LinkedList collection

is really designed to be accessed sequentially.

is really designed to be accessed sequentially.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Indexed List Access Indexed List Access

(concluded) (concluded)

// replaces the value at the specified position // in this list with item and returns the

// previous value

public T set(int index, T item) {

// get the reference that identifies node // at position index

DNode<T> p = nodeAtIndex(index);

// save the old value

T previousValue = p.nodeValue;

// assign item at position index p.nodeValue = item;

// return the previous value return previousValue;

}

Searching a List Searching a List

 The method indexOf() takes an The method indexOf() takes an

argument of type Object and returns the argument of type Object and returns the

index of the first occurrence of the target index of the first occurrence of the target

in the list or -1 if it is not found. It is used in the list or -1 if it is not found. It is used

by the index-based add() and remove() by the index-based add() and remove()

methods.

methods.

 The related method contains() searches The related method contains() searches the list and returns a boolean value.

the list and returns a boolean value.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Searching a List (continued) Searching a List (continued)

public int indexOf(Object item) {

int index = 0;

// search for item using equals() for (DNode<T> curr = header.next;

curr != header; curr = curr.next) {

if (item.equals(curr.nodeValue)) // success return index;

index++;

}

// item is not in the list; return -1 return -1;

}

Searching a List (concluded) Searching a List (concluded)

// returns true if this list contains // item and false otherwise

public boolean contains(Object item) {

return indexOf(item) >= 0;

}

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Modifying a List Modifying a List

The LinkedList class has various forms The LinkedList class has various forms

of the add() and remove() methods to insert of the add() and remove() methods to insert

and delete elements in a list. In each case and delete elements in a list. In each case

the method must position itself at the the method must position itself at the

appropriate node. Once the is done, the appropriate node. Once the is done, the

insertion or deletion operation is executed by insertion or deletion operation is executed by

calling the corresponding private methods calling the corresponding private methods

addBefore(curr, item) and remove(curr) addBefore(curr, item) and remove(curr)

where curr is the node reference.

where curr is the node reference.

Adding to the Back of a List Adding to the Back of a List

 The add(T item) method inserts a new The add(T item) method inserts a new node with value item at the back of the node with value item at the back of the

list. Its implementation uses list. Its implementation uses

addBefore(header,item)

addBefore(header,item) with with header

header serving as the argument. After serving as the argument. After updating the list size, add() returns

updating the list size, add() returns true true . .

 Some Java collections, that also implement Some Java collections, that also implement the Collection interface, do not allow

the Collection interface, do not allow

duplicate values. For these collections, the duplicate values. For these collections, the

add() method returns false if

add() method returns false if item item is is already in the list.

already in the list.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Adding to the Back of a List Adding to the Back of a List

(concluded) (concluded)

// appends item to the end of this list // and returns true

public boolean add(T item) {

// insert item at the end of list and // increment list size

DNodes.addBefore(header,item);

listSize++;

// the list has changed modCount++;

return true;

}

Removing at an Index Removing at an Index

 The remove(int i) method deletes the The remove(int i) method deletes the node at position index in the list and node at position index in the list and

returns its value.

returns its value.

 Locating the node is the task of the Locating the node is the task of the method call nodeAtIndex() which first method call nodeAtIndex() which first

uses rangeCheck() to validate the index or uses rangeCheck() to validate the index or

throw an IndexOutOfBoundsException.

throw an IndexOutOfBoundsException.

The deletion is handled by the private The deletion is handled by the private

method remove(curr).

method remove(curr).

Dalam dokumen PPT Slide 1 (Halaman 39-57)

Dokumen terkait