# Introduction to Computation and Problem

## Presentation on theme: "Introduction to Computation and Problem"— Presentation transcript:

Introduction to Computation and Problem
Solving Class 26: Linked Lists Prof. Steven R. Lerman and Dr. V. Judson Harward

The Java® Collection Classes
• The java.util package contains implementations of many data structures that we are also going to discuss and implement in a simpler way in class. • You are welcome to use the standard Java® implementations in problem sets. They are more elaborate and abstract than the implementations we will develop. • Learning how to implement a few data structures makes you more sophisticated in how you use all data structures. • We will cover the Java® implementations. 2

Lists as an Abstract Data Type
A list is a collection of elements that has a particular order. – It can have arbitrary length. – You should be able to insert or delete an element anywhere. – You should be able to go through the list in order an element at a time. 3

A List Interface public interface List { public boolean isEmpty();
public void addFirst( Object o ); public void addLast( Object o ); public boolean contains(Object o); public boolean remove(Object o); public Object removeFirst() throws NoSuchElementException; public void clear(); public int size(); } 4

Lists and Ordinal Position
• There are certain obvious things we would like to do with lists that we can't do using only this interface. Two examples: – How would you access the elements of the list if you did not know them in advance? – How would you insert an item into the list at any other position than the beginning and the end? • One approach is to number or index the positions in the list. 5

Indexed Lists • We could then add two methods
– public Object get( int n ) throws NoSuchElementException; – public void insert( Object o, int n ) • The following code block will traverse an indexed list, myList: for ( int i = 0; i < myList.size(); i++ ) { Object o = myList.get( i ); . . . } 6

Indexed Lists, 2 • Lists implemented using arrays often provide such
methods because they are easy to implement. • The idea of using an index to access list members can lead to problems, however. – Since the index depends on ordinal position, it changes every time an item is added to or deleted from the list. – If the list is not implemented on top of an indexed data structure like an array, accessing an indexed element is slow. • When we use large lists like a telephone directory in everyday life, we don't consider the index of an entry, only its relative position. 7

Iterators • An iterator is a helper class designed to
be used with a List or other collection class. • It has methods to return the collection's members one at a time. • Iterators can also implement methods that allow the collection to be modified relative to the current position of the iterator. 8

ListIterator Interface
public interface ListIterator { public boolean hasNext(); public Object next() throws NoSuchElementException; public void remove() throws IllegalStateException; public void add( Object o ); public void set( Object o ) throws IllegalStateException } 9

Iterator Methods • The type of iterator we present here returns a new element and advances to the next with the same operation, next(). There is no way to go back using this interface. The Java® ListIterator allows you to go forwards and backwards. • The most recent element returned by next() is the current element. • remove() will delete the current element from the underlying collection. set() will change it. • add() will insert a new element after the current element and before the element that would be returned by the following call to next(). After a call to add(), the inserted element becomes the new current. A call to next() will return the element after the inserted one. • The first call to next() returns the first element of the list. 10

Iterator and its Underlying List
• An iterator is an object based on an underlying collection so we need a way to create an iterator for a collection. • We do this by adding a method to our List interface: public ListIterator listIterator(); • Can you have 2 iterators over the same List? 11

Using an Iterator List myList = new SLinkedList(); . . .
ListIterator iter = myList.listIterator(); . . . while ( iter.hasNext() ) { Object o = iter.next(); } 12

An Iterator in Action After 1st call to next() New Iterator current
undefined green red purple orange current is green green red purple orange After 2nd call to next() After adding black current is black current is red green black red purple orange green black red purple orange 13

An Iterator in Action, 2 After 3rd call to After calling next()
remove() current is purple green black purple orange green black purple orange current is undefined 14

List Implementation Stategies
• There are many ways to implement a list. • In array-based implementations like the Java® Vector inserting an element at any place except the end of the list is very expensive because all the elements from the point of insertion until the end must be moved back to make room for the new entry. There is a similar problem with deletion. • For this reason, lists frequently use a linked implementation. 15

• Each item to be put on the list is contained in an instance of a new type of object called the link corresponding to a freight car. • In a singly linked list, the link not only contains the listed item, but it also points to the next item in the list as one freight car is coupled to the next. • The last link in the list points to nothing. 16

17

Singly Linked Lists, 2 • The list object itself points to the link holding the first listed item, and often holds an additional reference to the last link of the list to make it easier to append items. • We say that a link “contains” or “points to”, and the list instance “points” or “holds a pointer”. In Java® these are all synonyms for holding a reference. • So the link doesn't really contain the item. It simply holds a reference to the listed item. • The last link holds a null reference in the field that points to the next element. 18

List: List interface Iterator: List iterator interface SLinkedList: List implementation SLinkedListApp: main() application SLinkedListView: List GUI IteratorView: List iterator GUI 19

private static class SLink { Object item; SLink next; SLink( Object o, SLink n ) { item = o; next = n; } SLink( Object o ) { this( o, null ); } } . . . 20

Generic vs. Typed Lists • The List interface we have specified is general like the Java® Vector class. It stores and retrieves Objects. • If you are writing your own list class, and you know you will only be handling Strings, say, you can replace the Object fields with String fields. For example, private static class SLink { String item; SLink next; SLink( String o, SLink n ) { item = o; next = n; } SLink( Object o ) { this( o, null ); } } public void addFirst( String o ); 21

• Only first is necessary. • last and length could be found by traversing the list, but having these members and keeping them up to date makes the calls size() and append() much faster. private int length = 0; private SLink first = null; private SLink last = null; 22

== vs the Object equals method
• contains( Object o ) and remove( Object o ) must search for Object o in the list. What does it means to find it? • Must the list contain a reference to the identical object (==)? Or is it sufficient that the list contain a reference to an object that is equal but possibly distinct? static private boolean objectEquals( Object a, Object b ) { if ( a == null ) return ( b == null ); else return a.equals( b ); } 23

Beware the Special Case
• The tricky part about implementing a linked list is not implementing the normal case for each of the methods, for instance, removing an object from the middle of the list. • What's tricky is making sure that your methods will work in the exceptional and boundary cases. • For each method, you should think through whether the implementation will work on – an empty list, – a list with only one or two elements, – on the first element of a list, – on the last element of a list. 24

removeFirst() public Object removeFirst()
throws NoSuchElementException { if ( first == null ) // if list is empty throw new NoSuchElementException(); else { SLink t = first; first = first.next; // if list had 1 element and is now empty if ( first == null ) last = null; length--; return t.item; } 25

removeFirst(), before 26

removeFirst(), after 27

removeFirst(), special case
28

if ( first == null ) // if the list is empty first = last = new SLink( o ); } else first = new SLink( o, first ); } length++; 29

32

Iterator as Inner Class
public ListIterator listIterator() { return new SLinkedListIterator(); } public class SLinkedListIterator implements ListIterator . . . 33

contains() public boolean contains(Object o) {
ListIterator iter = new SLinkedListIterator(); while ( iter.hasNext() ) { if ( objectEquals( iter.next(), o )) { return true; } } return false; 34