php实现栈结构--队列-后进先出
<?php
class HeapStack{
private $stackArr = array();
private $stackMaxTop = 10; // 栈顶最大值(用于控制栈长度,是否栈满)
private $top = -1; // 栈顶(会随着push或pop的操作而变化)
private $out;
/**
* 入栈
*/
public function pushValue($value='')
{
if(empty($value)) return '压入的值不能为空';
if($this->top == $this->stackMaxTop) return '栈内已满';
array_push($this->stackArr, $value);
++$this->top;
return '入栈成功,栈顶值:'.$this->top;
}
/**
* 出栈
*/
public function popValue()
{
if($this->top == -1) return '栈内没有数据';
$this->out = array_pop($this->stackArr);
--$this->top;
return '出栈成功,当前栈顶值:'.$this->top.'出栈值:'.$this->out;
}
/**
* 获取栈内信息
*/
public function getSatck()
{
return $this->stackArr;
}
public function __destruct()
{
echo 'over ';
}
}
$stack = new HeapStack();
echo $stack->pushValue('stackValue')."\n";
echo $stack->pushValue('stackValue2')."\n";
var_dump($stack->getSatck());
echo $stack->popValue()."\n";
var_dump($stack->getSatck());
上一篇: laravel清理缓存方法