<-制作不易,您的好评就是我的动力~
传送门
一共四种操作
1. push x:向队尾插入一个数x 🐂🐂🐂
2. pop:从队头弹出一个数 🐂🐂🐂
3. empty:判断队列是否为空(是空的则为YES,非空的则为NO) 🐂🐂🐂
4. query:输出队头元素 🐂🐂🐂
//关键词用 黑色加粗。
第一种解法:数组模拟
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=100010;
int q[N];
int hh=0,tt=-1;
int m;
void push(int x){
q[++tt]=x;
}
void pop(){
hh++;
}
void empty(){
if(tt>=hh) puts("NO");
else puts("YES");
}
void query(){
cout<<q[hh]<<endl;
}
int main(){
cin>>m;
while(m--){
string s;
cin>>s;
if(s=="push"){
int x;
cin>>x;
push(x);
}
if(s=="pop"){
pop();
}
if(s=="empty"){
empty();
}
if(s=="query"){
query();
}
}
return 0;
}
恭喜你打败了80%的人 🎁
第二种解法:结构体
#include<bits/stdc++.h>
using namespace std;
struct queues{
int q[100010];
int tt=-1,hh=0;
} x;
void push(int xo){
x.q[++x.tt]=xo;
}
void pop(){
x.hh++;
}
void empty(){
if(x.tt>=x.hh) puts("NO");
else puts("YES");
}
void query(){
cout<<x.q[x.hh]<<endl;
}
int main(){
int n;
cin>>n;
while(n--){
string s;
cin>>s;
if(s=="push"){
int xo;
cin>>xo;
push(xo);
}
if(s=="pop"){
pop();
}
if(s=="empty"){
empty();
}
if(s=="query"){
query();
}
}
return 0;
}
恭喜你打败了40%的人 🎁
第三种解法:STL
#include<bits/stdc++.h>
using namespace std;
int main(){
queue<int> q;
int n;
cin>>n;
for(int i=1;i<=n;i++){
string s;
cin>>s;
if(s=="push"){
int x;
cin>>x;
q.push(x);
}
if(s=="pop"){
q.pop();
}
if(s=="empty"){
if(q.empty()) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
if(s=="query"){
cout<<q.front()<<endl;
}
}
return 0;
}
恭喜你打败了25%的人 🎁
第四种解法:打表
敬请期待~