PHP single-chain table

Source: Internet
Author: User
PHP single-chain table Header ("Content-type: text/html; charset = utf-8 ");
// Linked list node
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-chain table
Class singelLinkList {
Private $ header;
Private $ current;
Public $ count;

// Constructor
Public function _ construct ($ data = null ){
$ This-> header = new node ($ data );
$ This-> current = $ this-> header;
$ This-> count = 0;
}

// Add 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;
} Else {
Echo "the node id =". $ id. "is not found!
";
}
}

// Obtain the linked list
Public function getLinkList (){
$ This-> checkNull ();
$ Current = $ this-> header;
While ($ current-> next! = Null ){
Echo 'Id: '. $ current-> next-> id. 'name:'. $ current-> next-> name ."
";
If ($ current-> next = null ){
Break;
}
$ Current = $ current-> next;
}
}

// Obtain the length
Public function getLinkLength ()
{
Echo $ this-> count;
}

// Obtain the current node
Public function getCurrent ()
{
$ This-> checkNull ();
Echo 'current node id: '. $ this-> current-> next-> id. 'name:'. $ this-> current-> next-> name ."
";
}

// Judge whether it is null
Public function checkNull ()
{
If ($ this-> header-> next = null ){
Echo "the linked list is empty! ";
Exit;
}
}

// Obtain the 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 ."
";
}

// Update the 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 ('aaaaa '));
$ List-> addLink (new node ('bbbbbbbb '));
$ List-> addLink (new node ('cccccccc '));
$ List-> addLink (new node ('ddddddd '));
Echo "all linked list nodes:
";
$ List-> getLinkList ();
Echo"
";
Echo "last node of the current linked list:
";
$ List-> getCurrent ();
Echo"
";
Echo "to modify the linked list node id to 0:
";
$ List-> updateLink (0, '123 ');
Echo "find nodes with id 0:
";
$ List-> getLinkById (0 );
Echo"
";
Echo "deleting a linked list node id is 0:
";
$ List-> delLink (0 );
Echo "all linked list nodes:
";
$ List-> getLinkList ();
Echo"
";
Echo "all linked list nodes:
";
$ List-> getLinkLength ();
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.