废话少说,上代码(talk is easy ,show me the code):
<?php
/**
*通过php数组构造简单的顺序栈
*/
class SimpleStack{
private $stack=[];
private $size;
public function __construct($size = 10){
$this->size = $size;
}
//获取栈顶元素
public function pop(){
if(count($this->stack) === 0){
return false;
}
return array_pop($this->stack);
}
//向栈中推入元素
public function push($value){
if(count($this->stack) === $this->size){
return false;
}
array_push($this->stack, $value);
return true;
}
//返回栈的大小
public function size(){
return count($this->stack);
}
//判断栈是否时空栈
public function isEmpty(){
return current($this->stack)===false;
}
}
$newStack = new SimpleStack(15);
var_dump($newStack->isEmpty());
$newStack->push(111);
$newStack->push("lirui");
$newStack->push("33333");
$newStack->pop();
var_dump($newStack->size());
通过php数组构造简单的顺序栈
最新推荐文章于 2021-03-17 22:51:49 发布