A single linked list, as its name suggests, is a chained data structure that has a table header, and all nodes have their successors except for the last node. The following figure.
First, we write out the class of the linked list node. Each node in a single linked list saves its data field and the back-drive pointer
List node
class Node {public
$id;//Node ID public
$name;//node name public
$next;//Next node public
function __ Construct ($id, $name) {
$this->id = $id;
$this->name = $name;
$this->next = null;
}
}
The list also has two particularly important methods for inserting and deleting. Insert needs to find the insertion position, point the previous element's next pointer to the inserted node, and point the inserted node's next pointer to the latter node, as shown on the left of the following figure. The deletion then points to the next node in the previous node and returns the data content of the deleted element, as shown on the right of the following figure.
Single linked list class Singellinklist {private $header;//Link Header node//Construct method Public Function __construct ($id = NULL, $
name = null) {$this->header = new node ($id, $name, NULL);
//Get the list length public function getlinklength () {$i = 0;
$current = $this->header;
while ($current->next!= null) {$i + +;
$current = $current->next;
return $i;
}//Add node data public function Addlink ($node) {$current = $this->header; while ($current->next!= null) {if ($current->next->id > $node->id) {Brea
K
} $current = $current->next;
} $node->next = $current->next;
$current->next = $node;
//Delete 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) {$current->next = $current->next->next; else {echo "did not find id=". $id. "Node.
<br> ";
}//Determine if the table is an empty public function IsEmpty () {return $this->header = null;
//Purge list Public Function clear () {$this->header = null;
//Get list Public Function getlinklist () {$current = $this->header;
if ($current->next = = null) {echo ("list is empty!");
Return while ($current->next!= null) {echo ' ID: '. $current->next->id. ' Name: '. $current->next->name.
"<br>";
if ($current->next->next = = null) {break; } $current = $current->next;
}//Get node name public function Getlinknamebyid ($id) {$current = $this->header;
if ($current->next = = null) {echo "linked list is empty!";
Return
while ($current->next!= null) {if ($current->id = = $id) {break;
} $current = $current->next;
return $current->name;
}//Update node name public function UpdateLink ($id, $name) {$current = $this->header;
if ($current->next = = null) {echo "linked list is empty!";
Return
while ($current->next!= null) {if ($current->id = = $id) {break;
} $current = $current->next;
return $current->name = $name;
}} $lists = new Singellinklist ();
$lists->addlink (New node (5, ' eeeeee ')); $lists->addlink (new Node (1, ' aaaaaa '));
$lists->addlink (new node (6, ' ffffff '));
$lists->addlink (New node (4, ' dddddd '));
$lists->addlink (New node (3, ' CCCCCC '));
$lists->addlink (New node (2, ' bbbbbb '));
$lists->getlinklist ();
echo "<br>-----------Delete node--------------<br>";
$lists->dellink (5);
$lists->getlinklist ();
echo "<br>-----------Update node name--------------<br>";
$lists->updatelink (3, "222222");
$lists->getlinklist ();
echo "<br>-----------Get node name--------------<br>";
Echo $lists->getlinknamebyid (5);
echo "<br>-----------get the link length--------------<br>"; echo $lists->getlinklength ();