• Tidak ada hasil yang ditemukan

PPT Slide 1

N/A
N/A
Protected

Academic year: 2023

Membagikan "PPT Slide 1"

Copied!
57
0
0

Teks penuh

(1)

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

Data Structures for Java Data Structures for Java

William H. Ford William H. Ford William R. Topp William R. Topp

Chapter 11 Chapter 11

Implementing the LinkedList Class Implementing the LinkedList Class

Bret Ford

© 2005, Prentice Hall

(2)

Singly Linked Lists Singly Linked Lists

 Singly-linked lists provide efficient Singly-linked lists provide efficient

insertion and deletion at the front of the insertion and deletion at the front of the

list. Inserting at the back of a singly-linked list. Inserting at the back of a singly-linked

list is inefficient, because it requires a list is inefficient, because it requires a

linear scan that determines the back of linear scan that determines the back of

the list.

the list.

(3)

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

DNode Objects DNode Objects

 A DNode object has reference fields to both A DNode object has reference fields to both the previous node and the next node in the the previous node and the next node in the

list.

list.

 A sequence of DNode objects creates a list A sequence of DNode objects creates a list called a

called a doubly-linked list doubly-linked list . .

(4)

DNode Class DNode Class

 A DNode object has a variable A DNode object has a variable

nodeValue that stores the value and two nodeValue that stores the value and two

other variables, prev and next, that other variables, prev and next, that

reference the predecessor and successor of reference the predecessor and successor of

the node, respectively.

the node, respectively.

 The DNode class is similar to the Node The DNode class is similar to the Node class. It has public data that simplifies class. It has public data that simplifies

access when building implementation access when building implementation

structures.

structures.

(5)

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

DNode Class (continued) DNode Class (continued)

 The class has two constructors. The The class has two constructors. The

default constructor creates a DNode object default constructor creates a DNode object

with the nodeValue field set to null. The with the nodeValue field set to null. The

second constructor provides an argument second constructor provides an argument

of type Object to initialize nodeValue.

of type Object to initialize nodeValue.

(6)

DNode Class (continued) DNode Class (continued)

public class DNode<T>

{

public T nodeValue; // data value of the node public DNode<T> prev; // previous node in the list public DNode<T> next; // next node in the list

// default constructor; creates an object with // the value set to null and whose references // point to the node itself

public DNode() {

nodeValue = null;

// the next node is the current node next = this;

// the previous node is the current node prev = this;

}

(7)

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

DNode Class (continued) DNode Class (continued)

// creates object whose value is item and // whose references point to the node itself public DNode(T item)

{

nodeValue = item;

// the next node is the current node next = this;

// the previous node is the current node prev = this;

} }

(8)

DNode Class (concluded) DNode Class (concluded)

 The declaration of each constructor The declaration of each constructor uses the reference

uses the reference this this to initialize the to initialize the

links next and prev. The keyword this is a links next and prev. The keyword this is a

reference to the object itself. The effect is reference to the object itself. The effect is

to have each constructor create a node to have each constructor create a node

with links that point back to itself.

with links that point back to itself.

(9)

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

Inserting a Node in a Doubly Inserting a Node in a Doubly

Linked List Linked List

 Inserting a Node At a Position Inserting a Node At a Position

Assume the insertion occurs at reference Assume the insertion occurs at reference location curr.

location curr.

Four reference fields in the new node, in node Four reference fields in the new node, in node curr, and in node prevNode (curr.prev) must curr, and in node prevNode (curr.prev) must

be updated.

be updated.

(10)

Inserting a Node in a Doubly Inserting a Node in a Doubly

Linked List (continued) Linked List (continued)

// declare the DNode reference variables newNode and prevNode DNode<T> newNode, prevNode;

// create a new node and assign prevNode to reference the // predecessor of curr

newNode = new DNode<T>(item);

prevNode = curr.prev;

// update reference fields in newNode

