<?php
/**
* Single linked list
*/
Class Demo
{
Private $id;
Public $name;
Public $next;
Public function __construct ($id = ", $name =")
{
$this->id = $id;
$this->name = $name;
}
static public function Show ($head)
{
$cur = $head;
while ($cur->next) {
echo $cur->next->id, ' # # # ', $cur->next->name, ' <br/> ';
$cur = $cur->next;
}
echo ' }
Tail interpolation method
static public function push ($head, $node)
{
$cur = $head;
while (NULL! = $cur->next) {
$cur = $cur->next;
}
$cur->next = $node;
return $head;
}
static public Function Insert ($head, $node)
{
$cur = $head;
while (NULL! = $cur->next) {
if ($cur->next->id > $node->id) {
Break
}
$cur = $cur->next;
}
$node->next = $cur->next;
$cur->next = $node;
return $head;
}
static public function edit ($head, $node)
{
$cur = $head;
while (NULL! = $cur->next) {
if ($cur->next->id = = $node->id) {
Break
}
$cur = $cur->next;
}
$cur->next->name = $node->name;
return $head;
}
static public Function pop ($head, $node)
{
$cur = $head;
while (NULL! = $cur->next) {
if ($cur->next = = $node) {
Break
}
$cur = $cur->next;
}
$cur->next = $node->next;
return $head;
}
}
$team = new Demo ();
$node 1 = new Demo (1, ' Don Sanzang ');
Demo::p ush ($team, $node 1);
$node 1->name = ' Tang Monk ';
Demo::show ($team);
Demo::show ($team);
$node 2 = new Demo (2, ' Monkey King ');
Demo::insert ($team, $node 2);
Demo::show ($team);
$node 3 = new Demo (5, ' White Dragon Horse ');
Demo::p ush ($team, $node 3);
Demo::show ($team);
$node 4 = new Demo (3, ' pig ');
Demo::insert ($team, $node 4);
Demo::show ($team);
$node 5 = new Demo (4, ' Sand Monk ');
Demo::insert ($team, $node 5);
Demo::show ($team);
$node 4->name = ' pig enlightenment ';//php object is referenced, so demo::edit no need to
Unset ($node 4);
$node 4 = new Demo (3, ' Pig Awareness Energy ');
Demo::edit ($team, $node 4);
Demo::p op ($team, $node 1);
Demo::show ($team);
Code implemented by the PHP single-linked list