Last active
June 29, 2021 20:35
-
-
Save NCatalani/5e319beb3b46f937b3d728eaee3d0614 to your computer and use it in GitHub Desktop.
Simple stack abstraction made in PHP
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 | |
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