Skip to content

Instantly share code, notes, and snippets.

@thachp
Created June 21, 2015 12:04
Show Gist options
  • Save thachp/abe0ca0169eec8210ede to your computer and use it in GitHub Desktop.
Save thachp/abe0ca0169eec8210ede to your computer and use it in GitHub Desktop.
Splstack .. push and pop.
<?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