PHP List Usage Example analysis, PHP usage Example analysis
This example describes the use of the PHP list. Share to everyone for your reference. Specific as follows:
This article briefly introduces the basic usage of the list of PHP, including the creation, traversal, updating and other operations of the linked list nodes.
<?php/** * @author mzxy * @copyright * @param PHP List *//**** node class */class node{private $Data;//node data private $Next; /Next node Public function SetData ($value) {$this->data= $value; The Public Function Setnext ($value) {$this->next= $value; } public Function GetData () {return $this->data; } public Function GetNext () {return $this->next; The Public function __construct ($data, $next) {$this->setdata ($data); $this->setnext ($next); }}//function class linklist{private $header;//head node private $size;//Length Public Function GetSize () {$i = 0; $node = $this->header; while ($node->getnext ()!=null) {$i + +; $node = $node->getnext (); } return $i; The Public Function SetHeader ($value) {$this->header= $value; } public Function GetHeader () {return $this->header; } public Function __construct () {header ("content-type:text/html; Charset=utf-8 "); $this->setheader (New Node (Null,null)); }/** * @author mzxy * @param $data--Data to be added to the node * */Public Function Add ($data) {$node = $this->header; while ($node->getnext ()!=null) {$node = $node->getnext (); } $node->setnext (new node ($data, null)); }/** * @author mzxy * @param $data-Data to remove the node * */Public Function removeAt ($data) {$node = $this->header; while ($node->getdata ()! = $data) {$node = $node->getnext (); } $node->setnext ($node->getnext ()); $node->setdata ($node->getnext ()->getdata ()); }/** * @author mzxy * @param traversal * * */Public function get () {$node = $this->header; if ($node->getnext () ==null) {print ("The DataSet is empty!"); Return } while ($node->getnext ()!=null) {print ($node->getnext ()->getdata ()); if ($node->getnext ()->getnext () ==null) {break;} $node = $node->getnext (); }}/** * @author mzxy * @param $data-The data of the node to be accessed * @param This method only demonstrates no practical meaning * */Public Function getAt ($data) {$ Node= $this->HEADER->GETNExt (); if ($node->getnext () ==null) {print ("The DataSet is empty!"); Return } while ($node->getdata ()! = $data) {if ($node->getnext () ==null) {break;} $node = $node->getnext (); } return $node->getdata (); }/** * @author mzxy * @param $value--The original data of the node that needs to be updated--$initial---updated data * */Public Function update ($initial, $value) {$node = $this->header->getnext (); if ($node->getnext () ==null) {print ("The DataSet is empty!"); Return } while ($node->getdata ()! = $data) {if ($node->getnext () ==null) {break;} $node = $node->getnext (); } $node->setdata ($initial); }}?>
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/1029598.html www.bkjia.com true http://www.bkjia.com/PHPjc/1029598.html techarticle PHP List Usage example analysis, PHP Usage Example Analysis of this article describes the PHP list usage. Share to everyone for your reference. As follows: Here is a brief introduction to the basic use of the list of PHP ...