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 );}}