<?php/**1.DestroyStack():栈的销毁2.ClearStack():将栈置为空栈3.StackEmpty():判断栈是否为空4.StackLength():返回栈的长度5.GetTop():取得栈顶的元素6.Push():插入新的栈顶元素7.Pop():删除栈顶元素8.StackTraverse():遍历栈元素*///除了push和pop方法外,其余的方法和之前线性表的顺序存储的代码实现基本相同classSqStack{public$SqArr;//此处是用于存储栈元素的数组private$top;//表示栈顶,相当于数组的最后一个元素下标publicfunction__construct($SqArr){$this->SqArr=$SqArr;$this->top=count($SqArr)-1;}//栈的销毁publicfunctionDestroyStack(){$this->SqArr=null;$this->top=-1;}//将栈置为空栈publicfunctionClearStack(){$this->SqArr=array();$this->top=-1;}//判断栈是否为空publicfunctionStackEmpty(){if($this->top==-1){return'Null';}else{return'NoNull';}}//返回栈的长度publicfunctionStackLength(){if(is_null($this->SqArr)){returnnull;}return$this->top+1;}//取得栈顶的元素publicfunctionGetTop(){if($this->top==-1){return'ERROR';}return$this->SqArr[$this->top];}//插入新的栈顶元素publicfunctionPush($elem){$this->top++;$this->SqArr[$this->top]=$elem;}//删除栈顶元素publicfunctionPop(){if($this->top==-1){return'ERROR';}$p=$this->SqArr[$this->top];unset($this->SqArr[$this->top]);//注,此句可以有也可以没有,如果此语句存在,则在堆栈内存中会将此栈顶元素真正的删除;倘若没有此语句,则仅仅是top指针不在指向此栈顶元素了。另外,此语句有没有对应的下面的遍历方法的实现也不同,没有则对应StackTraverse()方法,有则对应StackTraverse2()方法。$this->top--;return$p;}//遍历栈元素publicfunctionStackTraverse(){if(is_null($this->SqArr)){returnnull;}$arr=array();for($i=0;$i<=$this->top;$i++){$arr[]=$this->SqArr[$i];}return$arr;}publicfunctionStackTraverse2(){return$this->SqArr;}}