PHP版链表的实现
本文首發于我的博客
引用:Java數據結構與算法——鏈表
引用:Java數據結構與算法——鏈表
該文章提供的JAVA版的鏈表的實現。
現在我也貼一下PHP版的鏈表的實現:
class Node {private $data;private $next;public function getData() {return $this->data;}public function setData($data) {$this->data = $data;return true;}public function getNext() {return $this->next;}public function setNext($next) {$this->next = $next;return true;} }/*** 鏈表類*/ class Link {private $size = 0;private $first;private $last;/*** 獲取鏈表長度*/public function getLength() {return $this->size;}/*** 鏈表中插入第一個元素的時候,頭和尾部都是同一個元素*/public function oneNode(string $element) {$this->first = new Node;$this->first->setData($element);$this->last = $this->first;}/*** 當只有一個元素的時候,刪除$fist和$last*/public function clear() {$this->first = $this->last = null;}/*** 頭節點插入*/public function addHead(string $element) {if ($this->size == 0) {$this->oneNode($element);} else {$node = new Node;$node->setData($element);$node->setNext($this->first);$this->first = $node;}$this->size++;return true;}/*** 尾節點插入*/public function addTail(string $element) {if ($this->size == 0) {$this->oneNode($element);} else {$node = new Node();$node->setData($element);$this->last->setNext($node);$this->last = $node;}$this->size++;}/*** 中間節點插入*/public function add(int $index, string $element) {if ($index <= $this->size) {if ($this->size == 0) {oneNode($element);} elseif ($index == 0) {$this->addHead($element);} elseif ($index == $this->size) {$this->addTail($element);} else {$tmp = $this->get($index - 1);$node = new Node;$node->setData($element);$node->setNext($tmp->getNext());$tmp->setNext($node);}$this->size++;} else {throw new \Exception("插入位置無效或超出鏈表長度");}}/*** 獲取指定位置的元素*/public function get(int $index) {$tmp = $this->first;for ($i = 0; $i < $index - 1; $i++) {$tmp = $tmp->getNext();}return $tmp;}/*** 刪除頭節點*/public function deleteFirst() {if ($this->size == 0) {throw new \Exception("空鏈表,無元素可刪除");} elseif ($this->size == 1) {$this->clear();} else {$tmp = $this->first;$this->first = $tmp->getNext();$this->size--;}}/*** 刪除尾節點*/public function deleteLast() {if ($this->size == 0) {throw new \Exception("空鏈表,無元素可刪除");} elseif ($this->size == 1) {$this->clear();} else {$tmp = $this->get($this->size - 1);$tmp->setNext(null);$this->size--;}}/*** 刪除指定節點*/public function deleteIndex(int $index) {if ($this->size == 0) {throw new \Exception("空鏈表,無元素可刪除");} elseif ($this->size == 1) {$this->clear();} else {$tmp = $this->get($index - 1);$tmp->setNext($tmp->getNext()->getNext());$this->size--;}}/*** 打印現有的所有元素*/public function printLink() {$tmp = $this->first;if(is_null($tmp)) {return false;} echo $tmp->getData();while(!is_null($tmp->getNext())) {$tmp = $tmp->getNext();echo "->" . $tmp->getData();}echo "\n";} }$link = new Link(); $link->addHead("1"); $link->printLink(); // 1$link->addHead("5"); $link->printLink(); // 5->1$link->addTail("9"); $link->printLink(); // 5->1->9$link->addTail("7"); $link->printLink(); // 5->1->9->7$link->add(3, "8"); $link->printLink(); // 5->1->9->8->7print_r("鏈表長度:" . $link->getLength() . "\n");$link->deleteFirst(); $link->printLink();$link->deleteLast(); $link->printLink();$link->deleteIndex(1); $link->printLink();print_r("鏈表長度:" . $link->getLength() . "\n");總結
- 上一篇: cinder配置多ceph储存池[Cep
- 下一篇: Teambition CEO齐俊元:大象