13-1 Exercise Implementing Lists with Linked Lists First, lets - - PDF document

13 1
SMART_READER_LITE
LIVE PREVIEW

13-1 Exercise Implementing Lists with Linked Lists First, lets - - PDF document

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,


slide-1
SLIDE 1

13-1

11/19/2001 (c) 2001, University of Washington 13-1

CSE 143 Java – Autumn 2001

Linked Lists

11/19/2001 (c) 2001, University of Washington 13-2

Overview

  • Topics
  • Nodes and Links
  • List implementation using linked lists
  • Reading:
  • Textbook: Ch. 23

11/19/2001 (c) 2001, University of Washington 13-3

List Representation

  • A linked list is a collection of nodes
  • 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-4

Linked List Example

  • Linked list containing strings “apple”, “banana”, and “pear”

11/19/2001 (c) 2001, University of Washington 13-5

Exercise

  • Draw the picture that results from executing the following:

Node front = new Node( ); front.item = “puzzle”; front.next = new Node( ); front.next.item = “mystery”; front.next.next = null; Object data = front.item; front = front.next;

11/19/2001 (c) 2001, University of Washington 13-6

Exercise

  • Suppose we’ve got a linked list containing “lion”, “tiger”,

“bear”, in that order. Insert “wolf” between “tiger” and “bear”

Node head; // first node in the list

  • Draw a picture:
slide-2
SLIDE 2

13-2

11/19/2001 (c) 2001, University of Washington 13-7

Exercise

  • Suppose we’ve got a list containing “Apple”, “Dell”,

“Compaq”, and “IBM” in that order. Delete “Compaq” from the list

Node head; // first node

  • Picture:

11/19/2001 (c) 2001, University of Washington 13-8

Implementing Lists with Linked Lists

  • First, let’s augment our node class with a constructor (for

convenience)

// Node for a simple list public class Node { Object item; // data associated with this node 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-9

Collection Interface (review)

  • Methods defined in interface Container

int size( ) – # of items currently in the collection isEmpty( ) – (size( ) == 0) boolean contains(Object o) – true of o.equals(element) for an element in the collection boolean add(Object o) – ensure that o is in the collection by adding it if needed; return true if collection altered; false if not. boolean addAll(Collection other) – add all elements in the other collection boolean remove(Object o) – remove a single instance of o from the collection; return true if something was actually removed void clear( ) – remove all elements Iterator iterator( ) – return an iterator object for this collection

11/19/2001 (c) 2001, University of Washington 13-10

List Interface (review)

  • Additional methods defined in interface List

Object get(int pos) – return element at position pos boolean set(int pos, Object elem) – store elem at position pos boolean add(int pos, Object elem) – store elem at position pos; slide elements at position pos to size( )-1 up one position to the right boolean remove(int pos) – remove item at given position; shift remaining elements to the left to fill the gap int indexOf(Object o) – return position of first occurrence of o in the list, or

  • 1 if not found

boolean equals(Object o) – true if o is another ArrayList that is element by element equal to this one

  • Precondition for most of these is 0 <= pos < size( )
  • We will implement a subset of these

11/19/2001 (c) 2001, University of Washington 13-11

List Data & Constructor

// Simple version of LinkedList for CSE143 lecture example class SimpleLinkedList implements List { // instance variables private Node head; // first node in the list, or null if list is empty … // construct new empty list public SimpleLinkedList( ) { head = null; }

11/19/2001 (c) 2001, University of Washington 13-12

Method add

  • First cut: add new node to existing list

public boolean add(Object o) { // create new node to place at end of list Node newNode = new Node(o, null); // find end of list and place new node there Node p = head; while (_____________________) { _________________________ } p.next = newNode; return true; }

  • Critique?
slide-3
SLIDE 3

13-3

11/19/2001 (c) 2001, University of Washington 13-13

Problems with naïve add method

  • Inefficient: requires traversal of entire list to get to the end
  • Buggy: fails when adding first node to an empty list
  • Possible solutions
  • Remember where the last node is (extra instance variable)
  • Special case code for adding first node

11/19/2001 (c) 2001, University of Washington 13-14

List Data & Constructor (revised)

// Simple version of LinkedList for CSE143 lecture example class SimpleLinkedList implements List { // instance variables 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 … // construct new empty list public SimpleLinkedList( ) { head = null; tail = null; }

11/19/2001 (c) 2001, University of Washington 13-15

Method add

public boolean add(Object o) { // create new node to place at end of list Node newNode = new Node(o, null); if (head == null) { // add first node to empty list head = newNode; tail = newNode; } else { // add new node to non-empty list tail.next = newNode; tail = newNode; } return true; }

11/19/2001 (c) 2001, University of Washington 13-16

Method add (check)

  • Trace: create a short list

SimpleLinkedList list = new SimpleLinkedList( ); list.add(“Huey”); list.add(“Dewey”); list.add(“Louie”);

  • picture:

11/19/2001 (c) 2001, University of Washington 13-17

Method size

  • This is simple

/** Return size of this list */ public int size( ) { int numNodes = 0; Node p = head; while (p != null) { numNodes++; p = p.next; } }

  • critique?

11/19/2001 (c) 2001, University of Washington 13-18

Method size (faster)

  • Add an instance variable to the list class

int size; // number of nodes in this list

  • In constructor

size = 0;

  • In method add

size++;

  • Method size

/** Return size of this list */ public int size( ) { return size; }

  • Critique?
slide-4
SLIDE 4

13-4

11/19/2001 (c) 2001, University of Washington 13-19

clear

  • Simpler than with arrays

/** Clear this list */ public void clear( ) { head = null; tail = null; size = 0; }

11/19/2001 (c) 2001, University of Washington 13-20

get

  • Access a list element by position

/** Return object at position pos of this list. Precondition: 0 <= pos < size( ) */ public Object get(int pos) { if (pos < 0 || pos >= size) { throw new IndexOutOfBoundsException( ); } 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-21

indexOf

  • Sequential search for first “equal” object

/** return first location of object o in this list if found, otherwise return –1 */ public int indexOf(Object o) { Node p = head; int location = 0; while (p != null && ! p.item.equals(o)) { p = p.next; location++; } if (p != null) { return location; } else { return –1; }

  • Any special cases to worry about?

11/19/2001 (c) 2001, University of Washington 13-22

add and remove given position

  • Observation: to add or remove a node at position k, we need

to alter the node at position k-1

  • Useful private method: get a reference to a node given its

position

// Return a reference to the node at position pos // assumption: 0 <= pos < size private Node getNodeAtPos(int pos) { p = head; for (int k = 0; k < pos; k++) { p = p.next; } return p; }

11/19/2001 (c) 2001, University of Washington 13-23

remove at position

/** Remove the object at position pos from this list. List changes, so return true * precondition: 0 <= pos < size */ public boolean remove(int pos) { if (pos < 0 || pos >= size) { 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; }

11/19/2001 (c) 2001, University of Washington 13-24

Exercise: add at position

/** Add object o at position pos in this list. List changes, so return true * precondition: 0 <= pos < size */ public boolean add(int pos, Object o) { if (pos < 0 || pos >= size) { throw new IndexOutOfBoundsException( ); } return true; }