<?php
Header ("Content-type:text/html;charset=utf-8");
Linked list nodes
Class Node {
public static $count =-1; Node ID
Public $name; Node name
Public $next; Next node
public $id;
Public function __construct ($name) {
$this->id = self:: $count;
$this->name = $name;
$this->next = null;
Self:: $count + = 1;
}
}
Single linked list
Class Singellinklist {
Private $header;
Private $current;
Public $count;
Construction method
Public function __construct ($data = null) {
$this->header = new node ($data);
$this->current = $this->header;
$this->count = 0;
}
adding node data
Public Function Addlink ($node) {
if ($this->current->next! = null)
$this->current = $this->current->next;
$this->count++;
$node->next = $this->current->next;
$this->current->next = $node;
}
Delete a linked list node
Public Function Dellink ($id) {
$current = $this->header;
$flag = false;
while ($current->next! = null) {
if ($current->next->id = = $id) {
$flag = true;
Break
}
$current = $current->next;
}
if ($flag) {
$this->count--;
$current->next = $current->next->next;
} else {
echo "id= not Found". $id. "The node! <br> ";
}
}
Get linked List
Public Function getlinklist () {
$this->checknull ();
$current = $this->header;
while ($current->next! = null) {
echo ' ID: '. $current->next->id. ' Name: '. $current->next->name. "<br>";
if ($current->next->next = = null) {
Break
}
$current = $current->next;
}
}
Get length
Public Function Getlinklength ()
{
Echo $this->count;
}
Get current node
Public Function GetCurrent ()
{
$this->checknull ();
Echo ' current Node ID: '. $this->current->next->id. ' Name: '. $this->current->next->name. "<br>";
}
Determines whether the empty
Public Function Checknull ()
{
if ($this->header->next = = null) {
echo "Linked list is empty!";
Exit
}
}
Get node Name
Public Function Getlinkbyid ($id) {
$this->checknull ();
$current = $this->header;
while ($current->next! = null) {
if ($current->id = = $id) {
Break
}
$current = $current->next;
}
echo ' Modified ID: '. $current->id. ' Name: '. $current->name. "<br>";
}
Update node Name
Public Function UpdateLink ($id, $name) {
$this->checknull ();
$current = $this->header;
while ($current->next! = null) {
if ($current->id = = $id) {
Break
}
$current = $current->next;
}
return $current->name = $name;
}
}
$list = new Singellinklist ();
$list->addlink (new node (' aaaaaa '));
$list->addlink (new node (' bbbbbb '));
$list->addlink (new node (' CCCCCC '));
$list->addlink (new node (' dddddd '));
echo "All linked list node:<br/>";
$list->getlinklist ();
echo "<br/>";
echo "Current linked list bottom node:<br/>";
$list->getcurrent ();
echo "<br/>";
echo "Modify the:<br/> of the list node ID 0";
$list->updatelink (0, ' 2222222 ');
echo "Find node:<br/> with id 0";
$list->getlinkbyid (0);
echo "<br/>";
echo "Delete linked list Node ID is 0:<br/>";
$list->dellink (0);
echo "All linked list node:<br/>";
$list->getlinklist ();
echo "<br/>";
echo "All linked list node:<br/>";
$list->getlinklength ();
?>