<?php
class Node {
public $data;
public $next;
public function __construct($data) {
$this->data = $data;
}
public function __toString() {
$output = ($this->next !== null) ? "$this->next, " : "";
return $output . $this->data;
}
}
class Stack {
private $top;
public function __construct() {
echo $this;
}
public function push($data) {
$node = new Node($data);
$node->next = $this->top;
$this->top = $node;
echo "push $data\n";
echo $this;
}
public function pop() {
if ($this->top === null) {
return null;
}
$data = $this->top->data;
$this->top = $this->top->next;
echo "pop $data\n";
echo $this;
return $data;
}
public function __toString() {
return "Stack: [$this->top]\n";
}
}
$stack = new Stack();
for ($i = 1; $i < 4; $i++) {
$stack->push($i);
}
for ($i = 1; $i < 6; $i++) {
$stack->pop();
}
To embed this project on your website, copy the following code and paste it into your website's HTML: