Copy CodeThe code is as follows:
Linked list nodes
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;
}
}
Single linked list
Class Singellinklist {
Private $header; Linked list head node
Construction method
Public function __construct ($id = null, $name = null) {
$this->header = new node ($id, $name, NULL);
}
Get the chain table length
Public Function Getlinklength () {
$i = 0;
$current = $this->header;
while ($current->next! = null) {
$i + +;
$current = $current->next;
}
return $i;
}
adding node data
Public Function Addlink ($node) {
$current = $this->header;
while ($current->next! = null) {
if ($current->next->id > $node->id) {
Break
}
$current = $current->next;
}
$node->next = $current->next;
$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) {
$current->next = $current->next->next;
} else {
echo "id= not Found". $id. "The node!
";
}
}
Get linked List
Public Function getlinklist () {
$current = $this->header;
if ($current->next = = null) {
Echo ("The list is empty!");
Return
}
while ($current->next! = null) {
echo ' ID: '. $current->next->id. ' Name: '. $current->next->name. "
";
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 "
-----------Delete a node--------------
";
$lists->dellink (5);
$lists->getlinklist ();
echo "
-----------Update the node name--------------
";
$lists->updatelink (3, "222222");
$lists->getlinklist ();
echo "
-----------Gets the node name--------------
";
Echo $lists->getlinknamebyid (5);
echo "
-----------Get the link table length--------------
";
echo $lists->getlinklength ();
?>
http://www.bkjia.com/PHPjc/326722.html www.bkjia.com true http://www.bkjia.com/PHPjc/326722.html techarticle Copy the code as follows:? PHP//List node class Node {public $id;//Node ID public $name;//node name public $next;//Next node public function __con struct ($id, $name) ...