class Node{ public $data=null; public $next = null; public $prev=null; public function __construct($data=null){ $this->data = $data; } } class BubbleList{ public $header; public function __construct($data=null){ $this->header = new Node(); } //插入 public function insert($data){ $current = $this->header; while($current->next){ $current=$current->next; } $Node = new Node($data); $current->next = $Node;//新节点绑定于前节点 $Node->prev=$current;//新节点存储于前节点 } //遍历 public function Link(){ $current=$this->header; while($current){ echo $current->data; echo " "; $current=$current->next; } } //通过数字返回所在位置 public function find($num){ $current = $this->header; $index=1; while($index <= $num){ $current=$current->next; $index++; } return $current; } //修改所在位置数字 public function update($num,$data){ $current = $this->header; $index=1; while($index <= $num){ $index++; $current = $current->next; } $current->data = $data; } //固定位置插入数字 public function addData($num,$data){ $current = $this->header; $index=1; while($index <= $num){ $prev = $current; $index++; $current=$current->next; } $pNode = new Node($data); $pNode->next = $current; $pNode->prev = $prev; $prev->next=$pNode; $current->prev = $pNode; } //通过固定位置删除 public function del($num){ $current = $this->header; $index=1; while($index <= $num){ $prev = $current; $index++; $current = $current->next; } $next = $current->next; $prev->next = $next; $next->prev = $prev; } } $obj = new BubbleList(); $arr=[100,3,90,1,40,5,4,100,8]; foreach ($arr as $key => $value) { $obj->insert($value); } // $obj->addData(4,7); //$obj->del(5); //$obj->find(3); //$obj->update(3,89); $obj->Link();