forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request wangzheng0822#51 from Hkesd/master
php 08_stack
- Loading branch information
Showing
6 changed files
with
202 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
* 链栈实现 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters