Java list-based element iterator, location iterator, and tree iterator (algorithm source code)

Source: Internet
Author: User

/* 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 ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.