Php implements the instance code for a single-chain table. Copy the code as follows :? Php linked list node classnode {public $ id; node idpublic $ name; node name public $ next; next node publicfunction _ construct ($ id, $ name)
The code is as follows:
// 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!
";
}
}
// 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 ."
";
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"
----------- Delete a node --------------
";
$ Lists-> delLink (5 );
$ Lists-> getLinkList ();
Echo"
----------- Update node name --------------
";
$ Lists-> updateLink (3, "222222 ");
$ Lists-> getLinkList ();
Echo"
----------- Get node name --------------
";
Echo $ lists-> getLinkNameById (5 );
Echo"
----------- Get the length of the linked list --------------
";
Echo $ lists-> getLinkLength ();
?>
The http://www.bkjia.com/PHPjc/326722.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/326722.htmlTechArticle code is as follows :? Php // linked list node class node {public $ id; // node id public $ name; // node name public $ next; // public function _ construct ($ id, $ name) of the next node )...