Php linked list usage instance analysis, php usage instance analysis

Source: Internet
Author: User

Php linked list usage instance analysis, php usage instance analysis

This example describes the usage of the php linked list. Share it with you for your reference. The details are as follows:

This section briefly introduces the basic usage of php linked list, including creating, traversing, and updating linked list nodes.

<? Php/*** @ author MzXy * @ copyright 2011 * @ param PHP linked list * // ***** Node class */class Node {private $ Data; // node Data private $ Next; // The Next node public function setData ($ value) {$ this-> Data = $ value;} public function setNext ($ value) {$ this-> Next = $ value;} public function getData () {return $ this-> Data;} public function getNext () {return $ this-> Next ;} public function _ construct ($ data, $ next) {$ this-> setData ($ data); $ this-> SetNext ($ next) ;}/// function class LinkList {private $ header; // header node private $ size; // length public function getSize () {$ I = 0; $ node = $ this-> header; while ($ node-> getNext ()! = Null) {$ I ++; $ node = $ node-> getNext () ;}return $ I;} 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 of the node to be added **/public function add ($ data) {$ node = $ this-> header; while ($ node-> getN Ext ()! = Null) {$ node = $ node-> getNext () ;}$ node-> setNext (new Node ($ data, null ));} /*** @ author MzXy * @ param $ data -- data of the node to be removed **/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 () = null) {break;} $ node = $ node-> getNext ();}} /*** @ author MzXy * @ param $ data -- data of the node to be accessed * @ param this method only demonstrates non-practical significance **/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 -- original data of the node 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 will help you with php programming.

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.