newNode.prev = prevNode; // statement 1

newNode.next = curr; // statement 2

// update curr and its predecessor to point at newNode prevNode.next = newNode; // statement 3

curr.prev = newNode; // statement 4

(11)

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

Inserting a Node in a Doubly Inserting a Node in a Doubly

Linked List (concluded)

Linked List (concluded)

(12)

Deleting a Node in a Doubly Deleting a Node in a Doubly

Linked List Linked List

 Deleting a node at a position. Deleting a node at a position.

Assume the deletion occurs at reference Assume the deletion occurs at reference location curr.

location curr.

The algorithm involves unlinking the node The algorithm involves unlinking the node

from the list by having the predecessor of curr from the list by having the predecessor of curr

and the successor of curr point at each other.

and the successor of curr point at each other.

(13)

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

Deleting a Node in a Doubly Deleting a Node in a Doubly

Linked List (concluded) Linked List (concluded)

DNode<T> prevNode = curr.prev, nextNode = curr.next;

// update the reference variables in the adjacent nodes.

prevNode.next = nextNode; // statement 1 nextNode.prev = prevNode; // statement 2

(14)

Circular Doubly Linked Lists Circular Doubly Linked Lists

 A doubly-linked list contains a A doubly-linked list contains a sentinel sentinel node node called header. called header.

 The sentinel is a DNode object containing The sentinel is a DNode object containing a null data value. A linked-list algorithm a null data value. A linked-list algorithm

never uses this value.

never uses this value.

 header.next references the first node of header.next references the first node of the list, and header.prev references the the list, and header.prev references the

last node.

last node.

(15)

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

Circular Doubly Linked Lists Circular Doubly Linked Lists

(continued)

(continued)

(16)

Circular Doubly Linked Lists Circular Doubly Linked Lists

(concluded) (concluded)

 Traversing a list can begin with the header Traversing a list can begin with the header node and continue either forward or

node and continue either forward or backward until the scan returns to the backward until the scan returns to the

header.

header.

(17)

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

Declaring a Doubly Linked List Declaring a Doubly Linked List

 The declaration of a list begins with the The declaration of a list begins with the declaration of the header node.

declaration of the header node.

 The default constructor is used to create The default constructor is used to create the header node.

the header node.

DNode<T> header = new DNode<T>();

(18)

Declaring a Doubly Linked List Declaring a Doubly Linked List

(continued) (continued)

 The declaration of a singly linked list The declaration of a singly linked list

begins by assigning front the value null.

begins by assigning front the value null.

The resulting singly-linked list has no The resulting singly-linked list has no

nodes.

nodes.

Empty list:

Empty list:

front == null

 A doubly-linked list always contains at A doubly-linked list always contains at least one node, the header.

least one node, the header.

Empty list:

Empty list:

header.next == header

or or

header.prev == header

(19)

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

Declaring a Doubly Linked List Declaring a Doubly Linked List

(concluded)

(concluded)

(20)

DNodes.toString() DNodes.toString()

public static <T> String toString(DNode<T> header) {

if (header.next == header) return "null";

// scan list starting at the first node;

// add value to string

DNode<T> curr = header.next;

String str = "[" + curr.nodeValue;

(21)

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

DNodes.toString() (concluded) DNodes.toString() (concluded)

// append all but last node, separating // items with a comma

// polymorphism calls toString() for the // nodeValue type

while(curr.next != header) {

curr = curr.next;

str += ", " + curr.nodeValue;

}

str += "]";

return str;

}

(22)

Inserting a Node Inserting a Node

The insert operation adds a new The insert operation adds a new element at a designated reference element at a designated reference

location in the list, creates a new node and adds location in the list, creates a new node and adds

it to the list immediately before the designated it to the list immediately before the designated

node.

node.

Implemented by addBefore() that takes a Implemented by addBefore() that takes a DNode DNode reference argument

reference argument curr curr and a new value as and a new value as

arguments. The return value is a reference to the arguments. The return value is a reference to the

new node. The algorithm involves updating four new node. The algorithm involves updating four

links, so the algorithm has running time O(1).

links, so the algorithm has running time O(1).

(23)

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

Inserting a Node (continued)

Inserting a Node (continued)

(24)

Inserting a Node (continued) Inserting a Node (continued)

public static <T> DNode<T> addBefore(DNode<T>

curr, T item) {

// declare reference variables for new node // and previous node

DNode<T> newNode, prevNode;

// create new DNode with item as initial value newNode = new DNode<T>(item);

// assign prevNode the reference value // of node before p

prevNode = curr.prev;

(25)

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

Inserting a Node (concluded) Inserting a Node (concluded)

// update reference fields in newNode newNode.prev = prevNode;

newNode.next = curr;

// update curr and prevNode to point at newNode prevNode.next = newNode;

curr.prev = newNode;

return newNode;

}

(26)

Inserting into an Empty List Inserting into an Empty List

 Inserting an element into an empty list Inserting an element into an empty list simultaneously creates both the first and simultaneously creates both the first and

the last node in the list.

the last node in the list.

 The header can reference this node by The header can reference this node by using the

using the header.next header.next and the link and the link header.prev

header.prev . .

(27)

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

Removing a Node Removing a Node

The algorithm involves updating links in the The algorithm involves updating links in the adjacent successor and predecessor nodes.

adjacent successor and predecessor nodes.

The method The method remove() remove() takes a takes a DNode DNode reference

reference curr curr as an argument. If curr points as an argument. If curr points back to itself (

back to itself ( curr.next == curr curr.next == curr ), ), curr curr is the is the header node of an empty list, and the method header node of an empty list, and the method

simply returns.

simply returns.

The update of the links requires only two The update of the links requires only two

statements, so remove() has running time O(1).

statements, so remove() has running time O(1).

(28)

Removing a Node (continued)

Removing a Node (continued)

(29)

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

Removing a Node (concluded) Removing a Node (concluded)

public static <T> void remove(DNode<T> curr) {

// return if the list is empty if (curr.next == curr)

return;

// declare references for the predecessor // and successor nodes

DNode<T> prevNode = curr.prev, nexNode = curr.next;

// update reference fields for // predecessor and successor prevNode.next = nexNode;

nexNode.prev = prevNode;

}

(30)

Modifying the Ends of a List Modifying the Ends of a List

 In a singly-linked list, operations that In a singly-linked list, operations that

insert or delete nodes at the ends of the insert or delete nodes at the ends of the

list require very distinct algorithms.

list require very distinct algorithms.

 Because a doubly-linked list is a circular Because a doubly-linked list is a circular

list with a header node, update operations list with a header node, update operations

at the ends of the list simply use at the ends of the list simply use

addBefore() and remove() with arguments addBefore() and remove() with arguments

that are reference fields in the header.

that are reference fields in the header.

(31)

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

Modifying the Ends of a List Modifying the Ends of a List

(continued) (continued)

 To insert item at the front of the list, call To insert item at the front of the list, call addBefore(header.next, item)

addBefore(header.next, item) . .

(32)

Modifying the Ends of a List Modifying the Ends of a List

(continued) (continued)

 To remove the front of the list, call To remove the front of the list, call remove(header.next)

remove(header.next) . .

(33)

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

Modifying the Ends of a List Modifying the Ends of a List

(concluded) (concluded)

 Update back of list with addBefore() or Update back of list with addBefore() or remove() using header as the argument.

remove() using header as the argument.

(34)

jumbleLetters() jumbleLetters()

