<?php
class Node
{
public $data;
public ?Node $next = null;
}
class LinkedList
{
private ?Node $head = null;
function addLeft($data)
{
$node = new Node();
$node->data = $data;
$node->next = $this->head;
$this->head = $node;
}
function removeLeft()
{
if (!$this->head)
{
echo "Linked list empty\n";
return;
}
$data = $this->head->data;
$this->head = $this->head->next;
echo $data . " removed\n";
}
function reverse()
{
$prev = NULL;
$next = NULL;
$curr = $this->head;
while ($curr)
{
$next = $curr->next;
$curr->next = $prev;
$prev = $curr;
$curr = $next;
}
$this->head = $prev;
}
}
function main()
{
$list = new LinkedList();
$list->addLeft(30);
$list->addLeft(20);
$list->addLeft(10);
print_r($list);
$list->reverse();
print_r($list);
$list->removeLeft();
$list->removeLeft();
$list->removeLeft();
$list->removeLeft();
print_r($list);
}
main();
To embed this project on your website, copy the following code and paste it into your website's HTML: