鱼C论坛

 找回密码
 立即注册
查看: 3084|回复: 7

[已解决]入队列1、2、3,出队列1,入队列4,再全部出队列

[复制链接]
发表于 2022-3-29 20:21:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
入队列1、2、3,出队列1,入队列4,再全部出队列
最佳答案
2022-3-29 20:51:58
  1. #include <queue>
  2. #include <cstddef>

  3. using std::queue;

  4. int main() {
  5.     queue<size_t> q;
  6.     q.push(1);
  7.     q.push(2);
  8.     q.push(3);
  9.     q.pop();
  10.     q.push(4);
  11.     while(!q.empty()) q.pop();
  12.     return 0;
  13. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-29 20:28:08 | 显示全部楼层
这是什么?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-29 20:51:58 | 显示全部楼层    本楼为最佳答案   
  1. #include <queue>
  2. #include <cstddef>

  3. using std::queue;

  4. int main() {
  5.     queue<size_t> q;
  6.     q.push(1);
  7.     q.push(2);
  8.     q.push(3);
  9.     q.pop();
  10.     q.push(4);
  11.     while(!q.empty()) q.pop();
  12.     return 0;
  13. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-29 21:49:31 | 显示全部楼层

能麻烦再出一个用两个栈实现上述队列的嘛
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-29 22:29:14 | 显示全部楼层
言辞. 发表于 2022-3-29 21:49
能麻烦再出一个用两个栈实现上述队列的嘛

用两个栈实现上述队列?
什么意思?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-29 22:32:56 | 显示全部楼层
人造人 发表于 2022-3-29 22:29
用两个栈实现上述队列?
什么意思?

就是利用两个栈的后进先出实现一个队列先进先出的效果
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-29 23:03:01 | 显示全部楼层
言辞. 发表于 2022-3-29 22:32
就是利用两个栈的后进先出实现一个队列先进先出的效果
  1. #include <stack>
  2. #include <cstddef>

  3. //#define DEBUG

  4. #ifdef DEBUG
  5. #include <iostream>
  6. using std::cout, std::endl;
  7. #endif

  8. using std::stack;

  9. template<typename T>
  10. class queue {
  11. public:
  12.     void push(const T &value) {
  13.         this->lhs.push(value);
  14.     }
  15.     void pop() {
  16.         this->move(this->lhs, this->rhs);
  17.         this->rhs.pop();
  18.         this->move(this->rhs, this->lhs);
  19.     }
  20.     const T top() {
  21.         this->move(this->lhs, this->rhs);
  22.         const T result = this->rhs.top();
  23.         this->move(this->rhs, this->lhs);
  24.         return result;
  25.     }
  26.     bool empty() {
  27.         return this->lhs.empty();
  28.     }
  29. private:
  30.     stack<T> lhs, rhs;

  31.     // lhs to rhs
  32.     static void move(stack<T> &lhs, stack<T> &rhs) {
  33.         while(!lhs.empty()) {
  34.             rhs.push(lhs.top());
  35.             lhs.pop();
  36.         }
  37.     }
  38. };

  39. int main() {
  40. #ifdef DEBUG
  41.     {
  42.         queue<size_t> q;
  43.         q.push(1);
  44.         q.push(2);
  45.         q.push(3);

  46.         cout << q.top() << endl;
  47.         q.pop();

  48.         q.push(4);
  49.         while(!q.empty()) {
  50.             cout << q.top() << endl;
  51.             q.pop();
  52.         }
  53.         return 0;
  54.     }
  55. #endif
  56.     queue<size_t> q;
  57.     q.push(1);
  58.     q.push(2);
  59.     q.push(3);
  60.     q.pop();
  61.     q.push(4);
  62.     while(!q.empty()) q.pop();
  63.     return 0;
  64. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-30 09:11:14 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-12 00:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表