一看队列,就不想手写队列了。。。。
直接STL的queue不香吗~~~
好像也不是很香,还是得手动模拟才能让自己更加深刻的体会到队列这个数据结构的精髓的!
不过这里还要注意他的输入和要求,还是有点独特的
push x:将x 加入队尾,保证x 为 int 型整数。
pop:输出队首,并让队首出队
front:输出队首:队首不出队
直接认为一个是head是指针指着尾部,一个是指针指着头的即可#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+1000;
int a[maxn];
int main()
{
string s1;
int n,i,m,j,t;
cin>>m;
int head=0,tail=0;
for(i=0;i<m;i++){
cin>>s1;
if(s1=="push"){
cin>>n;
a[head++]=n;
}
else if(s1=="pop"){
if(tail==head){
cout<<"error"<<endl;
continue;
}
cout<<a[tail]<<endl;
tail++;
}
else if(s1=="front"){
if(tail==head){
cout<<"error"<<endl;
continue;
}
cout<<a[tail]<<endl;
}
}
return 0;
}

京公网安备 11010502036488号