数组模拟栈和队列

数组模拟栈#include<iostream>using namespace std;const int N=100010;int arr[N];int main(){ int n,tt=1; cin>>n; while(n--) { string ch; int x; cin>>ch; if(ch=="push") {
摘要由CSDN通过智能技术生成

数组模拟栈和队列

栈:后进先出
队列:先进先出

单调栈,单调队列——>我的另一篇博文

#include<iostream>
using namespace std;
const int N=100010;
int arr[N];


int main()
{
    int n,tt=1;
    cin>>n;
    while(n--)
    {
        string ch;
        int x;
        cin>>ch;
        if(ch=="push")
        {
            cin>>x;
           arr[tt++]=x;
        }
        else if(ch=="pop")
        {
            tt--;
        }
        else if(ch=="empty")
        {
            if(tt>1)
                cout<<"NO"<<endl;
            else
                cout<<"YES"<<endl;
        }
        else if(ch=="query")
        {
            cout<<arr[tt-1]<<endl;
        }

    }
}
队列

#include<iostream>

using namespace std;
const int N=100010;
int hh=0,tt=-1,a[N];

int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        string str;
        int x;
        cin>>str;
        if(str=="push")
        {
            cin>>x;
            a[++tt]=x;
        }
        else if(str=="pop")
        {
            hh++;
        }
        else if(str=="empty")
        {
            if(tt<hh)
                cout<<"YES"<<endl;
            else
                cout<<"NO"<<endl;
        }
        else if(str=="query")
        {
            cout<<a[hh]<<endl;
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值