© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 11 Implementing.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
Linked Lists Chapter 4.
DATA STRUCTURES USING C++ Chapter 5
M180: Data Structures & Algorithms in Java
Review Learn about linked lists
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Chapter 3: Arrays, Linked Lists, and Recursion
Data Structures Using C++ 2E
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 18 Binary.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 17 Binary.
Chapter 7 More Lists. Chapter 7: More Lists 7.1 – Circular Linked Lists 7.2 – Doubly Linked Lists 7.3 – Linked Lists with Headers and Trailers 7.4 – A.
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Chapter 4: Loops and Files
Data Structures Using Java1 Chapter 4 Linked Lists.
ADSA: Linked Lists/ Advanced Data Structures and Algorithms Objective – –implement and use linked lists Semester 2, Linked.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 8 Collection.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 10 Linked.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 5 Generic.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
A Doubly Linked List prevnextdata There’s the need to access a list in reverse order header dnode.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
CS2006- Data Structures I Chapter 5 Linked Lists III.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
M180: Data Structures & Algorithms in Java Linked Lists – Part 2 Arab Open University 1.
Recursive Objects (Part 2) 1. Adding to the front of the list  adding to the front of the list  t.addFirst('z') or t.add(0, 'z') 2 'a' 'x' LinkedList.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 4 Introduction.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 12 Iterators.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Data.
CSCS-200 Data Structure and Algorithms Lecture
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 20 Ordered.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture1.
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
Recursive Objects Singly Linked List (Part 2) 1. Operations at the head of the list  operations at the head of the list require special handling because.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Chapter 16: Linked Lists.
Data Structures for Java William H. Ford William R. Topp
Chapter 4 Unordered List.
Lecture 6 of Computer Science II
C++ Programming:. Program Design Including
Linked Lists Chapter 5 (continued)
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Data Structures for Java William H. Ford William R. Topp
List Data Structure.
Linked Lists.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Podcast Ch18b Title: STree Class
Generic Classes and Methods
Chapter 4 Unordered List.
Linked Lists Chapter 5 (continued)
Programming II (CS300) Chapter 07: Linked Lists
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 5 (continued)
Presentation transcript:

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 11 Implementing the LinkedList Class Bret Ford © 2005, Prentice Hall

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Singly Linked Lists Singly-linked lists provide efficient insertion and deletion at the front of the list. Inserting at the back of a singly-linked list is inefficient, because it requires a linear scan that determines the back of the list. Singly-linked lists provide efficient insertion and deletion at the front of the list. Inserting at the back of a singly-linked list is inefficient, because it requires a linear scan that determines the back of the list.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. DNode Objects A DNode object has reference fields to both the previous node and the next node in the list. A DNode object has reference fields to both the previous node and the next node in the list. A sequence of DNode objects creates a list called a doubly-linked list. A sequence of DNode objects creates a list called a doubly-linked list.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. DNode Class A DNode object has a variable nodeValue that stores the value and two other variables, prev and next, that reference the predecessor and successor of the node, respectively. A DNode object has a variable nodeValue that stores the value and two other variables, prev and next, that reference the predecessor and successor of the node, respectively. The DNode class is similar to the Node class. It has public data that simplifies access when building implementation structures. The DNode class is similar to the Node class. It has public data that simplifies access when building implementation structures.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. DNode Class (continued) The class has two constructors. The default constructor creates a DNode object with the nodeValue field set to null. The second constructor provides an argument of type Object to initialize nodeValue. The class has two constructors. The default constructor creates a DNode object with the nodeValue field set to null. The second constructor provides an argument of type Object to initialize nodeValue.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. DNode Class (continued) public class DNode { public T nodeValue; // data value of the node public DNode prev; // previous node in the list public DNode 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; }

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. 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; }

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. DNode Class (concluded) The declaration of each constructor uses the reference this to initialize the links next and prev. The keyword this is a reference to the object itself. The effect is to have each constructor create a node with links that point back to itself. The declaration of each constructor uses the reference this to initialize the links next and prev. The keyword this is a reference to the object itself. The effect is to have each constructor create a node with links that point back to itself.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Inserting a Node in a Doubly Linked List Inserting a Node At a Position Inserting a Node At a Position Assume the insertion occurs at reference location curr. Assume the insertion occurs at reference location curr. Four reference fields in the new node, in node curr, and in node prevNode (curr.prev) must be updated. Four reference fields in the new node, in node curr, and in node prevNode (curr.prev) must be updated.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Inserting a Node in a Doubly Linked List (continued) // declare the DNode reference variables newNode and prevNode DNode newNode, prevNode; // create a new node and assign prevNode to reference the // predecessor of curr newNode = new DNode (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

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Inserting a Node in a Doubly Linked List (concluded)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Deleting a Node in a Doubly Linked List Deleting a node at a position. Deleting a node at a position. Assume the deletion occurs at reference location curr. Assume the deletion occurs at reference location curr. The algorithm involves unlinking the node from the list by having the predecessor of curr and the successor of curr point at each other. The algorithm involves unlinking the node from the list by having the predecessor of curr and the successor of curr point at each other.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Deleting a Node in a Doubly Linked List (concluded) DNode prevNode = curr.prev, nextNode = curr.next; // update the reference variables in the adjacent nodes. prevNode.next = nextNode;// statement 1 nextNode.prev = prevNode;// statement 2

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Circular Doubly Linked Lists A doubly-linked list contains a sentinel node called header. A doubly-linked list contains a sentinel node called header. The sentinel is a DNode object containing a null data value. A linked-list algorithm never uses this value. The sentinel is a DNode object containing a null data value. A linked-list algorithm never uses this value. header.next references the first node of the list, and header.prev references the last node. header.next references the first node of the list, and header.prev references the last node.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Circular Doubly Linked Lists (continued)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Circular Doubly Linked Lists (concluded) Traversing a list can begin with the header node and continue either forward or backward until the scan returns to the header. Traversing a list can begin with the header node and continue either forward or backward until the scan returns to the header.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Declaring a Doubly Linked List The declaration of a list begins with the declaration of the header node. The declaration of a list begins with the declaration of the header node. The default constructor is used to create the header node. The default constructor is used to create the header node. DNode header = new DNode ();

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Declaring a Doubly Linked List (continued) The declaration of a singly linked list begins by assigning front the value null. The resulting singly-linked list has no nodes. Empty list: The declaration of a singly linked list begins by assigning front the value null. The resulting singly-linked list has no nodes. Empty list: front == null A doubly-linked list always contains at least one node, the header. Empty list: or A doubly-linked list always contains at least one node, the header. Empty list: header.next == header or header.prev == header

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Declaring a Doubly Linked List (concluded)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.DNodes.toString() public static String toString(DNode header) { if (header.next == header) return "null"; // scan list starting at the first node; // add value to string DNode curr = header.next; String str = "[" + curr.nodeValue;

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. 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; }

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Inserting a Node The insert operation adds a new element at a designated reference location in the list, creates a new node and adds it to the list immediately before the designated node. The insert operation adds a new element at a designated reference location in the list, creates a new node and adds it to the list immediately before the designated node. Implemented by addBefore() that takes a DNode reference argument curr and a new value as arguments. The return value is a reference to the new node. The algorithm involves updating four links, so the algorithm has running time O(1). Implemented by addBefore() that takes a DNode reference argument curr and a new value as arguments. The return value is a reference to the new node. The algorithm involves updating four links, so the algorithm has running time O(1).

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Inserting a Node (continued)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Inserting a Node (continued) public static DNode addBefore(DNode curr, T item) { // declare reference variables for new node // and previous node DNode newNode, prevNode; // create new DNode with item as initial value newNode = new DNode (item); // assign prevNode the reference value // of node before p prevNode = curr.prev;

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. 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; }

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Inserting into an Empty List Inserting an element into an empty list simultaneously creates both the first and the last node in the list. Inserting an element into an empty list simultaneously creates both the first and the last node in the list. The header can reference this node by using the header.next and the link header.prev. The header can reference this node by using the header.next and the link header.prev.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Removing a Node The algorithm involves updating links in the adjacent successor and predecessor nodes. The algorithm involves updating links in the adjacent successor and predecessor nodes. The method remove() takes a DNode reference curr as an argument. If curr points back to itself ( curr.next == curr ), curr is the header node of an empty list, and the method simply returns. The method remove() takes a DNode reference curr as an argument. If curr points back to itself ( curr.next == curr ), curr is the header node of an empty list, and the method simply returns. The update of the links requires only two statements, so remove() has running time O(1). The update of the links requires only two statements, so remove() has running time O(1).

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Removing a Node (continued)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Removing a Node (concluded) public static void remove(DNode curr) { // return if the list is empty if (curr.next == curr) return; // declare references for the predecessor // and successor nodes DNode prevNode = curr.prev, nexNode = curr.next; // update reference fields for // predecessor and successor prevNode.next = nexNode; nexNode.prev = prevNode; }

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Modifying the Ends of a List In a singly-linked list, operations that insert or delete nodes at the ends of the list require very distinct algorithms. In a singly-linked list, operations that insert or delete nodes at the ends of the list require very distinct algorithms. Because a doubly-linked list is a circular list with a header node, update operations at the ends of the list simply use addBefore() and remove() with arguments that are reference fields in the header. Because a doubly-linked list is a circular list with a header node, update operations at the ends of the list simply use addBefore() and remove() with arguments that are reference fields in the header.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Modifying the Ends of a List (continued) To insert item at the front of the list, call addBefore(header.next, item). To insert item at the front of the list, call addBefore(header.next, item).

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Modifying the Ends of a List (continued) To remove the front of the list, call remove(header.next). To remove the front of the list, call remove(header.next).

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Modifying the Ends of a List (concluded) Update back of list with addBefore() or remove() using header as the argument. Update back of list with addBefore() or remove() using header as the argument.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.jumbleLetters() public static String jumbleLetters(String word) { DNode header = new DNode (); 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));

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. jumbleLetters() (concluded) // create the jumbled word and clear the list while (header.next != header) { jumbleword += header.next.nodeValue; DNodes.remove(header.next); } return jumbleword; }

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. 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? ");

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. 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 header = new DNode (); String jumbleword = "";

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. 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; }

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. 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

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Graphical Version of Program 11.1 Program11_1G.java in Chapter 11 of the software supplement is a graphical version of Program Here is a sample run. Program11_1G.java in Chapter 11 of the software supplement is a graphical version of Program Here is a sample run.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. LinkedList Class Private Members The DNode reference header identifies the doubly-linked list that stores the elements. The DNode reference header identifies the doubly-linked list that stores the elements. The integer listSize maintains a count of the number of elements in the list. The integer listSize maintains a count of the number of elements in the list. A class method increments modCount whenever its execution updates the list. The variable is used in the implementation of iterators (Chapter 13). A class method increments modCount whenever its execution updates the list. The variable is used in the implementation of iterators (Chapter 13).

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. LinkedList Class Private Members (continued)

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. LinkedList Class Outline public class LinkedList implements List { // number of elements in the list private int listSize; // the doubly-linked list header node private DNode header; // maintains a count of the number of list updates private int modCount; < constructor, List interface, and special purpose methods > }

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Index Operations For add() and remove() at an index, we need to first determine if the index is in range and then identify the reference that points to the node at the index position. The private methods rangeCheck() and nodeAtIndex() handle these tasks. For add() and remove() at an index, we need to first determine if the index is in range and then identify the reference that points to the node at the index position. The private methods rangeCheck() and nodeAtIndex() handle these tasks. rangeCheck() throws an IndexOutOfBoundsException if an index is not in range. rangeCheck() throws an IndexOutOfBoundsException if an index is not in range.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Index Operations (continued) nodeAtIndex() returns a DNode reference that points at position index using a loop from 0 to index that tracks a node reference variable which starts at header and moves forward using the next reference field. nodeAtIndex() returns a DNode reference that points at position index using a loop from 0 to index that tracks a node reference variable which starts at header and moves forward using the next reference field.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Index Operations (concluded) // return the DNode reference that points at // an element at position index private DNode nodeAtIndex(int index) { // check if index is in range rangeCheck(index); // start at the header DNode p = header; // go to index either by moving forward // from the front of the list; see // Programming Exercise 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 // construct an empty list public LinkedList() { header = new DNode (); listSize = 0; modCount = 0; }

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Indexed List Access The indexed access methods get() and set() are implemented by using the private method nodeAtIndex(). The indexed access methods get() and set() are implemented by using the private method nodeAtIndex(). These methods are not efficient if they are used repeatedly in a program, because nodeAtIndex() must sequence through the doubly-linked list elements until it reaches the node at position index. A LinkedList collection is really designed to be accessed sequentially. These methods are not efficient if they are used repeatedly in a program, because nodeAtIndex() must sequence through the doubly-linked list elements until it reaches the node at position index. A LinkedList collection is really designed to be accessed sequentially.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Indexed List Access (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 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; }

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Searching a List The method indexOf() takes an argument of type Object and returns the index of the first occurrence of the target in the list or -1 if it is not found. It is used by the index-based add() and remove() methods. The method indexOf() takes an argument of type Object and returns the index of the first occurrence of the target in the list or -1 if it is not found. It is used by the index-based add() and remove() methods. The related method contains() searches the list and returns a boolean value. The related method contains() searches the list and returns a boolean value.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Searching a List (continued) public int indexOf(Object item) { int index = 0; // search for item using equals() for (DNode 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; }

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. 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 The LinkedList class has various forms of the add() and remove() methods to insert and delete elements in a list. In each case the method must position itself at the appropriate node. Once the is done, the insertion or deletion operation is executed by calling the corresponding private methods addBefore(curr, item) and remove(curr) where curr is the node reference. The LinkedList class has various forms of the add() and remove() methods to insert and delete elements in a list. In each case the method must position itself at the appropriate node. Once the is done, the insertion or deletion operation is executed by calling the corresponding private methods addBefore(curr, item) and remove(curr) where curr is the node reference.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Adding to the Back of a List The add(T item) method inserts a new node with value item at the back of the list. Its implementation uses addBefore(header,item) with header serving as the argument. After updating the list size, add() returns true. The add(T item) method inserts a new node with value item at the back of the list. Its implementation uses addBefore(header,item) with header serving as the argument. After updating the list size, add() returns true. Some Java collections, that also implement the Collection interface, do not allow duplicate values. For these collections, the add() method returns false if item is already in the list. Some Java collections, that also implement the Collection interface, do not allow duplicate values. For these collections, the add() method returns false if item is already in the list.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Adding to the Back of a List (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; }

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Removing at an Index The remove(int i) method deletes the node at position index in the list and returns its value. The remove(int i) method deletes the node at position index in the list and returns its value. Locating the node is the task of the method call nodeAtIndex() which first uses rangeCheck() to validate the index or throw an IndexOutOfBoundsException. The deletion is handled by the private method remove(curr). Locating the node is the task of the method call nodeAtIndex() which first uses rangeCheck() to validate the index or throw an IndexOutOfBoundsException. The deletion is handled by the private method remove(curr).

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Removing at an Index (concluded) public T remove(int index) { DNode 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; }