一、题目
活动 - AcWing 系统讲解常用算法与数据结构,给出相应代码模板,并会布置、讲解相应的基础算法题目。https://www.acwing.com/problem/content/831/
二、题解
有多种实现方式,可以通过链表数组等,也可以实现循环队列
#include<iostream>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int M = 100010;
int front = 0, rear = -1;
int q[M];
void empty() {
if (rear + 1 == front) cout << "YES\n";
else cout << "NO\n";
}
void query() {
cout << q[front] << endl;
}
void pop() {
front++;
}
void push(int x) {
q[++rear] = x;
}
int main(){
int m;
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;
}