Example code for php to implement a single-chain table

Source: Internet
Author: User

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 ();
?>

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.