Skip to content

Instantly share code, notes, and snippets.

@KerryJones
Last active December 4, 2015 06:47
Show Gist options
  • Save KerryJones/186c7649dcb0d315fe1a to your computer and use it in GitHub Desktop.
Save KerryJones/186c7649dcb0d315fe1a to your computer and use it in GitHub Desktop.
PHP Data Structure: Stack
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