首页 > 其他 > 详细

4.模拟队列

时间:2020-07-11 19:51:28      阅读:47      评论:0      收藏:0      [点我收藏+]

技术分享图片

 技术分享图片

 库函数做法

 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 }

 

4.模拟队列

原文:https://www.cnblogs.com/fx1998/p/13285023.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!