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