-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElementsIterator.java
More file actions
46 lines (32 loc) · 825 Bytes
/
Copy pathElementsIterator.java
File metadata and controls
46 lines (32 loc) · 825 Bytes
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
import java.util.Iterator;
import java.util.*;
public class ElementsIterator implements Iterator {
BTDList list;
BTDListNode current;
int size;
public ElementsIterator(BTDList list){
this.list=list;
current = (!list.isEmpty())?null:list.getFirstNode();
}
public Object next() {
if(!hasNext()){
throw new NoSuchElementException();
}
else{
Object temp = current.getItem();
current = (current.getNext()==list.getHead()?null:current.getNext());
return temp;
}
}
public void remove() {
if(size==0){
throw new NoSuchElementException();
}else{
Object removedNode= list.removeNode(current);
size--;
System.out.println(removedNode.toString()+" removed from Binary Tree" );
}
}
public boolean hasNext() {
return (current!=null); }
}