Overview • Topics • Nodes and Links • List implementation using linked lists CSE 143 Java – Autumn 2001 • Reading: • Textbook: Ch. 23 Linked Lists 11/19/2001 (c) 2001, University of Washington 13-1 11/19/2001 (c) 2001, University of Washington 13-2 List Representation Linked List Example • A linked list is a collection of nodes • Linked list containing strings “apple”, “banana”, and “pear” • Each node contains a reference to • Data item • Next node in the list, or null if this is the last node in the list • Representation public class Node { public Object item; // data item referred to by this node public Node next; // next node in this list } 11/19/2001 (c) 2001, University of Washington 13-3 11/19/2001 (c) 2001, University of Washington 13-4 Exercise Exercise • Draw the picture that results from executing the following: • Suppose we’ve got a linked list containing “lion”, “tiger”, “bear”, in that order. Insert “wolf” between “tiger” and “bear” Node front = new Node( ); front.item = “puzzle”; Node head; // first node in the list front.next = new Node( ); • Draw a picture: front.next.item = “mystery”; front.next.next = null; Object data = front.item; front = front.next; 11/19/2001 (c) 2001, University of Washington 13-5 11/19/2001 (c) 2001, University of Washington 13-6 13-1
Exercise Implementing Lists with Linked Lists • First, let’s augment our node class with a constructor (for • Suppose we’ve got a list containing “Apple”, “Dell”, convenience) “Compaq”, and “IBM” in that order. Delete “Compaq” from // Node for a simple list the list public class Node { Node head; // first node Object item; // data associated with this node • Picture: Node next; // next Node or null if none // construct new node with given data item and next node public Node(Object item, Node next) { this.item = item; this.next = next; } } 11/19/2001 (c) 2001, University of Washington 13-7 11/19/2001 (c) 2001, University of Washington 13-8 Collection Interface (review) List Interface (review) • Methods defined in interface Container • Additional methods defined in interface List int size( ) – # of items currently in the collection Object get(int pos) – return element at position pos boolean set(int pos, Object elem) – store elem at position pos isEmpty( ) – (size( ) == 0) boolean add(int pos, Object elem) – store elem at position pos; slide elements boolean contains(Object o) – true of o.equals(element) for an element in the at position pos to size( )-1 up one position to the right collection boolean remove(int pos) – remove item at given position; shift remaining boolean add(Object o) – ensure that o is in the collection by adding it if needed; elements to the left to fill the gap return true if collection altered; false if not. int indexOf(Object o) – return position of first occurrence of o in the list, or boolean addAll(Collection other) – add all elements in the other collection -1 if not found boolean remove(Object o) – remove a single instance of o from the collection; boolean equals(Object o) – true if o is another ArrayList that is element by return true if something was actually removed element equal to this one void clear( ) – remove all elements • Precondition for most of these is 0 <= pos < size( ) Iterator iterator( ) – return an iterator object for this collection • We will implement a subset of these 11/19/2001 (c) 2001, University of Washington 13-9 11/19/2001 (c) 2001, University of Washington 13-10 List Data & Constructor Method add // Simple version of LinkedList for CSE143 lecture example • First cut: add new node to existing list class SimpleLinkedList implements List { public boolean add(Object o) { // instance variables // create new node to place at end of list private Node head; // first node in the list, or null if list is empty Node newNode = new Node(o, null); … // find end of list and place new node there // construct new empty list Node p = head; while (_____________________) { public SimpleLinkedList( ) { head = null; _________________________ } } p.next = newNode; return true; } • Critique? 11/19/2001 (c) 2001, University of Washington 13-11 11/19/2001 (c) 2001, University of Washington 13-12 13-2
Problems with naïve add method List Data & Constructor (revised) // Simple version of LinkedList for CSE143 lecture example • Inefficient: requires traversal of entire list to get to the end class SimpleLinkedList implements List { • Buggy: fails when adding first node to an empty list // instance variables • Possible solutions private Node head; // first node in the list, or null if list is empty private Node tail; // last node in the list, or null if list is empty • Remember where the last node is (extra instance variable) … • Special case code for adding first node // construct new empty list public SimpleLinkedList( ) { head = null; tail = null; } 11/19/2001 (c) 2001, University of Washington 13-13 11/19/2001 (c) 2001, University of Washington 13-14 Method add Method add (check) public boolean add(Object o) { • Trace: create a short list // create new node to place at end of list SimpleLinkedList list = new SimpleLinkedList( ); Node newNode = new Node(o, null); list.add(“Huey”); if (head == null) { list.add(“Dewey”); // add first node to empty list list.add(“Louie”); head = newNode; tail = newNode; • picture: } else { // add new node to non-empty list tail.next = newNode; tail = newNode; } return true; } 11/19/2001 (c) 2001, University of Washington 13-15 11/19/2001 (c) 2001, University of Washington 13-16 Method size Method size (faster) • This is simple • Add an instance variable to the list class int size; // number of nodes in this list /** Return size of this list */ • In constructor public int size( ) { int numNodes = 0; size = 0; Node p = head; • In method add while (p != null) { size++; numNodes++; • Method size p = p.next; /** Return size of this list */ } public int size( ) { } return size; • critique? } • Critique? 11/19/2001 (c) 2001, University of Washington 13-17 11/19/2001 (c) 2001, University of Washington 13-18 13-3
clear get • Simpler than with arrays • Access a list element by position /** Return object at position pos of this list. Precondition: 0 <= pos < size( ) */ /** Clear this list */ public Object get(int pos) { public void clear( ) { if (pos < 0 || pos >= size) { head = null; throw new IndexOutOfBoundsException( ); tail = null; } size = 0; Node p = head; } for (int k = 0; k < pos; k++) { p = p.next; } return p.item; } • Compare to get for ArrayList: what’s different? • set is similar, and left as an exercise 11/19/2001 (c) 2001, University of Washington 13-19 11/19/2001 (c) 2001, University of Washington 13-20 indexOf add and remove given position • Sequential search for first “equal” object • Observation: to add or remove a node at position k, we need to alter the node at position k-1 /** return first location of object o in this list if found, otherwise return –1 */ public int indexOf(Object o) { • Useful private method: get a reference to a node given its Node p = head; int location = 0; position while (p != null && ! p.item.equals(o)) { // Return a reference to the node at position pos p = p.next; location++; // assumption: 0 <= pos < size } private Node getNodeAtPos(int pos) { if (p != null) { p = head; return location; for (int k = 0; k < pos; k++) { } else { p = p.next; return –1; } } return p; • Any special cases to worry about? } 11/19/2001 (c) 2001, University of Washington 13-21 11/19/2001 (c) 2001, University of Washington 13-22 remove at position Exercise: add at position /** Remove the object at position pos from this list. List changes, so return true /** Add object o at position pos in this list. List changes, so return true * precondition: 0 <= pos < size */ * precondition: 0 <= pos < size */ public boolean remove(int pos) { public boolean add(int pos, Object o) { if (pos < 0 || pos >= size) { if (pos < 0 || pos >= size) { throw new IndexOutOfBoundsException( ); throw new IndexOutOfBoundsException( ); } } if (pos == 0) { head = head.next; if (head == null) { tail = null; } } else { Node prev = getNodeAtPos(pos-1); prev.next = prev.next.next; } size --; return true; return true; } } 11/19/2001 (c) 2001, University of Washington 13-23 11/19/2001 (c) 2001, University of Washington 13-24 13-4
Recommend
More recommend