Download presentation
Presentation is loading. Please wait.
1
Lists and Iterators (Chapter 6) COMP53 Oct 22, 2007
2
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
3
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()
4
In the Java Vector class these 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)
5
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 012n r
6
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 012n r V 012n r V 012n o r
7
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 012n r V 012n o r V 012n r
8
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
9
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.)
10
Extendable Arrays size, capacity and A[] are fields of the ArrayIndexList 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; }
11
UML for IndexList
12
UML for ArrayIndexList
13
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
14
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)
15
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 trailer header nodes/positions elements prevnext elem node
16
Insertion Operation addAfter(p, X), which returns position q ABC p ABC p X q ABXC pq
17
Insertion Algorithm Algorithm addAfter(p,e): Create a new node v v.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}
18
ABCD p Deletion Operation remove(p) ABC D p ABC
19
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
20
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
21
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
22
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
23
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
24
What’s next? Designing and implementing iterators Java Collections Framework
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.