Last active
December 4, 2015 06:47
-
-
Save KerryJones/186c7649dcb0d315fe1a to your computer and use it in GitHub Desktop.
PHP Data Structure: Stack
This file contains hidden or 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
| class Stack { | |
| protected $top; | |
| public function push($data) { | |
| $node = new Node($data); | |
| $node->next = $this->top; | |
| $this->top = $node; | |
| } | |
| public function pop() { | |
| $node = $this->top; | |
| $this->top = $this->top->next; | |
| return $node; | |
| } | |
| public function peek() { | |
| return $this->top->data; | |
| } | |
| } | |
| class Node { | |
| public $data; | |
| public $next; | |
| public function __construct($data) { | |
| $this->data = $data; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment