php 08_stack

This commit is contained in:
Hkesd 2018-10-11 20:11:09 +08:00
parent ce72454dcb
commit 7540c6c23f
6 changed files with 202 additions and 6 deletions

View File

@ -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;
}

0
php/08_stack/.gitkeep Normal file
View File

View File

@ -0,0 +1,159 @@
<?php
/**
* User: lide01
* Date: 2018/10/11 19:37
* Desc:
*/
namespace Algo_08;
use Algo_06\SingleLinkedListNode;
class StackOnLinkedList
{
/**
* 头指针
*
* @var SingleLinkedListNode
*/
public $head;
/**
* 栈长度
*
* @var
*/
public $length;
/**
*
* StackOnLinkedList constructor.
*/
public function __construct()
{
$this->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;
}
}

32
php/08_stack/main.php Normal file
View File

@ -0,0 +1,32 @@
<?php
/**
* User: lide01
* Date: 2018/10/11 20:01
* Desc:
*/
namespace Algo_08;
require_once '../vendor/autoload.php';
$stack = new StackOnLinkedList();
$stack->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();

View File

@ -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 求链表的中间结点
* findMiddleNode 求链表的中间结点
#### 08_stack
* 链栈实现

View File

@ -6,7 +6,8 @@
"autoload": {
"psr-4": {
"Algo_06\\": "06_linkedlist/",
"Algo_07\\": "07_linkedlist/"
"Algo_07\\": "07_linkedlist/",
"Algo_08\\": "08_stack/"
}
}
}