/* 高手之作,本人謹以收藏者身份共用源碼,供大家參考之! */
/*
* 迭代器ADT介面
*/
package dsa;
public interface Iterator {
boolean hasNext();//檢查迭代器中是否還有剩餘的元素
Object getNext();//返回迭代器中的下一元素
}
/*
* 基於列表實現的元素迭代器
*/
package dsa;
public class IteratorElement implements Iterator {
private List list;//列表
private Position nextPosition;//當前(下一個)元素的位置
//預設構造方法
public IteratorElement() { list = null; }
//構造方法
public IteratorElement(List L) {
list = L;
if (list.isEmpty())//若列表為空白,則
nextPosition = null;//當前元素置空
else//否則
nextPosition = list.first();//從第一個元素開始
}
//檢查迭代器中是否還有剩餘的元素
public boolean hasNext() { return (null != nextPosition); }
//返回迭代器中的下一元素
public Object getNext() throws ExceptionNoSuchElement {
if (!hasNext()) throw new ExceptionNoSuchElement("意外:沒有下一元素");
Position currentPosition = nextPosition;
if (currentPosition == list.last())//若已到達尾元素,則
nextPosition = null;//不再有下一元素
else//否則
nextPosition = list.getNext(currentPosition);//轉向下一元素
return currentPosition.getElem();
}
}
/*
* 基於列表實現的位置迭代器
*/
package dsa;
public class IteratorPosition implements Iterator {
private List list;//列表
private Position nextPosition;//當前(下一個)位置
//預設構造方法
public IteratorPosition() { list = null; }
//構造方法
public IteratorPosition(List L) {
list = L;
if (list.isEmpty())//若列表為空白,則
nextPosition = null;//當前位置置空
else//否則
nextPosition = list.first();//從第一個位置開始
}
//檢查迭代器中是否還有剩餘的位置
public boolean hasNext() { return (nextPosition != null); }
//返回迭代器中的下一位置
public Object getNext() throws ExceptionNoSuchElement {
if (!hasNext()) throw new ExceptionNoSuchElement("意外:沒有下一位置");
Position currentPosition = nextPosition;
if (currentPosition == list.last())//若已到達尾位置,則
nextPosition = null;//不再有下一個位置
else//否則
nextPosition = list.getNext(currentPosition);//轉向下一位置
return currentPosition;
}
}
/**
* 基於列表實現的樹迭代器
*/
package dsa;
public class IteratorTree implements Iterator {
private List list;//列表
private Position nextPosition;//當前(下一個)元素的位置
//預設構造方法
public IteratorTree() { list = null; }
//前序走訪
public void elementsPreorderIterator(TreeLinkedList T) {
if (null == T) return;//遞迴基
list.insertLast(T);//首先輸出當前節點
TreeLinkedList subtree = T.getFirstChild();//從當前節點的長子開始
while (null != subtree) {//依次對當前節點的各個孩子
this.elementsPreorderIterator(subtree);//做前序走訪
subtree = subtree.getNextSibling();
}
}
//後序遍曆
public void elementsPostorderIterator(TreeLinkedList T) {
if (null == T) return;//遞迴基
TreeLinkedList subtree = T.getFirstChild();//從當前節點的長子開始
while (null != subtree) {//依次對當前節點的各個孩子
this.elementsPostorderIterator(subtree);//做後序遍曆
subtree = subtree.getNextSibling();
}
list.insertLast(T);//當所有後代都訪問過後,最後才訪問當前節點
}
//層次遍曆
public void levelTraversalIterator(TreeLinkedList T) {
if (null == T) return;
Queue_List Q = new Queue_List();//空隊
Q.enqueue(T);//根節點入隊
while (!Q.isEmpty()) {//在隊列重新變空之前
TreeLinkedList tree = (TreeLinkedList) (Q.dequeue());//取出隊列首節點
list.insertLast(tree);//將新出隊的節點接入迭代器中
TreeLinkedList subtree = tree.getFirstChild();//從tree的第一個孩子起
while (null != subtree) {//依次找出所有孩子,並
Q.enqueue(subtree);//將其加至隊列中
subtree = subtree.getNextSibling();
}
}
}
//檢查迭代器中是否還有剩餘的元素
public boolean hasNext() { return (null != nextPosition); }
//返回迭代器中的下一元素
public Object getNext() throws ExceptionNoSuchElement {
if (!hasNext()) throw new ExceptionNoSuchElement("No next position");
Position currentPosition = nextPosition;
if (currentPosition == list.last())//若已到達尾元素,則
nextPosition = null;//不再有下一元素
else//否則
nextPosition = list.getNext(currentPosition);//轉向下一元素
return currentPosition.getElem();
}
}