Stack Handling Library
<?php
function &stack_initialize() {
$new = array();
return $new;
}
function stack_destroy(&$stack) {
unset($stack);
}
function stack_push(&$stack, $value) {
$stack[] = $value;
}
function stack_pop(&$stack) {
return array_pop($stack);
}
function stack_peek(&$stack) {
return $stack[count($stack)-1];
}
function stack_size(&$stack) {
return count($stack);
}
function stack_swap(&$stack) {
$n = count($stack);
if ($n > 1) {
$second = $stack[$n-2];
$stack[$n-2] = $stack[$n-1];
$stack[$n-1] = $second;
}
}
function stack_dup(&$stack) {
$stack[] = $stack[count($stack)-1];
}
$mystack =& stack_initialize();
stack_push($mystack, 73);
stack_push($mystack, 74);
stack_push($mystack, 5);
stack_dup($mystack);
echo '<p>Stack size is: ', stack_size($mystack), '</p>';
echo '<p>Popped off the value: ', stack_pop($mystack), '</p>';
stack_swap($mystack);
echo '<p>Current top element is: ', stack_peek($mystack), '</p>';
stack_destroy($mystack);
?>
Related examples in the same category