php链表

<?php
class Node{
  private $data;
  private $next;
  public function getData(){
	   return $this->data;		
  }
  public function setData($data){
	   $this->data = $data;		
  }
  public function getNext(){
	   return $this->next;		
  }
  public function setNext($next){
	    $this->next = $next;		
  }
  public function __construct($data,$next){
	 $this->setData($data);
	 $this->setNext($next);	
  }
}
class LinkList
{
    private $header;
    private $size;
    public function __construct(){
        header("content-type:text/html; charset=utf-8");
        $this->setHeader(new Node(null,null));
    }
    public function setHeader( $value){
        $this->header = $value ;
    }
    public function getHeader(){
        return $this->header;
    }
    public function add($data){
        $node = $this->header ;  
        while($node->getNext() != null){
              $node = $node->getNext();
        } 
        $node->setNext(new Node($data,null));

    }
     public function getSize(){
        $i = 0;
        $node = $this->header;
        while($node->getNext() != null){
            $i++;  
            $node = $node->getNext();
              
        }
        return $i;
     }
     public function removeAt($data)
    {
        $node=$this->header;
        while($node->getData()!=$data)
        {
            $node=$node->getNext();
        }//print_r($node);exit;
       // 
        $data_ = $node->getNext()!=null ? $node->getNext()->getData() : null;
        $node->setData( $data_ );
        $data__ = $node->getNext()!=null ? $node->getNext()->getNext() : null;
        $node->setNext($data__);
    }
    public function getAt($data)
    {
        $node=$this->header->getNext();
          if($node->getNext()==null){
            print("数据集为空!");
            return;
        }
        while($node->getData()!=$data)
        {
            if($node->getNext()==null){break;}
            $node=$node->getNext();
        }
        return $node->getData();        
    }
    public function get()
    {
        $node=$this->header;
        if($node->getNext()==null){
            print("数据集为空!");
            return;
        }
        echo '<hr/>';
        while($node->getNext()!=null)
        {
            print($node->getNext()->getData());echo '   ';
            if($node->getNext()->getNext()==null){break;}
            $node=$node->getNext();
            
        }
        echo '<hr/>';
    }
    public function update($initial,$data)
    {
           $node=$this->header;
          while($node->getData()!=$initial)
          {
            if($node->getNext()==null){break;}
            $node=$node->getNext();
          }
          $node->setData($data);
    }
}

$lk =  new LinkList();
$lk->add(11);

$lk->add(22);$lk->get();

$lk->removeAt(11);
//$lk->update(11,33);
//$lk->update(22,33);
//$lk->removeAt(11);$lk->removeAt(22);
$lk->get();
//echo $lk->getSize();


http://www.oschina.net/code/snippet_129890_4731

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值