Stack is a special post-first-out linear table that can only be inserted at one end) and delete (delete is generally called stack play, stack back or stack out) operations. One end that allows the insert and delete operations is called the stack top, and the other end is called the stack bottom. STACK: stores data according to the principle of "back-in-first-out". The data that comes first is pushed to the bottom of the stack, and the data that comes later is on the top of the stack. When you need to read data, data pops up from the top of the stack. When there are no elements in the stack, it is called an empty stack.
Data Structure and algorithm (PHP implementation)-Stack)
| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
<?php/** * Data structures and algorithms (implemented in PHP)-stacks ). *
* @ Author creative programming (topphp. org) * @ Copyright (c) 2013 creative programming (topphp. org) All Rights Reserved * @license http://www.opensource.org/licenses/mit-license.php MIT LICENSE * @version 1.0.0 - Build20130607 */class
Stack { /** * Stack. *
* @var array */ private
$stack; /** * Stack length. *
* @var integer */ private
$size; /** * Constructor-Initialize data. */ public
function __construct() { $this->stack =
array(); $this->size = 0; } /** * Stack pushing (stack loading and stack loading) operations. *
* @ Param mixed $ data pressure stack data. * @ Return object returns the object itself. */ public
function push($data) { $this->stack[$this->size++] =
$data; return
$this; } /** * Stack rollback and stack exit operations. *
* @ Return mixed: If the stack is empty, false is returned. Otherwise, the top element of the stack is returned. */ public
function pop() { if
(!$this->isEmpty()) { $top
= array_splice($this->stack, --$this->size,
1); return
$top[0]; } return
FALSE; } /** * Obtain the stack. *
* @ Return array returns the stack. */ public
function getStack() { return
$this->stack; } /** * Obtain the top element of the stack. *
* @ Return mixed: If the stack is empty, false is returned. Otherwise, the top element of the stack is returned. */ public
function getTop() { if
(!$this->isEmpty()) { return
$this->stack[$this->size - 1]; } return
FALSE; } /** * Obtain the stack length. *
* @ Return integer returns the length of the stack. */ public
function getSize() { return
$this->size; } /** * Check whether the stack is empty. *
* @ Return Boolean if the stack is empty, true is returned; otherwise, false is returned. */ public
function isEmpty() { return
0 === $this->size; }}?> |
Sample Code
| 12345678 |
<?php$stack
= new Stack();$stack->push(1)->push(2)->push(3)->push(4)->push(5)->push(6);echo
'<pre>', print_r($stack->getStack(), TRUE),
'</pre>'; $stack->pop();echo
'<pre>', print_r($stack->getStack(), TRUE),
'</pre>';?> |
Note: PhP array functions have functions similar to Stacks: array_push (pressure stack) and array_pop (bullet stack ).