SqArr=$SqArr; $this->top=count($SqArr)-1; } //栈的销毁 public function DestroyStack(){ $this->SqArr=null; $this->top=-1; } //将栈置为空栈 public function ClearStack(){ $this->SqArr=array(); $this->top=-1; } //判断栈是否为空 public function StackEmpty(){ if($this->top==-1){ return 'Null'; }else{ return 'No Null'; } } //返回栈的长度 public function StackLength(){ if(is_null($this->SqArr)){ return null; } return $this->top+1; } //取得栈顶的元素 public function GetTop(){ if($this->top==-1){ return 'ERROR'; } return $this->SqArr[$this->top]; } //插入新的栈顶元素 public function Push($elem){ $this->top++; $this->SqArr[$this->top]=$elem; } //删除栈顶元素 public function Pop(){ if($this->top==-1){ return 'ERROR'; } $p=$this->SqArr[$this->top]; unset($this->SqArr[$this->top]); //注,此句可以有也可以没有,如果此语句存在,则在堆栈内存中会将此栈顶元素真正的删除;倘若没有此语句,则仅仅是top指针不在指向此栈顶元素了。另外,此语句有没有对应的下面的遍历方法的实现也不同,没有则对应StackTraverse()方法,有则对应StackTraverse2()方法。 $this->top--; return $p; } //遍历栈元素 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; } }