/* For your reference, I share the source code as a collector! */
/*
* Iterator ADT Interface
*/
Package DSA;
Public interface iterator {
Boolean hasnext (); // check whether there are any remaining elements in the iterator
Object getnext (); // return the next element in the iterator
}
/*
* List-based element iterator
*/
Package DSA;
Public class iteratorelement implements iterator {
Private list; // list
Private position nextposition; // the position of the current (next) Element
// Default constructor
Public iteratorelement () {list = NULL ;}
// Constructor
Public iteratorelement (list l ){
List = L;
If (list. isempty () // if the list is empty
Nextposition = NULL; // The current element is null.
Else // otherwise
Nextposition = List. First (); // starts with the first element.
}
// Check whether there are any remaining elements in the iterator
Public Boolean hasnext () {return (null! = Nextposition );}
// Return the next element in the iterator
Public object getnext () throws exceptionnosuchelement {
If (! Hasnext () throw new exceptionnosuchelement ("unexpected: No next element ");
Position currentposition = nextposition;
If (currentposition = List. Last () // if the last element has been reached
Nextposition = NULL; // no longer has the next element
Else // otherwise
Nextposition = List. getnext (currentposition); // turn to the next element
Return currentposition. getelem ();
}
}
/*
* List-based location iterator
*/
Package DSA;
Public class iteratorposition implements iterator {
Private list; // list
Private position nextposition; // The current (next) Position
// Default constructor
Public iteratorposition () {list = NULL ;}
// Constructor
Public iteratorposition (list l ){
List = L;
If (list. isempty () // if the list is empty
Nextposition = NULL; // The current position is null.
Else // otherwise
Nextposition = List. First (); // starts from the first position
}
// Check whether there are any remaining positions in the iterator
Public Boolean hasnext () {return (nextposition! = NULL );}
// Return the next position in the iterator
Public object getnext () throws exceptionnosuchelement {
If (! Hasnext () throw new exceptionnosuchelement ("unexpected: No next position ");
Position currentposition = nextposition;
If (currentposition = List. Last () // if the last position has been reached
Nextposition = NULL; // no longer has the next position
Else // otherwise
Nextposition = List. getnext (currentposition); // turn to the next position
Return currentposition;
}
}
/**
* List-based tree iterator
*/
Package DSA;
Public class iteratortree implements iterator {
Private list; // list
Private position nextposition; // the position of the current (next) Element
// Default constructor
Public iteratortree () {list = NULL ;}
// Pre-order traversal
Public void elementspreorderiterator (treelinkedlist t ){
If (null = T) return; // recursive basis
List. insertlast (t); // output the current node first
Treelinkedlist subtree = T. getfirstchild (); // starting from the eldest son of the current node
While (null! = Subtree) {// each child of the current node
This. elementspreorderiterator (subtree); // pre-order traversal
Subtree = subtree. getnextsibling ();
}
}
// Post-order traversal
Public void elementspostorderiterator (treelinkedlist t ){
If (null = T) return; // recursive basis
Treelinkedlist subtree = T. getfirstchild (); // starting from the eldest son of the current node
While (null! = Subtree) {// each child of the current node
This. elementspostorderiterator (subtree); // perform post-order traversal.
Subtree = subtree. getnextsibling ();
}
List. insertlast (t); // when all future generations access the current node
}
// Hierarchical Traversal
Public void leveltraversaliterator (treelinkedlist t ){
If (null = T) return;
Queue_list q = new queue_list (); // empty team
Q. enqueue (t); // enter the root node
While (! Q. isempty () {// before the queue is empty again
Treelinkedlist tree = (treelinkedlist) (q. dequeue (); // retrieves the first node of the queue.
List. insertlast (tree); // connect a new node to the iterator.
Treelinkedlist subtree = tree. getfirstchild (); // starts from the first child of the tree.
While (null! = Subtree) {// locate all the children in sequence and
Q. enqueue (subtree); // Add it to the queue
Subtree = subtree. getnextsibling ();
}
}
}
// Check whether there are any remaining elements in the iterator
Public Boolean hasnext () {return (null! = Nextposition );}
// Return the next element in the iterator
Public object getnext () throws exceptionnosuchelement {
If (! Hasnext () throw new exceptionnosuchelement ("no next position ");
Position currentposition = nextposition;
If (currentposition = List. Last () // if the last element has been reached
Nextposition = NULL; // no longer has the next element
Else // otherwise
Nextposition = List. getnext (currentposition); // turn to the next element
Return currentposition. getelem ();
}
}