PHP linked List

Source: Internet
Author: User
Before the blog Park search found no PHP implementation of the list, in Baidu also almost no! So put the code on it!

I think there are some flaws in this code, what is it? is the type of constraint is not strict enough! PHP itself is a weak type of language!

So in this piece is a bit confused! Forget the pointers! I am a novice!

Here is the code

Node class

              

Class node{ private $Data; node Data private $Next; next node public function SetData ($value) {$ This , Data = $value;} Public function Setnext ($value) {$ This and Next = $value;} Public function GetData () { return $ This, Data;} Public function GetNext () { return $ this and Next;} Public function __construct ($data, $next) {$ setData ($data); Setnext ($next);} }

Function class

              

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 add node * */Public Function Add ($data) {$node = $ the 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, set Next ($node-GetNext ()); GetData (SetData, $node, GetNext, $node); }/* * * @author MZXY * @param traversal * */Public function get () {$node = $ this-& Gt 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-& Gt GetNext (); }}//* * @author Mzxy * @param $data-The data of the node to be accessed * @paRam This method simply demonstrates no actual meaning * */Public Function getAt ($data) {$node = $ this--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, and 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); } }

  • Related Article

    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.