Skip to content

Instantly share code, notes, and snippets.

@carousel
Created December 12, 2017 23:24
Show Gist options
  • Save carousel/032b7bce365718637b4b27329f048bf1 to your computer and use it in GitHub Desktop.
Save carousel/032b7bce365718637b4b27329f048bf1 to your computer and use it in GitHub Desktop.
Stack abstract data structure
<?php
function push($item = null)
{
$stack = [];
if ($item) {
$stack[] = $item;
}
return $stack;
}
function pop($stack)
{
if (count($stack) > 0) {
$size = count($stack);
unset($stack[$size - 1]);
}
return $stack;
}
$stack[] = push('a');
$stack[] = push('b');
$stack[] = push('c');
$stack[] = push('d');
$stack = pop($stack);
$stack = pop($stack);
$stack = pop($stack);
var_dump($stack);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment