题解 | 【模板】队列

【模板】队列

https://www.nowcoder.com/practice/afe812c80ad946f4b292a26dd13ba549

#include <iostream>
#include <array>
using namespace std;
array<int, 100001> a;//--------因为操作数为100000,tail和head最多只可能到100000,可以直接数组表示,如果太大就要用链表模拟了
int head = 0, tail = -1;//--------是个闭区间,当head>tail时表示区间内没有元素
void push(int n){//-------下面就简单了,tail指向队尾,head指向队首
  tail++;
  a[tail] = n;
}
void pop(){
  if (head > tail)
    puts("error");
  else{
    cout << a[head] << endl;
    head++;
  }
}
void front(){
  if (head > tail)
    puts("error");
  else {
    cout << a[head] << endl;
  }
}
int main() {
  int n;
  cin >> n;
  string s;
  int x;
  for (int i = 0; i < n; i++){
    cin >> s;
    if (s == "push"){
      cin >> x;
      push(x);
    }
    else if (s == "front")
      front();
    else
     pop();
  }
  return 0;
}
// 64 位输出请用 printf("%lld")

#写题解领奖励#
全部评论

相关推荐

牛客78682892...:直接点还好,总比要了简历也不回的强
点赞 评论 收藏
分享
活泼的代码渣渣在泡池...:哈哈哈挺好的,我也上岸美团了,不说了,我又接了一单
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务