栈原理:先进后出,设置栈的最大容量,容器
++i:如果i=1 那么这个就是先加 变为2在作为下标
i++:如果i=1 那么这个就是2先作为下标使用,在加变3
当然大家也可以根据容器的个数count后进行判断是否存满
class Data{
const MAXSIZE = 4;// 栈最大容量
private $top = 1;
private $data = array();// 利用数组存储数据
public function __construct(){
$this->data = array();
}
// 入栈
public function push($string){
if ($this->top >= self::MAXSIZE+1){
echo 'data is full...';
return false;
}
$this->data[++$this->top] = $tring;
}
// 出栈,返回出栈元素
public function pop(){
if ($this->top == 1){
echo 'data is empty...';
return false;
}
$string = $this->data[$this->top];
unset($this->data[$this->top--]);
return $string;
}
// 遍历栈
public function show(){
if ($this->top == -1){
echo 'data is empty';
return false;
}
for($i=$this->top; $i>-1; $i--){
echo $this->data[$i].'
';
}
}
}
$data = new Data;
$data->push(1);
$data->push(2);
$data->push(3);
$data->push(4);
$data->show();
$a = $data->pop();
$a = $data->pop();
$a = $data->pop();
$a = $data->pop();
$stack->show();```