Skip to content

Instantly share code, notes, and snippets.

@NCatalani
Last active June 29, 2021 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NCatalani/5e319beb3b46f937b3d728eaee3d0614 to your computer and use it in GitHub Desktop.
Save NCatalani/5e319beb3b46f937b3d728eaee3d0614 to your computer and use it in GitHub Desktop.
Simple stack abstraction made in PHP
<?php
namespace Stack;
class Stack {
private $stack;
function __construct() {
$this->initStack();
}
private function initStack() {
$this->stack = Array();
}
public function getTop() {
if ($this->isEmpty) return NULL;
$size = $this->getSize;
return $this->stack[$size-1];
}
public function push($element) {
$this->stack[] = $element;
}
public function pop() {
$element = array_pop($this->stack);
return $element;
}
public function getSize() {
$size = count($this->stack);
return $size;
}
public function isEmpty() {
if (empty($this->stack)) return TRUE;
return FALSE;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment