首页 > 代码库 > 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链表的记录
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。