4.模拟队列

 

 库函数做法

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int main() {
 4     int m;
 5     cin >> m;
 6     queue<int> q;
 7     while (m--) {
 8         string s;
 9         int x;
10         cin >> s;
11         if (s == "push") {
12             cin >> x;
13             q.push(x);
14         } else if (s == "pop") {
15             q.pop();
16         } else if (s == "empty") {
17             if (q.empty()) {
18                 cout << "YES" << endl;
19             } else {
20                 cout << "NO" << endl;
21             }
22         } else if (s == "query") {
23             cout << q.front() << endl;
24         }
25     }
26     return 0;
27 }

数组模拟做法

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N = 100010;
 4 int q[N], hh, tt = -1;
 5 int main() {
 6     int m;
 7     cin >> m;
 8     while (m--) {
 9         string op;
10         int x;
11         cin >> op;
12         if (op == "push") {
13             cin >> x;
14             q[++tt] = x;
15         } else if (op == "pop") {
16             hh++;
17         } else if (op == "empty") {
18             if (hh <= tt) {
19                 cout << "NO" << endl;
20             } else {
21                 cout << "YES" << endl;
22             }
23         } else if (op == "query") {
24             cout << q[hh] << endl;
25         }
26     }
27     return 0;
28 }
原文地址:https://www.cnblogs.com/fx1998/p/13285023.html