数据结构-栈

特点:先进后出
1.数组模拟栈
AcWing-828

#include <iostream>
#include <stack>

using namespace std;

const int N=1e5+10;
int m;
int stk[N];
int tt;//栈顶下标

void push(int x)//插入
{
    stk[++tt]=x;
}

void pop()//弹出
{
    tt--;
}

bool empty()//判读是否为空
{
    if(tt>0) return false;
    else return true;
}

int query()//取出栈顶元素
{
   return stk[tt]; 
}


int main()
{
    cin>>m;
    while(m--)//操作
    {
        string s;
        cin>>s;
        if(s=="push")
        {
            int x;
            cin>>x;
            push(x);
        }
        else if(s=="pop") pop();
        else if(s=="empty") {if(empty()) cout<<"YES"<<endl; else cout<<"NO"<<endl;}
        else cout<<query()<<endl;
    }
    return 0;
}
posted @   Eric`  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
点击右上角即可分享
微信分享提示