Lists and Iterators (Chapter 6) COMP53 Oct 22, 2007.

24
Lists and Iterators (Chapter 6) COMP53 Oct 22, 2007
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    224
  • download

    2

Transcript of Lists and Iterators (Chapter 6) COMP53 Oct 22, 2007.

Lists and Iterators(Chapter 6)

COMP53Oct 22, 2007

The Array List ADT (aka Vector or Index List)

• The ArrayList ADT extends the notion of an array by storing a sequence of arbitrary objects

• An element can be accessed, inserted or removed by specifying its rank (number of elements preceding it)

• An exception is thrown if an incorrect rank is specified; e.g.:– negative rank– rank larger than current size of the array list

The Array List ADT

• Main array list operations:– object get(integer r):

returns the element at rank r without removing it– object set(integer r, object o):

replace the element at rank r with o and return the old element– void insert(integer r, object o):

insert a new element o to have rank r– object remove(integer r):

removes and returns the element at rank r

All these operations need to check that r is a valid rank.

• Additional operations size() and isEmpty()

In the Java Vector classthese methods are renamed:

Java Vector:• size() and isEmpty()

• elementAt(int index)

• object setElementAt(object o, int index)

• insertElementAt(object o, int index)

• object remove(int index)

Array List ADT:• size() and isEmpty()

• get(integer r)

• object set(integer r, object o)

• add(integer r, object o)

• object remove(integer r)

Array-Based Implementation• Use an array V of capacity N• A variable n keeps track of the size of the array

(number of elements stored)• Operation get(r) is implemented in O(1)

time by returning V[r]

V

0 1 2 nr

Insertion• In operation set(r,o), we need to make

room for the new element by shifting forward elements V[r], …, V[n 1]

• In the worst case (r 0), this requires n operations (O(n) time)

V

0 1 2 nr

V

0 1 2 nr

V

0 1 2 no

r

Deletion• For remove(r), we need to fill the hole left by the

removed element by shifting backward the (n – r – 1) elements V[r 1], …, V[n 1]

• Also O(n) time

V

0 1 2 nr

V

0 1 2 no

r

V

0 1 2 nr

Performance

• The array-based implementation of an Array List:– The space used is O(n)

– size,isEmpty,get and set run in O(1) time– add and remove run in O(n) time

• In an add operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one

Extendable Arrays• A simple solution to the capacity limitation of arrays is to

replace a full array with a longer array, and copy elements from the original array to the new array.

• This can be done in O(n) time.(Does not increase the asymptotic efficiency of the add operation.)

Extendable Arrays• size, capacity and A[] are fields

of the ArrayIndexList<E> class,

if (size == capacity) { // increase capacity capacity *= 2; // create a new array E[] new_array = (E[]) new Object[capacity]; // copy references from old array for (int i=0; i<size; i++) new_array[i] = A[i]; // replace old array with new array A = new_array;}

UML for IndexList

UML for ArrayIndexList

Position List ADT(aka Node List)

• The Position List ADT specifies positions of elements relative to positions of other elements

• We’ll use a new kind of object, Position, as an abstraction of the location of some existing element

• Just one method:– object element(): returns the element stored at the

position

Position List ADT(aka Node List)

• The Node List ADT models a sequence of positions storing arbitrary objects

• It establishes a before/after relation between positions

• Generic methods:– size()– isEmpty()

• Accessor methods (all return type Position):– first(), last()– prev(p), next(p)

• Update methods:– set(p, e) – remove(p)– addBefore(p, e)– addAfter(p, e)– addFirst(e)– addLast(e)

Doubly Linked List

• A doubly-linked list provides a natural implementation of the Node List ADT

• Nodes implement Position and store:– element– link to the previous node– link to the next node

• Sentinel header and trailer nodes

trailerheader nodes/positions

elements

prev next

elem node

Insertion• Operation addAfter(p, X), which returns position q

A B C

p

A B C

p

X

q

A B X C

p q

Insertion Algorithm

Algorithm addAfter(p,e):Create a new node vv.setElement(e)v.setPrev(p) {link v to its predecessor}v.setNext(p.getNext()) {link v to its successor}(p.getNext()).setPrev(v) {link p’s old successor to v}p.setNext(v) {link p to its new successor, v}size ++return v {the position for the element e}

A B C D

p

Deletion• Operation remove(p)

A B C

D

p

A B C

Deletion Algorithm

Algorithm remove(p):t = p.element {temporary variable to hold the return value}(p.getPrev()).setNext(p.getNext()) {fix links around p}(p.getNext()).setPrev(p.getPrev())p.setPrev(null){invalidating the position p}p.setNext(null)

size --return t

Performance

• List ADT using a doubly-linked list:– The space used by a list with n elements is O(n)– All the operations of the List ADT run in O(1)

time– Operation element() of the

Position ADT runs in O(1) time

Iterators• An iterator abstracts the process of scanning through a

collection of elements• Methods of the Iterator ADT:

– element element()– boolean hasNext()– element nextElement()– reset()

• Extends the concept of Position by adding a traversal capability

• In other contexts, iterators may be called cursors– example: SQL databases

Iterators

• An iterator is typically associated with an another data structure

• Data structures that support iterators add a method to create and iterator:– ElementIterator iterator()

• Two notions of iterator:– snapshot: freezes the contents of the data structure at a given

time– dynamic: follows changes to the data structure

Using Iterators

Iterator iter = mylist.iterator();while (iter.hasNext()){ Object obj = iter.next(); // do something with this object}

mylist is a reference to some data structure that supports iterators

What’s next?

• Designing and implementing iterators• Java Collections Framework