review list implementations
play

Review: List Implementations The external interface is already - PDF document

Review: List Implementations The external interface is already defined Implementation goal: implement methods efficiently CSE 143 Java ArrayList approach: use an array with extra space internally ArrayList efficiency Linked


  1. Review: List Implementations • The external interface is already defined • Implementation goal: implement methods “efficiently” CSE 143 Java • ArrayList approach: use an array with extra space internally • ArrayList efficiency Linked Lists • Iterating, indexing (get & set) is fast Typically a one-liner Reading: Ch. 23 • Adding at end is fast, except when we have to grow • Adding or removing in the middle is slow: requires sliding all later elements 11/30/2003 (c) 2001-3, University of Washington 15-1 11/30/2003 (c) 2001-3, University of Washington 15-2 A Different Strategy: Lists via Links Links Instead of packing all elements together in an array, • For each element in the list, create a Link object • Each Link points to the data item (element) at that position, and also points to the next Link in the chain create a linked chain of all the elements Link next item 11/30/2003 (c) 2001-3, University of Washington 15-3 11/30/2003 (c) 2001-3, University of Washington 15-4 Linked Links Linked List • Each Link points to the next • The List has a reference to the first Link • No limit on how many can be linked! • Altogether, the list involves 3 different object types • A null reference signals the end null null List Link Link next next item item first Link Link next next item item Link Link next next item item 11/30/2003 (c) 2001-3, University of Washington 15-5 11/30/2003 (c) 2001-3, University of Washington 15-6 CSE143 Au03 15-1

  2. Link Class: Data Link Constructor /** Link for a simple list */ /** Link for a simple list */ public class Link { public class Link { public Object item ; // data associated with this link public Object item; // data associated with this link public Link next ; // next Link, or null if no next link public Link next; // next Link, or null if none //no more instance variables Link //but maybe some methods /** Construct new link with given data item and next link (or null if none) next */ } //end Link item public Link (Object item, Link next) { Link this.item = item; this.next = next; next Note 1: This class does NOT represent the chain, only one link of a chain item } Note 2: “public” violates usual rules – but appropriate in this context … Note 3: The links are NOT part of the data. The data is totally unaware that it is part of a chain. } 11/30/2003 (c) 2001-3, University of Washington 15-7 11/30/2003 (c) 2001-3, University of Washington 15-8 Exercise: Add a Node (1) Exercise: Add a Node (2) • Suppose we’ve got a linked list containing “lion”, • Now, write the code needed to insert “wolf” between “tiger”, and “bear” in that order, with a variable pointing “tiger” and “bear” to the head of the list Link head; // first link in the list, or null if list is empty • Draw a picture of the list 11/30/2003 (c) 2001-3, University of Washington 15-9 11/30/2003 (c) 2001-3, University of Washington 15-10 Exercise: Delete a Node (1) Exercise: Delete a Node (2) • Suppose we’ve got a list containing “IBM”, “Dell”, • Now, write the code needed to delete “Compaq” from “Compaq”, and “Apple” in that order the list Link head; // first link in the list, or null if list is empty • Draw a picture 11/30/2003 (c) 2001-3, University of Washington 15-11 11/30/2003 (c) 2001-3, University of Washington 15-12 CSE143 Au03 15-2

  3. LinkedList Data LinkedList Data & Constructor /** Simple version of LinkedList for CSE143 lecture example */ /** Simple version of LinkedList for CSE143 lecture example */ public class SimpleLinkedList implements List { public class SimpleLinkedList implements List { // instance variables // instance variables private Link first; // first link in the list, or null if list is empty private Link first ; // first link in the list, or null if list is empty … … List } /** construct new empty list */ public SimpleLinkedList( ) { List first = null; // no links yet! first } first … 11/30/2003 (c) 2001-3, University of Washington 15-13 11/30/2003 (c) 2001-3, University of Washington 15-14 List Interface (review) Method add (First Try) • Operations to implement: public boolean add(Object o) { int size( ) // create new link and place at end of list: boolean isEmpty( ) Link newLink = new Link(o, null); boolean add(Object o) // find last link in existing chain: it's the one whose next link is null: boolean addAll(Collection other) Link p = first; void clear( ) while (p.next != null) { Object get(int pos) boolean set(int pos, Object o) p = p.next; int indexOf(Object o) } boolean contains(Object o) // found last link; now add the new link after it: Object remove(int pos) p.next = newLink; boolean remove(Object o) return true; // we changed the list => return true boolean add(int pos, Object o) Iterator iterator( ) } • What isn’t included here?? 11/30/2003 (c) 2001-3, University of Washington 15-15 11/30/2003 (c) 2001-3, University of Washington 15-16 Problems with naïve add method Draw the Picture • Client code: • Inefficient: requires traversal of entire list to get to the end LinkedList vertexes = new SimpleLinkedList(); Point2D p1 = new Point2D.Double(100.0, 50.0); • One loop iteration per link Point2D p2 = new Point2D.Double( 250, 310); • Gets slower as list gets longer Point2D p3 = new Point2D.Double(90, 350.0); vextexes.add(p1); • Solution?? vertexes.add(p2); vertexes.add(p3); vertexes.add(p1); • Buggy: fails when adding first link to an empty list • Check the code: where does it fail? • Solution?? 11/30/2003 (c) 2001-3, University of Washington 15-17 11/30/2003 (c) 2001-3, University of Washington 15-18 CSE143 Au03 15-3

  4. Improvements to naïve add method List Data & Constructor (revised) public class SimpleLinkedList implements List { • Inefficient: requires traversal of entire list to get to the // instance variables end private Link first; // first link in the list, or null if list is empty • A solution: Change LinkedList to keep a pointer to last link as private Link last; // last link in the list, or null if list is empty well as the first … /** construct new empty list */ • Buggy: fails when adding first link to an empty list public SimpleLinkedList( ) { • A solution: check for this case and execute special code first = null; // no links yet! last = null; // no links yet! } • Q: “Couldn’t we ....?” Answer: “probably”. There are many ways link lists could be implemented … 11/30/2003 (c) 2001-3, University of Washington 15-19 11/30/2003 (c) 2001-3, University of Washington 15-20 Link List with last Method add (Final Version) public boolean add(Object o) { null List // create new link to place at end of list: Link Link newLink = new Link(o, null); // check if adding the first link last next if (first == null) { item first Link // we're adding the first link first = newLink; next last } else { item Link // we have some existing links; add the new link after the old last link last.next = newLink; next } item // update the last link last = newLink; return true; // we changed the list => return true } 11/30/2003 (c) 2001-3, University of Washington 15-21 11/30/2003 (c) 2001-3, University of Washington 15-22 Method size( ) Method size (faster) • First try it with this restriction: you can’t add or redefine • Add an instance variable to the list class instance variables int numLinks; // number of links in this list • Add to constructor: • Hint: count the number of links in the chain List n numLinks = 0; /** Return size of this list */ public int size( ) { • Add to method add : numLink int count = 0; last s numLinks ++; first • Method size /** Return size of this list */ public int size( ) { return numLinks; return count; } } • Critique? • Critique? 11/30/2003 (c) 2001-3, University of Washington 15-23 11/30/2003 (c) 2001-3, University of Washington 15-24 CSE143 Au03 15-4

Recommend


More recommend