数组模拟栈

数组模拟栈:

值得注意的是:栈中tt从0开始
这一点区别于队列

AcWing对应题目[https://www.acwing.com/problem/content/830/]

#include<iostream>
#include<stdio.h>
using namespace std;
const int N=100010;
int skt[N],tt=0;//栈从0开始
//入栈
void push(int n)
{
    skt[++tt]=n;
}
//出栈
void pop()
{
    tt--;
}
//判🈳
bool empty()
{
    if(tt==0)
    {
        return true;
    }
    else{
        return false;
    }

}
int query()//查询栈顶元素
{
    return skt[tt];
}
int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
        string str;
        int num;
        cin>>str;
        if(str=="push")
        {

        scanf("%d",&num);
            push(num);
        }
        if(str=="pop")
        {
            pop();
        }
        if(str=="empty")
        {
           if(empty())
           {
            printf("YES\n");
           }
           else{
            printf("NO\n");
           }

        }
        if(str=="query")
        {
            printf("%d\n",query());
        }
    }
}
posted @   划水马师傅  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示