Skip to content

Commit 7540c6c

Browse files
committed
php 08_stack
1 parent ce72454 commit 7540c6c

File tree

6 files changed

+202
-6
lines changed

6 files changed

+202
-6
lines changed

php/06_linkedlist/SingleLinkedList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public function getPreNode(SingleLinkedListNode $node)
133133
$preNode = $this->head;
134134
// 遍历找到前置节点 要用全等判断是否是同一个对象
135135
// http://php.net/manual/zh/language.oop5.object-comparison.php
136-
while ($curNode !== $node) {
136+
while ($curNode !== $node && $curNode != null) {
137137
$preNode = $curNode;
138138
$curNode = $curNode->next;
139139
}

php/08_stack/.gitkeep

Whitespace-only changes.

php/08_stack/StackOnLinkedList.php

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
/**
3+
* User: lide01
4+
* Date: 2018/10/11 19:37
5+
* Desc:
6+
*/
7+
8+
namespace Algo_08;
9+
10+
11+
use Algo_06\SingleLinkedListNode;
12+
13+
class StackOnLinkedList
14+
{
15+
/**
16+
* 头指针
17+
*
18+
* @var SingleLinkedListNode
19+
*/
20+
public $head;
21+
22+
/**
23+
* 栈长度
24+
*
25+
* @var
26+
*/
27+
public $length;
28+
29+
/**
30+
*
31+
* StackOnLinkedList constructor.
32+
*/
33+
public function __construct()
34+
{
35+
$this->head = new SingleLinkedListNode();
36+
$this->length = 0;
37+
}
38+
39+
/**
40+
* 出栈
41+
*
42+
* @return bool
43+
*/
44+
public function pop()
45+
{
46+
if (0 == $this->length) {
47+
return false;
48+
}
49+
50+
$this->head->next = $this->head->next->next;
51+
$this->length--;
52+
53+
return true;
54+
}
55+
56+
/**
57+
* 入栈
58+
*
59+
* @param $data
60+
*
61+
* @return SingleLinkedListNode|bool
62+
*/
63+
public function push($data)
64+
{
65+
return $this->pushData($data);
66+
}
67+
68+
/**
69+
* 入栈 node
70+
*
71+
* @param SingleLinkedListNode $node
72+
*
73+
* @return bool
74+
*/
75+
public function pushNode(SingleLinkedListNode $node)
76+
{
77+
if (null == $node) {
78+
return false;
79+
}
80+
81+
$node->next = $this->head->next;
82+
$this->head->next = $node;
83+
84+
$this->length++;
85+
return true;
86+
}
87+
88+
/**
89+
* 入栈 data
90+
*
91+
* @param $data
92+
*
93+
* @return SingleLinkedListNode|bool
94+
*/
95+
public function pushData($data)
96+
{
97+
$node = new SingleLinkedListNode($data);
98+
99+
if (!$this->pushNode($node)) {
100+
return false;
101+
}
102+
103+
return $node;
104+
}
105+
106+
/**
107+
* 获取栈顶元素
108+
*
109+
* @return SingleLinkedListNode|bool|null
110+
*/
111+
public function top()
112+
{
113+
if (0 == $this->length) {
114+
return false;
115+
}
116+
117+
return $this->head->next;
118+
}
119+
120+
/**
121+
* 打印栈
122+
*/
123+
public function printSelf()
124+
{
125+
if (0 == $this->length) {
126+
echo 'empty stack' . PHP_EOL;
127+
return;
128+
}
129+
130+
echo 'head.next -> ';
131+
$curNode = $this->head;
132+
while ($curNode->next) {
133+
echo $curNode->next->data . ' -> ';
134+
135+
$curNode = $curNode->next;
136+
}
137+
echo 'NULL' . PHP_EOL;
138+
}
139+
140+
/**
141+
* 获取栈长度
142+
*
143+
* @return int
144+
*/
145+
public function getLength()
146+
{
147+
return $this->length;
148+
}
149+
150+
/**
151+
* 判断栈是否为空
152+
*
153+
* @return bool
154+
*/
155+
public function isEmpty()
156+
{
157+
return $this->length > 0 ? false : true;
158+
}
159+
}

php/08_stack/main.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* User: lide01
4+
* Date: 2018/10/11 20:01
5+
* Desc:
6+
*/
7+
8+
namespace Algo_08;
9+
10+
require_once '../vendor/autoload.php';
11+
12+
$stack = new StackOnLinkedList();
13+
$stack->pushData(1);
14+
$stack->pushData(2);
15+
$stack->pushData(3);
16+
$stack->pushData(4);
17+
var_dump($stack->getLength());
18+
$stack->printSelf();
19+
20+
$topNode = $stack->top();
21+
var_dump($topNode->data);
22+
23+
$stack->pop();
24+
$stack->printSelf();
25+
$stack->pop();
26+
$stack->printSelf();
27+
28+
var_dump($stack->getLength());
29+
30+
$stack->pop();
31+
$stack->pop();
32+
$stack->printSelf();

php/README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
## 数据结构与算法之美PHP实现
22

33
### 项目运行
4-
* 依赖composer自动加载,php目录下执行`composer dump-autoload`
4+
* 依赖composer自动加载,php目录下执行`composer dump-autoload` || `sh buildAutoLoad.sh`
55
* 项目代码均在mac&php7环境下跑通
66

77
### 项目实现
8-
#### 06
8+
#### 06_linkedlist
99
* 单链表php实现
1010
* 回文判断
11-
#### 07
11+
12+
#### 07_linkedlist
1213
* reverse 单链表反转
1314
* checkCircle 链表中环的检测
1415
* mergerSortedList 两个有序的链表合并
1516
* deleteLastKth 删除链表倒数第n个结点
16-
* findMiddleNode 求链表的中间结点
17+
* findMiddleNode 求链表的中间结点
18+
19+
#### 08_stack
20+
* 链栈实现

php/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"autoload": {
77
"psr-4": {
88
"Algo_06\\": "06_linkedlist/",
9-
"Algo_07\\": "07_linkedlist/"
9+
"Algo_07\\": "07_linkedlist/",
10+
"Algo_08\\": "08_stack/"
1011
}
1112
}
1213
}

0 commit comments

Comments
 (0)