Created
June 21, 2015 12:04
-
-
Save thachp/abe0ca0169eec8210ede to your computer and use it in GitHub Desktop.
Splstack .. push and pop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* SPLStack provide functionalities of a stack implemented using a doubly linked list. | |
* Purpose of this is to understand how to implement stack in PHP. | |
* @author thachp | |
*/ | |
$stack = new SplStack(); | |
// push item to stack | |
$stack->push('1'); | |
$stack->push('2'); | |
$stack->push('3'); | |
// rewind to begining | |
$stack->rewind(); | |
// echo all items expect 3,2,1 | |
while($stack->valid() ) | |
{ | |
echo $stack->current() . "\n"; | |
$stack->next(); | |
} | |
// pop out an item. | |
$stack->rewind(); | |
$stack->pop(); | |
// expect 2, 1 because 3 has been pop out | |
while($stack->valid() ) | |
{ | |
echo $stack->current() . "\n"; | |
$stack->next(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment