<?php/** 1. destroystack (): stack destruction 2. clearstack (): stack to empty stack 3. stackempty (): determine if stack is empty 4. Stacklength (): returns the length of the stack 5. gettop (): Gets the element 6 of the top of the stack. push (): insert a new stack top element 7. pop (): Delete the top element of the stack 8. Stacktraverse (): traverses the stack element *///except for the push and pop methods, the rest of the methods are basically the same as the code implementations of the previous linear table's sequential storage class sqstack{ public $SQARR;//Here is an array private $top for storing stack elements; //represents the top of the stack, Corresponds to the last element of the array subscript public function __construct ($SQARR) { $this->sqarr= $SqArr; $this->top= Count ($SQARR) destruction of -1; } //stacks public Function destroystack () {&NBsp; $this->sqarr=null; $this->top=-1; } //the stack to empty stack public function clearstack () { $this->sqarr=array (); $this->top=-1; } //determine if the stack is empty public function stackempty () { if ($this->top==-1) { return ' Null '; }else{ return ' No null '; } } //returns the length of the stack public Function stacklength () { &Nbsp; if (Is_null ($this->sqarr)) { return null; } return $this->top+1; } //gets the top element of the stack public function gettop () { if ($this->top==-1) { return ' ERROR '; } return $this->sqarr[$this->top]; } Inserts a new stack top element public function push ($elem) { $this->top++; $this->sqarr[$this->top]=$ Elem; } //Delete Stack top element public function pop () { if ($this->top==-1) { return ' ERROR '; } $p = $this->sqarr[$this->top]; unset ($this->sqarr[$this->top]); //Note, this sentence can or may not, if this statement exists, The top element of this stack is actually deleted in the stack memory, but if there is no such statement, the top pointer does not point to the top element of the stack. In addition, this statement has no corresponding implementation of the following traversal method is also different, there is no corresponding to the Stacktraverse () method, there is a corresponding StackTraverse2 () method. $this top--; return $p; } //Traversal stack element public function stacktraverse () { if (Is_null ($this->sqarr)) { return null; } $ Arr=array (); for ($i =0; $i <= $this->top; $i +) { $arr []= $this->sqarr[$i]; } return $arr; } public function stacktraverse2 () { return $this->sqarr; }}
This article is from the "Everything Possible" blog, please be sure to keep this source http://noican.blog.51cto.com/4081966/1599657
Stack of data structures-sequential storage structure (PHP code implementation)