PHP creates linked lists and adds, deletes, updates, and loops for linked list nodes

Source: Internet
Author: User
Tags php file upload
This article mainly introduces the creation of a list of PHP and the list of nodes to add, delete, update and traverse, interested in the friend's reference, I hope to be helpful to everyone.

The examples in this article describe the use of the PHP list, 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-&GT;HEADER-&GT;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); }}?>

Summary: the above is the entire content of this article, I hope to be able to help you learn.

Related recommendations:

PHP cache for storage and classification databases

PHP File Upload Management system

PHP implements the import and export of CSV format data

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.