public static String jumbleLetters(String word) {

DNode<Character> header = new DNode<Character>();

String jumbleword = "";

// use rnd.nextInt(2) to determine if char // is inserted at the front (value = 0) or // back (value = 1) of list

for (int i = 0; i < word.length(); i++) if (rnd.nextInt(2) == 0)

// add at the front of the list

DNodes.addBefore(header.next,word.charAt(i));

else

// insert at the back of the list

DNodes.addBefore(header, word.charAt(i));

(35)

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

jumbleLetters() jumbleLetters()

(concluded) (concluded)

// create the jumbled word and clear the list while (header.next != header)

{

jumbleword += header.next.nodeValue;

DNodes.remove(header.next);

}

return jumbleword;

}

(36)

Program 11.1 Program 11.1

import java.util.Random;

import java.util.Scanner;

import ds.util.DNode;

import ds.util.DNodes;

public class Program11_1 {

static Random rnd = new Random();

public static void main(String[] args) {

Scanner keyIn = new Scanner(System.in);

String word, jumbleword;

int numWords, i, j;

// prompt for the number of words to enter System.out.print("How many words will you" + " enter? ");

(37)

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

Program 11.1 (continued) Program 11.1 (continued)

numWords = keyIn.nextInt();

for (i = 0; i < numWords; i++) {

System.out.print("Word: ");

word = keyIn.next();

jumbleword = jumbleLetters(word);

// output the word and its jumbled variation System.out.println("Word/Jumbled Word: "

+ word + " " + jumbleword);

} }

public static String jumbleLetters(String word) {

DNode<Character> header = new DNode<Character>();

String jumbleword = "";

(38)

Program 11.1 (concluded) Program 11.1 (concluded)

// use rnd.nextInt(2) to determine if char // is inserted at the front (value = 0) or // back (value = 1) of list

for (int i = 0; i < word.length(); i++) if (rnd.nextInt(2) == 0)

// add at the front of the list

DNodes.addBefore(header.next,word.charAt(i));

else

// insert at the back of the list

DNodes.addBefore(header, word.charAt(i));

// create the jumbled word and clear the list while (header.next != header)

{

jumbleword += header.next.nodeValue;

DNodes.remove(header.next);

}

return jumbleword;

} }

(39)

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

Program 11.1 (Run) 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

(40)

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.

(41)

© 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).

(42)

LinkedList Class Private LinkedList Class Private

Members (continued)

Members (continued)

(43)

© 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 >

}

(44)

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.

(45)

© 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.

(46)

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;

}

(47)

© 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;

}

(48)

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.

(49)

© 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;

}

(50)

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.

(51)

© 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;

}

(52)

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;

}

(53)

© 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.

(54)

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.

(55)

© 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;

}

(56)

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).

(57)

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

Removing at an Index Removing at an Index

(concluded) (concluded)

public T remove(int index) {

DNode<T> p = nodeAtIndex(index);

// save the return value

T returnElement = p.nodeValue;

// remove element at node p and decrement list size remove(p);

listSize--;

// we've made a modification modCount++;

// return the value that was removed return returnElement;

}

Referensi

Garis besar

Dokumen terkait

List in chronological order previous positions, concluding with the present position. List

Binary Search Case 2 Binary Search Case 2  The value The value target target is less than is less than midValue midValue , , and the search must continue in the lower and the search

Implementing a View Implementing a View continued continued // object that is returned by viewOne private View viewObj = null; // method returns a View object that // accesses the

VertexInfo Class // maintains vertex properties, including // its set of Edges class VertexInfo { // vertex reference back to the vertex in the map public T vertex; // list of

Shariah refers to the road to the watering place, the straight path to be followed Shariah refers to the road to the watering place, the straight path to be followed The

 The Foundations of Artificial Intelligence  The History of Artificial Intelligence  The State of the Art  Philosophical Foundations  Logic Programming Language Topic Contents

Predefined Methods Predefined Methods continued continued  To access a Java method defined in the To access a Java method defined in the Math class, a calling statement must Math

The method name typically begins with the word typically begins with the word set set Time24 Class Mutator setTime: void setTimeint hour, int minute The arguments update the