<?php
declare(strict_types=1);
class Node {
function __construct(
public $data,
public ?Node $next = null,
) {}
}
class LinkedList {
protected $head;
}
interface ISinglyLinkedList {
function push($data);
}
class SinglyLinkedList extends LinkedList implements ISinglyLinkedList {
function push($data) {
$node = new Node($data);
$node->next = $this->head;
$this->head = $node;
}
}
function main() {
$list = new SinglyLinkedList();
for ($i=5; $i>0; $i--) { $list->push($i); }
print_r($list);
}; main();
To embed this project on your website, copy the following code and paste it into your website's HTML: