Skip to content

Commit

Permalink
Merge pull request wangzheng0822#51 from Hkesd/master
Browse files Browse the repository at this point in the history
php 08_stack
  • Loading branch information
wangzheng0822 authored Oct 12, 2018
2 parents d1ed176 + 7540c6c commit f368951
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 6 deletions.
2 changes: 1 addition & 1 deletion php/06_linkedlist/SingleLinkedList.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function getPreNode(SingleLinkedListNode $node)
$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;
}
Expand Down
Empty file added php/08_stack/.gitkeep
Empty file.
159 changes: 159 additions & 0 deletions php/08_stack/StackOnLinkedList.php
Original file line number Diff line number Diff line change
@@ -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 changes: 32 additions & 0 deletions php/08_stack/main.php
Original file line number Diff line number Diff line change
@@ -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();
12 changes: 8 additions & 4 deletions php/README.md
Original file line number Diff line number Diff line change
@@ -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
* 链栈实现
3 changes: 2 additions & 1 deletion php/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"autoload": {
"psr-4": {
"Algo_06\\": "06_linkedlist/",
"Algo_07\\": "07_linkedlist/"
"Algo_07\\": "07_linkedlist/",
"Algo_08\\": "08_stack/"
}
}
}

0 comments on commit f368951

Please sign in to comment.