单项链表
Posted zh718594493
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单项链表相关的知识,希望对你有一定的参考价值。
class Node{ public $data=null; public $next=null; public function __construct($data=null){ $this->data = $data; } } class LinkNode{ public $header=null; public function __construct(){ $this->header = new Node(); } //添加 public function insert($data){ $current = $this->header; while($current->next){ $current=$current->next; } $current->next = new Node($data); } //遍历 public function listLink(){ $current = $this->header; while($current){ echo $current->data; echo " "; $current=$current->next; } } //通过数字返回节点所在位置 public function find($data){ $current = $this->header->next; $index=0; while($current){ if($current->data == $data){ break; } $current = $current->next; $index++; } return $index; } //修改 public function update($num,$data){ $index=1; $current = $this->header; while($index <= $num){ $prev=$current; $current=$current->next; $index++; } $current->data=$data; } //固定位置插入数字 public function addData($num,$data){ $current = $this->header->next; $index = 1; $node = new Node($data); while($index < $num){ $prev = $current; $current=$current->next; $index++; } $prev->next=$node; $node->next=$current; } //清除数据 public function delData($num){ } } $arr=[10,304,4,5,1,30]; $obj = new LinkNode(); foreach ($arr as $key => $value) { $obj->insert($value); } //$obj->addData(4,100); //$obj->update(2,9); //$obj->listLink();
以上是关于单项链表的主要内容,如果未能解决你的问题,请参考以下文章