2012-06-25 13:03:14 +00:00
|
|
|
<?php
|
|
|
|
class NodeIterator {
|
2012-07-02 16:55:13 +00:00
|
|
|
/*---------------------------
|
2012-08-20 13:52:35 +00:00
|
|
|
The NodeIterator can be used to iterate linked lists.
|
2012-07-02 16:55:13 +00:00
|
|
|
|
|
|
|
Sample:
|
|
|
|
$iter = new NodeIterator(array());
|
|
|
|
while($iter->hasNext()) {
|
|
|
|
$curr = $iter->getNext();
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
---------------------------*/
|
2012-07-08 16:11:25 +00:00
|
|
|
private $node;
|
2012-06-25 13:03:14 +00:00
|
|
|
|
2012-07-08 16:11:25 +00:00
|
|
|
function NodeIterator($node) {
|
|
|
|
$this->node = $node;
|
2012-06-25 13:03:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function hasNext() {
|
2012-07-08 16:11:25 +00:00
|
|
|
if($this->node == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2012-06-25 13:03:14 +00:00
|
|
|
}
|
|
|
|
|
2012-07-08 16:11:25 +00:00
|
|
|
function getNext() {
|
|
|
|
$n = $this->node;
|
|
|
|
$this->node = $this->node->getChild();
|
|
|
|
return $n->data;
|
2012-06-25 13:03:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|