当前位置:网站首页>栈,后入先出
栈,后入先出
2022-06-26 12:32:00 【liuliang514218119】
<?php
# 后入先出 栈
class stack
{
public $data = [];
public $size;
public function __construct($size = 10)
{
$this->size = $size;
}
# 入栈
public function push($element)
{
if (count($this->data) >= $this->size) {
throw new Exception("栈已满");
return "栈已满";
}
array_push($this->data, $element);
}
# 出栈
public function pop()
{
if (count($this->data) < 1) {
throw new Exception("栈为空");
return "栈为空";
}
return array_pop($this->data);
}
# 栈顶
public function get_top()
{
if (count($this->data) < 1) {
throw new Exception("栈为空");
return "栈为空";
}
return end($this->data); #内部指针指向数组中的最后一个元素
}
public function get_data()
{
return $this->data;
}
}
//声明栈
$stack = new stack();
$stack->push(5);
$stack->push(6);
$stack->push(7);
$stack->push(8);
$stack->push(9);
$stack->pop();
print_r($stack->get_top());
echo "\n";
print_r($stack->get_data());
die;

边栏推荐
- A most practical arbitrage wizard EA [2022 modified version]
- What software is flush? Is online account opening safe?
- On the use of protostaff [easy to understand]
- Encapsulate request request of uni app
- Lintcode 130 · 堆化
- SQL injection in Pikachu shooting range
- Scala-day06- pattern matching - Generic
- VMware virtual machine bridging mode can not access the campus network "suggestions collection"
- VMware虚拟机 桥接模式 无法上网 校园网「建议收藏」
- 【Redis 系列】redis 学习十六,redis 字典(map) 及其核心编码结构
猜你喜欢

Vscode solves the problem of Chinese garbled code

Realize microservice load balancing (ribbon)

2、 MySQL Foundation

HUST網絡攻防實踐|6_物聯網設備固件安全實驗|實驗二 基於 MPU 的物聯網設備攻擊緩解技術

Scala-day02- variables and data types

11、 Box styles and user interface

TSMC Samsung will mass produce 3nm chips in 2022: will the iPhone be the first?

Laravel+gatewayworker completes the im instant messaging and file transfer functions (Chapter 4: server debugging errors)

Using the methods in the repository to solve practical problems

Deep thinking from senior member managers
随机推荐
汇编语言(7)运算指令
webgame开发中的文件解密
Fengshentai old shooting range Kali series
我想知道,股票开户有哪些优惠活动?网上开户是否安全么?
International beauty industry giants bet on China
redis通过6379端口无法连接服务器
How long ago did PHP get
Five trends of member management in 2022
女性科学家的流失
Deep thinking from senior member managers
Comparison of latest mobile phone processors in 2020 (with mobile phone CPU ladder diagram)
环形队列php
Current situation investigation and investment prospect forecast analysis report of China's electrolytic copper market from 2022 to 2028
Which is safer and better for great wisdom to open an account
Build Pikachu shooting range and introduction
Thinkphp5 query report: sqlstate[hy093]: invalid parameter number
国际美妆业巨头押注中国
Leetcode 78. 子集 and 90. 子集 II
2021 q3-q4 investigation report on the use status of kotlin multiplatform
Vulnerability scanning and reverse osmosis of Internet anti artifact