-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStockListIterator.java
More file actions
52 lines (45 loc) · 1.32 KB
/
Copy pathStockListIterator.java
File metadata and controls
52 lines (45 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.Iterator;
import java.util.LinkedList;
import java.util.NoSuchElementException;
public class StockListIterator implements Iterator<Stock> {
// Add code to implement an iterator for Stocks.
// This will be similar to the iterator in OrderedStockList
// with a few small changes.
private Node cursor;
/**
* first call next() flag.
* <p>
* If first call next(),
* then will return front stock of the OrderedStockList,
* otherwise, return the real next stock.
* <p>
* So you could use it for iterate like this:
* while(iterator.hasNext()){
* System.out.println(iterator.next());
* }
*/
private boolean firstCall;
public StockListIterator(OrderedStockList stockList) {
this.cursor = stockList.getFront();
this.firstCall = true;
}
@Override
public boolean hasNext() {
if (this.cursor == null) {
return false;
}
return this.cursor.getNext() != null;
}
@Override
public Stock next() {
if (!this.hasNext()) {
throw new NoSuchElementException();
}
if (this.firstCall) {
firstCall = false;
return this.cursor.getData();
}
this.cursor = this.cursor.getNext();
return this.cursor.getData();
}
}