Php linked list

Source: Internet
Author: User
Before searching for the php linked list in the blog garden, I found that the linked list is not implemented by php, and there is almost no link in Baidu! So paste the code here!

I think there are some defects in this code. what is it? That is, the type constraints are not strict enough! Php itself is a weak type of language!

So I am confused about this part! Forget to give advice! I am a newbie!

Below is the code

Node

              

Class Node {private $ Data; // Node Data private $ Next; // public function setData ($ value) for the Next Node {$ 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-> getNext ()! = 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 );}}

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.