-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack iterator
More file actions
41 lines (39 loc) · 930 Bytes
/
Stack iterator
File metadata and controls
41 lines (39 loc) · 930 Bytes
1
// Stack iterator£ºlinked-list implementationimport java.util.Iterator;public class Stack<Item> implements Iterable<Item> { public Iterator<Item> iterator£¨£©{ return new ListIterator(); } private class ListIterator implements Iterator<Item>{ private Node current = first; public boolean hasNext(){ return current !=null; } public void remove() { /*not supported*/ } public Item next() { Item item = current. item; current = current.next; return item; } }}// Stack iterator: array implementationimport java.util.Iterator;public class Stack<Item> implements Iterable<Item> { public Iterator<Item> iterator () { return new ReverseArrayIterator(); } private class ReverseArrayIterator implements Iterator <Item> { private int i = N ; pubklic boolean hasNext() { return i>0; } public void remove() { /*not supported */ } public Item next() { return s[--i]; }}