数组模拟栈和队列
栈:后进先出
队列:先进先出
单调栈,单调队列
——>我的另一篇博文
栈
#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;
}
}
}