题解 | #【模板】队列#
【模板】队列
https://www.nowcoder.com/practice/afe812c80ad946f4b292a26dd13ba549
#include <iostream>
using namespace std;
const int N = 100010;
int a[N];
int main() {
int n;
int index = 0;
int top = 0;
cin >> n;
while(n--){
string s;
cin >> s;
if(s == "push"){
int x;
cin >> x;
a[index++] = x;
}else if(s == "pop"){
if(index == top) cout << "error" << endl;
else cout << a[top++] << endl;
}else if(s == "front"){
if(index == top) cout << "error" << endl;
else cout << a[top] << endl;
}
}
}
// 64 位输出请用 printf("%lld")
用数组模拟实现,两个指针指向数组,一个为头部,一个为当前元素
海康威视公司福利 1102人发布
查看6道真题和解析