Copy codeThe Code is as follows: <? Php
// Linked 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;
}
}
// Single-chain table
Class singelLinkList {
Private $ header; // linked list header Node
// Constructor
Public function _ construct ($ id = null, $ name = null ){
$ This-> header = new node ($ id, $ name, null );
}
// Obtain the length of the linked list
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 ){
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;
} Else {
Echo "the node id =". $ id. "is not found! <Br> ";
}
}
// Obtain the linked list
Public function getLinkList (){
$ Current = $ this-> header;
If ($ current-> next = null ){
Echo ("the linked list is empty! ");
Return;
}
While ($ current-> next! = Null ){
Echo 'id: '. $ current-> next-> id. 'name:'. $ current-> next-> name. "<br> ";
If ($ current-> next = null ){
Break;
}
$ Current = $ current-> next;
}
}
// Obtain the node name
Public function getLinkNameById ($ id ){
$ Current = $ this-> header;
If ($ current-> next = null ){
Echo "The linked list is empty! ";
Return;
}
While ($ current-> next! = Null ){
If ($ current-> id ==$ id ){
Break;
}
$ Current = $ current-> next;
}
Return $ current-> name;
}
// Update the node name
Public function updateLink ($ id, $ name ){
$ Current = $ this-> header;
If ($ current-> next = null ){
Echo "The 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, 'eeeeeee '));
$ Lists-> addLink (new node (1, 'aaaaa '));
$ Lists-> addLink (new node (6, 'ffffff '));
$ Lists-> addLink (new node (4, 'ddddddd '));
$ Lists-> addLink (new node (3, 'cccccccc '));
$ Lists-> addLink (new node (2, 'bbbbbbbb '));
$ Lists-> getLinkList ();
Echo "<br> ----------- delete a node ------------ <br> ";
$ Lists-> delLink (5 );
$ Lists-> getLinkList ();
Echo "<br> ----------- update node name -------------- <br> ";
$ Lists-> updateLink (3, "222222 ");
$ Lists-> getLinkList ();
Echo "<br> ----------- obtain the node name ------------ <br> ";
Echo $ lists-> getLinkNameById (5 );
Echo "<br> ----------- obtain the length of the linked list ------------ <br> ";
Echo $ lists-> getLinkLength ();
?>