特点: 后入先出。
php实现
class Stack{
protected $stack = []; //栈
protected $size; //长度
public function __construct($size)
{
$this->size = $size;
}
//入栈
public function push($value){
//栈的实际长度比传入长度大 栈已经满
if(count($this->stack)>=$this->size) return false;
array_unshift($this->stack,$value);//插入元素 为第一个元素
}
//出栈
public function pop(){
if(count($this->stack)=='0') return false;
array_shift($this->stack); //删除第一个元素
}
//获取栈顶
public function top(){
if(count($this->stack)=='0') return false;
return current($this->stack);
}
public function data(){
return $this->stack;
}
}
//声明栈
$stack = new Stack(10);
$stack->push(5);
$stack->push(6);
$stack->push(7);
$stack->pop();
print_r($stack->top());
print_r($stack->data());die;