<?PHP/** * DATE:2017/3/31 * time:21:13*/classNode {Private $Data; Private $Next; Public functionGetNext () {return $this-Next; } Public functionGetData () {return $this-Data; } Public functionSetData ($value) { $this->data =$value; } Public functionSetnext ($next) { $this-Next=$next; } Public function__construct ($value,$next){ $this->setdata ($value); $this->setnext ($next);}}classlistnode{Private $header; Public functionGetHeader () {return $this-Header; } Public functionSetHeader ($header) { $this-Header=$header; } Public function__construct () {$this->setheader (NewNode (NULL,NULL));} Public functionAdd$data){ $node=$this-Header; while($node->getnext ()! =NULL){ $node=$node-GetNext (); } $node->setnext (NewNode ($data,NULL)); } Public functionget () {$node=$this-Header; if($node->getnext () = =NULL){ Print("Empty"); } while($node->getnext ()! =NULL){ Print($node->getnext ()getData ()); if($node->getnext ()->getnext () = =NULL){ Break; } $node=$node-GetNext (); } } Public functionRemove$data){ $node=$this-Header; while($node->getnext ()! =NULL&&$node->getnext ()->getdata ()! =$data){ $node=$node-GetNext (); } if($node->getnext ()! =NULL){ $node->setnext ($node->getnext ()GetNext ()); }//unset ($node->getnext ()); } Public functionAddafter ($data,$a){ $node=$this-Header; while($node->getnext () &&$node->getdata ()! =$a){ $node=$node-GetNext (); } if($node->getnext () = =NULL){ return ; }Else{ $b=$node-GetNext (); $node->setnext (NewNode ($data,$b)); } } Public functionAddbefore ($data,$a){ $node=$this-Header; if($node->getdata () = =$a){ NewNode ($data,$node); } while($node->getnext () &&$node->getnext ()->getdata ()! =$a){ $node=$node-GetNext (); } if($node->getnext () = =NULL){ return ; }Else{ $b=$node-GetNext (); $node->setnext (NewNode ($data,$b)); } } Public functionUpdata ($data,$a){ $node=$this-Header; if($node->getdata () = =$a){ $node->setdata ($data); } while($node->getnext () &&$node->getdata ()! =$a){ $node=$node-GetNext (); } if($node->getdata () = =$a){ $node->setdata ($data); }Else{ return ; } }}//The following is the test code$a=NewListNode ();$a->add (1);//$a->add (2);//$a->add (3);//$a->add (4);//$a->remove (2);$a->addbefore (6,3);$a->updata (6,22);$a->get ();
The list of the PHP links is recorded