Download presentation
Presentation is loading. Please wait.
Published byReginald Fields Modified over 6 years ago
1
Need for Iterators O(n2) this is a common application
7 2 T get(int at) 6 1 4 for (int i=0; i< L.size(); i++) { ..... L.get(i)..... } 3 5 this is a common application 8 Suppose L.size() = n n = (1+n)*n/2 O(n2) O(n) ? We cannot improve this, because the internal information and structures are protected. But, 12/1/2018 IT 179
2
RandomAccess vs Linked List
static int countEven(MyList<Integer> x) { int count=0; if (x instanceof RandomAccess) { for (int i=0;i<x.size();i++) if (x.get(i)%2 == 0) count++; } else { Iterator<Integer> xi = x.iterator(); while (xi.hasNext()) if (xi.next()%2 == 0) count++; } return count; 12/1/2018 IT 179
3
Iterable Another inner class that defines our iterator.
Iterable<T> {interface} + Iterator<T> iterator(); MyList<T> {interface} (see the java document) …. package myUtil; import java.util.Iterator; import java.util.NoSuchElementException; public class MySLinkedList <T> implements MyList<T>{ { ..... public Iterator<T> iterator() return new myIterator(); } Another inner class that defines our iterator. 12/1/2018 IT 179
4
API java.util.Iterator<E>
Iterator<T> {interface} + boolean hasNext(); + T next(); + void remove(); API java.util.Iterator<E> head 1 2 3 4 5 6 nextNode Point to the node that will be returned by next call of next() seenNode Point to the node that is seen (may be removed later) prevNode Point to the node before the seen node. 12/1/2018 IT 179
5
myIterator inner class
package myUtil; import java.util.Iterator; import java.util.NoSuchElementException; public class MySLinkedList<T> implements MyList<T>, Iterable<T>{ ..... /***** This is an inner class for myIterator ************/ public class myIterator implements Iterator<T> { private Node<T> prevNode=null, seenNode=null, nextNode=head; // current node to be seen //by next() public boolean hasNext() { return (nextNode != null); } … /***** This is the end of inner class myIterator ************/ myIterator inner class 12/1/2018 IT 179
6
myIterator (countined)
/*****This is an inner class for myIterator ************/ public class myIterator implements Iterator<T> { public T next() { if (! hasNext()) throw new NoSuchElementException("NoSuchElementException: "); if (seenNode != null) // The seen node is not removed prevNode = seenNode; seenNode = nextNode; nextNode = nextNode.next; return seenNode.data; } /***** This is the end of inner class myIterator ************/ 12/1/2018 IT 179
7
myIterator (countined)
/*****This is an inner class for myIterator ************/ public class myIterator implements Iterator<T> { public void remove() { if (seenNode == null) throw new IllegalStateException("IllegalStateException: "); if (seenNode == head) { head = seenNode.next; } else { prevNode.next = nextNode; seenNode = null; // it's gone size--; /***** This is the end of inner class myIterator ************/ 12/1/2018 IT 179
8
Anonymous Class public Iterator<T> iterator() {
return new myIterator(); } internal class Anonymous Class public Iterator<T> iterator() { return new Iterator<T>() { /* Return an Anonymous class as an iterator ***/ …… /* same as the body the myIterator */ /* This is the end of the Anonymous class *****/ }; /** don't forget ";" **/ } /* end of iterator */ 12/1/2018 IT 179
9
A Marker Interface static <T extends RandomAccess> void swap(T x, int i, int j) { if (!(x instanceof List)) return; T temp = x.get(i); x.set(i,x.get(j)); x.set(j,temp); } public static void main(String[] args) { ArrayList<Object> A = new ArrayList<Object>(); LinkedList<Object> B = new LinkedList<Object>(); List<Object> C; swap(A,30,95); swap(B,30,95); C = B swap(C,30,95); 12/1/2018 IT 179
10
Single Linked List O(n2) 1+2+3+.....+n = (1+n)*n/2
It is very common to add new items to the end of the list public void add(T item) { // append item to the end of the list add(size, item); } head Easy Solution: tail 52 Can Iterator help? No! 12 14 16 23 45 48 n = (1+n)*n/2 O(n2) for (int i=0; i<n; i++) { something = list.add(something); } Add to the tail (append) 12/1/2018 IT 179
11
API java.util.ListIterator<T>
{interface} + boolean hasNext(); + boolean hasPrevious(); + T next(); + T previous(); + void remove(); + int nextIndex() + int previousIndex(); + void add(T item); + void set(T item); Tail doesn’t help head tail 1 2 3 4 nextNode Point to the node that will be returned by next call of next() seenNode Point to the node that is seen (may be removed later) prevNode Point to the node before the seen node. 12/1/2018 IT 179
12
Single Linked List O(n2) 1+2+3+.....+n = (1+n)*n/2
It is very common to add new items to the end of the list public void add(T item) { // append item to the end of the list add(size, item); } head Easy Solution: tail 52 Can Iterator help? No! 12 14 16 23 45 48 n = (1+n)*n/2 O(n2) for (int i=0; i<n; i++) { something = list.add(something); } Add to the tail (append) 12/1/2018 IT 179
13
API java.util.ListIterator<T>
{interface} + boolean hasNext(); + boolean hasPrevious(); + T next(); + T previous(); + void remove(); + int nextIndex() + int previousIndex(); + void add(T item); + void set(T item); Tail doesn’t help head tail 1 2 3 4 nextNode Point to the node that will be returned by next call of next() seenNode Point to the node that is seen (may be removed later) prevNode Point to the node before the seen node. 12/1/2018 IT 179
14
API java.util.ListIterator<E>
ListIterator<T> {interface} + boolean hasNext(); + boolean hasPrevious(); + T next(); + T previous(); + void remove(); + int nextIndex() + int previousIndex(); + void add(T item); + void set(T item); head tail 1 2 3 4 nextNode Point to the node that will be returned by next call of next() seenNode Point to the node that is seen (may be removed later) 12/1/2018 IT 179
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.