首页 > 代码库 > PHP链表的记录

PHP链表的记录

<?php
/**

 * Date: 2017/3/31
 * Time: 21:13
 */
class Node {
    private $Data;
    private $Next;

    public function getNext()
    {
        return $this->Next;
    }
    public function getData()
    {
        return $this->Data;
    }
    public function setData($value)
    {
        $this->Data = http://www.mamicode.com/$value;
    }
    public function setNext($next)
    {
        $this->Next = $next;
    }
public  function __construct($value,$next){
    $this->setData($value);
    $this->setNext($next);
}

}
class listNode{
    private $header;
    public function getHeader()
    {
        return $this->header;
    }
    public function setHeader($header)
    {
        $this->header = $header;
    }
   public function __construct(){
    $this->setHeader(new Node(null,null));
}
    public function  add($data){
        $node = $this->header;
        while($node->getNext()!= null){
            $node = $node->getNext();
        }
        $node->setNext(new Node($data,null));
    }
    public function  get(){
        $node =$this->header;
        if($node->getNext() == null){
                print("空");
        }
        while($node->getNext()!=null){
            print($node->getNext()->getData());
            if($node->getNext()->getNext() == null){
                break;
            }
            $node = $node->getNext();
        }
    }
    public function remove($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 function addAfter($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(new Node($data,$b));
        }

    }

    public function addBefore($data,$a){
        $node = $this->header;
        if($node->getData() == $a){
            new Node($data,$node);
        }
        while($node->getNext()&&$node->getNext()->getData()!=$a){
            $node= $node->getNext();
        }
        if($node->getNext() ==null){
            return ;
        }else{
            $b = $node->getNext();
            $node->setNext(new Node($data,$b));
        }
    }

    public  function updata($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 ;
        }
    }
}
//以下为测试代码
$a= new listNode();
$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();

 

PHP链表的记录