跳到正文

算法·理论

stack 栈

ting15319约 1 分钟
原文
文章信息与版本

文章信息

发布
2025年12月19日
收录
2026年1月7日
更新
2026年11月13日
篇幅
约 1 分钟

版本

  1. v2当前
  2. v1
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
	stack<int>a;
	int n,y;
	cin>>n;
	string s;
	while(n--)
	{
		cin>>s;
		if(s=="push")
		{
			cin>>y;
			a.push(y);
		}
		if(s=="pop")
		{

			a.pop();
		}
		if(s=="empty")
		{
			if(a.empty())
			{
				cout<<"YES\n";
			}
			else
			{
				cout<<"NO\n";
			}
		}
		if(s=="query")
		{
			cout<<a.top()<<"\n";
		}
	}
}
原文