diff --git a/php/06_linkedlist/SingleLinkedList.php b/php/06_linkedlist/SingleLinkedList.php index 5928860..222f638 100644 --- a/php/06_linkedlist/SingleLinkedList.php +++ b/php/06_linkedlist/SingleLinkedList.php @@ -133,7 +133,7 @@ class SingleLinkedList $preNode = $this->head; // 遍历找到前置节点 要用全等判断是否是同一个对象 // http://php.net/manual/zh/language.oop5.object-comparison.php - while ($curNode !== $node) { + while ($curNode !== $node && $curNode != null) { $preNode = $curNode; $curNode = $curNode->next; } diff --git a/php/08_stack/.gitkeep b/php/08_stack/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/php/08_stack/StackOnLinkedList.php b/php/08_stack/StackOnLinkedList.php new file mode 100644 index 0000000..bfac655 --- /dev/null +++ b/php/08_stack/StackOnLinkedList.php @@ -0,0 +1,159 @@ +head = new SingleLinkedListNode(); + $this->length = 0; + } + + /** + * 出栈 + * + * @return bool + */ + public function pop() + { + if (0 == $this->length) { + return false; + } + + $this->head->next = $this->head->next->next; + $this->length--; + + return true; + } + + /** + * 入栈 + * + * @param $data + * + * @return SingleLinkedListNode|bool + */ + public function push($data) + { + return $this->pushData($data); + } + + /** + * 入栈 node + * + * @param SingleLinkedListNode $node + * + * @return bool + */ + public function pushNode(SingleLinkedListNode $node) + { + if (null == $node) { + return false; + } + + $node->next = $this->head->next; + $this->head->next = $node; + + $this->length++; + return true; + } + + /** + * 入栈 data + * + * @param $data + * + * @return SingleLinkedListNode|bool + */ + public function pushData($data) + { + $node = new SingleLinkedListNode($data); + + if (!$this->pushNode($node)) { + return false; + } + + return $node; + } + + /** + * 获取栈顶元素 + * + * @return SingleLinkedListNode|bool|null + */ + public function top() + { + if (0 == $this->length) { + return false; + } + + return $this->head->next; + } + + /** + * 打印栈 + */ + public function printSelf() + { + if (0 == $this->length) { + echo 'empty stack' . PHP_EOL; + return; + } + + echo 'head.next -> '; + $curNode = $this->head; + while ($curNode->next) { + echo $curNode->next->data . ' -> '; + + $curNode = $curNode->next; + } + echo 'NULL' . PHP_EOL; + } + + /** + * 获取栈长度 + * + * @return int + */ + public function getLength() + { + return $this->length; + } + + /** + * 判断栈是否为空 + * + * @return bool + */ + public function isEmpty() + { + return $this->length > 0 ? false : true; + } +} \ No newline at end of file diff --git a/php/08_stack/main.php b/php/08_stack/main.php new file mode 100644 index 0000000..6347821 --- /dev/null +++ b/php/08_stack/main.php @@ -0,0 +1,32 @@ +pushData(1); +$stack->pushData(2); +$stack->pushData(3); +$stack->pushData(4); +var_dump($stack->getLength()); +$stack->printSelf(); + +$topNode = $stack->top(); +var_dump($topNode->data); + +$stack->pop(); +$stack->printSelf(); +$stack->pop(); +$stack->printSelf(); + +var_dump($stack->getLength()); + +$stack->pop(); +$stack->pop(); +$stack->printSelf(); \ No newline at end of file diff --git a/php/README.md b/php/README.md index 3aa9674..6a6fa5d 100644 --- a/php/README.md +++ b/php/README.md @@ -1,16 +1,20 @@ ## 数据结构与算法之美PHP实现 ### 项目运行 -* 依赖composer自动加载,php目录下执行`composer dump-autoload` +* 依赖composer自动加载,php目录下执行`composer dump-autoload` || `sh buildAutoLoad.sh` * 项目代码均在mac&php7环境下跑通 ### 项目实现 -#### 06 +#### 06_linkedlist * 单链表php实现 * 回文判断 -#### 07 + +#### 07_linkedlist * reverse 单链表反转 * checkCircle 链表中环的检测 * mergerSortedList 两个有序的链表合并 * deleteLastKth 删除链表倒数第n个结点 -* findMiddleNode 求链表的中间结点 \ No newline at end of file +* findMiddleNode 求链表的中间结点 + +#### 08_stack +* 链栈实现 \ No newline at end of file diff --git a/php/composer.json b/php/composer.json index ea60240..01f6f02 100644 --- a/php/composer.json +++ b/php/composer.json @@ -6,7 +6,8 @@ "autoload": { "psr-4": { "Algo_06\\": "06_linkedlist/", - "Algo_07\\": "07_linkedlist/" + "Algo_07\\": "07_linkedlist/", + "Algo_08\\": "08_stack/" } } }