Implement the following operations of a stack using queues.

  • push(x) -- Push element x onto stack.
  • pop() -- Removes the element on top of the stack.
  • top() -- Get the top element.
  • empty() -- Return whether the stack is empty.
解题思路:
用队列实现栈的操作。包含输入、输出、取栈顶元素、推断栈是否为空。
我们知道,栈是先进后出的容器,队列是先进先出的容器。所以输入元素非常easy,可是输出元素不是输出队列的front,而是back元素。删掉back元素。此时我们能够将队列1中的非队尾元素存在另外一个队列2中,删掉之前队列的最后一个元素不另行存储即可。

此时队列2中有元素。是可运行的容器。队列1是空,备用容器,所以我们须要两个表示符,代表两个队列,表示此队列是否是当前可操作的容器。简单来讲,就是用两个队列的操作(push()。pop(),empty())来实现栈的各种操作。


完整可运行代码例如以下:

#include<iostream>
#include<queue>
using namespace std;
queue<int> qu1;
queue<int> qu2;
bool qu1_use=1;
bool qu2_use=0;
void push(int x);
void pop() ;
int top() ;
bool empty() ;
void main()
{
push(1);
push(2);
push(3);
push(4);
int i=5;
while(i)
{
if(!empty())
{
cout<<top()<<endl;
pop();
}
i--;
} }
void push(int x)
{
if(qu1_use==1)
{
qu1.push(x);
}
else
qu2.push(x);
}
void pop()
{
if(qu1.empty()&&qu2.empty())
exit(0);
if(qu1_use==1&&!qu1.empty())
{
while(qu1.size()>1)
{
qu2.push(qu1.front());
qu1.pop();
}
qu1.pop();
qu1_use=0;
qu2_use=1;
return;
}
if(qu2_use==1&&!qu2.empty())
{
while(qu2.size()>1)
{
qu1.push(qu2.front());
qu2.pop();
}
qu2.pop();
qu1_use=1;
qu2_use=0;
return;
}
return;
} int top()
{ if(qu1_use==1&&!qu1.empty())
{
return qu1.back();
}
if(qu2_use==1&&!qu2.empty())
{
return qu2.back();
}
//if(qu1.empty()&&qu2.empty())
exit(0);
} bool empty()
{
if((qu1_use==1&&qu1.empty())||(qu2_use==1&&qu2.empty()))
{
cout<<"empty!"<<endl;
return true;
} else return false;
}

运行上面代码得到结果为:

4
3
2
1
empty!

Implement Stack using Queues 用队列实现栈的更多相关文章

  1. LeetCode OJ:Implement Stack using Queues(队列实现栈)

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  2. 225 Implement Stack using Queues(用队列实现栈Medium)

    题目意思:用队列实现栈,push(),pop(),top(),empty() 思路:用两个queue,pop时将一个queue的元素pop再push到另一个队列,queue只留最后一个元素,并pop, ...

  3. LeetCode 225 Implement Stack using Queues 用队列实现栈

    1.两个队列实现,始终保持一个队列为空即可 class MyStack { public: /** Initialize your data structure here. */ MyStack() ...

  4. [LeetCode] Implement Stack using Queues 用队列来实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  5. [LeetCode] 225. Implement Stack using Queues 用队列来实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  6. leetcode:Implement Stack using Queues 与 Implement Queue using Stacks

    一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...

  7. leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues

    155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...

  8. 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues

    232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...

  9. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

随机推荐

  1. DQL查询语句使用(select)

      9)DQL查询语句使用   SELECT语句在PL/SQL中使用,必须 采用下面用法:     select id INTO 变量   from t001 where id=5;    将记录字段 ...

  2. nginx配置修改

    改变nginx配置想让它生效而不停止服务,如下两种方式都可以:1) 检查nginx配置: nginx -t; 配置重载: nginx -s reload2) 检查nginx配置: nginx -t; ...

  3. Python——异常基础

    异常基础 在Python中,异常会依据错误自己主动地被触发.也能由代码触发和截获.异常由五个语句处理: 1.[try/except]:捕捉由Python或你引起的异常并恢复. 2.[try/final ...

  4. node-webkit 主页面和 iframe 页通讯

    <html lang="en-US"> <head> <title>Hello World!</title> <style&g ...

  5. jquery时间格式化插件

    插件的代码: (function($){ $.formatDate = function(pattern,date){ //假设不设置,默觉得当前时间 if(!date) date = new Dat ...

  6. UVA 11077 Find the Permutations 递推置换

                               Find the Permutations Sorting is one of the most used operations in real ...

  7. oracle 11g RAC 的一些基本概念

    一.脑裂以及对策 脑裂(split-brain)是集群中的一个糟糕的情况:集群中的所有集群正在工作的时候,内部通讯被断开.这种情况下,集群被分成了几个部分,每个部分的集群软件都会尝试去接管其他节点的资 ...

  8. CentOS 6.5 下编译安装 Nginx 1.8.0

    转自:https://i.cnblogs.com/EditPosts.aspx?postid=8303227&update=1 安装编译依赖的包 yum -y install gcc gcc- ...

  9. MYSQL主从复制搭建及切换操作(GTID与传统)

    结构如下: MYSQL主从复制方式有默认的复制方式异步复制,5.5版本之后半同步复制,5.6版本之后新增GTID复制,包括5.7版本的多源复制. MYSQL版本:5.7.20 操作系统版本:linux ...

  10. weboffice7

    document.all.WebOffice1.ShowToolBar = false;