数组模拟栈

题目

代码

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
int stack[N], tt;
void push_stack(int x)
{
stack[++tt] = x;
}
void pop_stack()
{
tt--;
}
int empty_stack()
{ // 返回1则为空,0则不为空。
return tt == 0;
}
int top_stack()
{
return stack[tt];
}
int main()
{
int m;
cin >> m;
while (m--)
{
string str;
cin >> str;
if (str == "push")
{
int x;
cin >> x;
push_stack(x);
}
else if (str == "pop")
{
pop_stack();
}
else if (str == "empty")
{
if (empty_stack())
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
}
else
{
cout << top_stack() << endl;
}
}
return 0;
}
posted @   electricity111  阅读(2)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示