Stack satisfies the principle of last in, first out (LIFO);
The following uses PHP to implement the relevant operation of the stack;
Basic operation of this instance stack:
On-Stack (push): pushes an element into the stack, with the top pointer pointing to the top element of the stack
Stack (POP): Remove elements from the top of the stack, and the top pointer moves to the bottom of the stack;
Empty (IsEmpty): Empty stack when the stack top pointer reverts to the initial pointer state;
Full (Isfull): When the top of the stack equals capacity minus a stack full;
Using a PHP array to implement a simple Operation Stack;
<?PHP/** * Created by Phpstorm. * User:gordon * DATE:2017/8/6 * time:12:58*//** * Class stack simple stack*/classstack{/** * Stack initial pointer*/ ConstTop_init =-1; /** * Stack bottom pointer*/ ConstBOTTOM = 0; /** * @var array stack*/ Private $_stack; /** * @var int stack top pointer*/ Private $_stacktop; /** * @var the capacity of the int stack*/ Private $_stacksize; /** Stack constructor. Initialize stack * @param the size of the int $size stack;*/ Public function__construct ($size) { $this->_stack = []; $this->_stacksize =$size; /** * Stack top initial state on the bottom of the stack*/ $this->_stacktop = self::Top_init; } /** * @method destroy Stack (not required)*/ Public function__destruct () {unset($this-_stack); } /** * @method into the stack * @param mixed $elem into the stack element * @return bool*/ Public functionPush$elem) { if($this-Isfull ()) { return false; } $this->_stack[++$this->_stacktop] =$elem; return true; } /** * @method out stack * @return Mixed|null returns the element at the top of the stack or returns NULL when the stack is empty*/ Public functionpop () {if($this-IsEmpty ()) { return NULL; } $result=$this->_stack[$this-_stacktop]; unset($this->_stack[$this-_stacktop]); $this->_stacktop--; return $result; } /** * @method determine if the stack is empty; * @return BOOL*/ Public functionIsEmpty () {return $this->_stacktop = = = Self::Top_init; } /** * @method determine if the stack is up to the maximum; * @return bool*/ Public functionIsfull () {return $this->_stacksize-1 = = =$this-_stacktop; } /** * @method empty stack*/ Public functionClearstack () {$this->_stack = []; $this->_stacktop = self::Top_init; } /** * @method traversal stack (for testing purposes);*/ Public functionOutputstack () {Echo' Take the traversal stack from bottom to top '.Php_eol; for($i= Self::bottom;$i<$this->_stacktop + 1;$i++) { Echo $this->_stack[$i].Php_eol; } Echo Php_eol; Echo Php_eol; Echo' Take the traversal stack from top to bottom '.Php_eol; for($i=$this->_stacktop;$i>= Self::bottom;$i--) { Echo $this->_stack[$i].Php_eol; } }}
Test the Stack Operation class
$s=NewStack (4);$s->push (1);$s->push (2);$s->push (3);$s->push (4);$s->push (5);$s->outputstack ();//stack from top to Bottom:4 3 2 1$s->pop ();//output:4 stack:3 2 1$s->pop ();//output:3 stack:2 1$s->outputstack ();//Stack:2 1$s->push (5);$s->outputstack ();//stack:5 2 1$s-Clearstack ();Var_dump($s->pop ());//null;
After the test code is very reasonable;
If there is no, please advise
Data structure essay-php implementation stack