WEB开发笔记 www.chhua.com 每日练习 PHP使用数组实现堆栈

堆栈是一种数据结构的实现形式,是广泛用来存取数据的一种容器。
在堆栈中,最后压入的数据(进栈),将会被最先弹出(出栈)。
即在数据存储时采用“先进后出”的数据结构。
PHP中,将数组当做一个栈,主要是使用array_push()和array_pop()两个系统函数来完成。

入栈主要是利用array_push()函数向第一个参数的数组尾部添加一个或多个元素,然后返回新数组的长度,示例如下:

1
2
3
4
5
6
7
 
<span style="color: #000000;font-weight: bold"><?php</span>
<span style="color: #000088">$zhan</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"WEB"</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//声明一个数组当做栈</span>
<a rel="noopener noreferrer nofollow" href="http://www.php.net/array_push" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array_push</span></a><span style="color: #009900">(</span><span style="color: #000088">$zhan</span><span style="color: #339933">,</span><span style="color: #0000ff">"PHP"</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//将字符串压入栈(数组)中</span>
<a rel="noopener noreferrer nofollow" href="http://www.php.net/array_push" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array_push</span></a><span style="color: #009900">(</span><span style="color: #000088">$zhan</span><span style="color: #339933">,</span><span style="color: #0000ff">"WWW.CHHUA.COM"</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//再压入一个元素</span>
<a rel="noopener noreferrer nofollow" href="http://www.php.net/print_r" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">print_r</span></a><span style="color: #009900">(</span><span style="color: #000088">$zhan</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//打印数组内容 </span>
<span style="color: #000000;font-weight: bold">?></span>

出栈主要是利用array_pop()函数将数组的最后一个函数弹出,并将数组的长度减1,示例如下:

1
2
3
4
5
6
 
<span style="color: #000000;font-weight: bold"><?php</span>
<span style="color: #000088">$zhan</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"WEB"</span><span style="color: #339933">,</span><span style="color: #0000ff">"www.chhua.com"</span><span style="color: #339933">,</span><span style="color: #0000ff">"WEB开发笔记"</span><span style="color: #339933">,</span><span style="color: #0000ff">"PHP"</span><span style="color: #339933">,</span><span style="color: #0000ff">"网站建设"</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//声明一个数组当做栈</span>
<a rel="noopener noreferrer nofollow" href="http://www.php.net/array_pop" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array_pop</span></a><span style="color: #009900">(</span><span style="color: #000088">$zhan</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//将字符串出栈(数组)中</span>
<a rel="noopener noreferrer nofollow" href="http://www.php.net/print_r" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">print_r</span></a><span style="color: #009900">(</span><span style="color: #000088">$zhan</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//打印数组内容  Array([0] => WEB[1] => www.chhua.com[2] => WEB开发笔记[3] => PHP)</span>
<span style="color: #000000;font-weight: bold">?></span>