Implement Stack using Queues 用队列实现栈
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.
此时队列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;
}
运行上面代码得到结果为:
Implement Stack using Queues 用队列实现栈的更多相关文章
- LeetCode OJ:Implement Stack using Queues(队列实现栈)
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- 225 Implement Stack using Queues(用队列实现栈Medium)
题目意思:用队列实现栈,push(),pop(),top(),empty() 思路:用两个queue,pop时将一个queue的元素pop再push到另一个队列,queue只留最后一个元素,并pop, ...
- LeetCode 225 Implement Stack using Queues 用队列实现栈
1.两个队列实现,始终保持一个队列为空即可 class MyStack { public: /** Initialize your data structure here. */ MyStack() ...
- [LeetCode] Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- [LeetCode] 225. Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
- 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 ...
- 【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( ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
随机推荐
- DQL查询语句使用(select)
9)DQL查询语句使用 SELECT语句在PL/SQL中使用,必须 采用下面用法: select id INTO 变量 from t001 where id=5; 将记录字段 ...
- nginx配置修改
改变nginx配置想让它生效而不停止服务,如下两种方式都可以:1) 检查nginx配置: nginx -t; 配置重载: nginx -s reload2) 检查nginx配置: nginx -t; ...
- Python——异常基础
异常基础 在Python中,异常会依据错误自己主动地被触发.也能由代码触发和截获.异常由五个语句处理: 1.[try/except]:捕捉由Python或你引起的异常并恢复. 2.[try/final ...
- node-webkit 主页面和 iframe 页通讯
<html lang="en-US"> <head> <title>Hello World!</title> <style&g ...
- jquery时间格式化插件
插件的代码: (function($){ $.formatDate = function(pattern,date){ //假设不设置,默觉得当前时间 if(!date) date = new Dat ...
- UVA 11077 Find the Permutations 递推置换
Find the Permutations Sorting is one of the most used operations in real ...
- oracle 11g RAC 的一些基本概念
一.脑裂以及对策 脑裂(split-brain)是集群中的一个糟糕的情况:集群中的所有集群正在工作的时候,内部通讯被断开.这种情况下,集群被分成了几个部分,每个部分的集群软件都会尝试去接管其他节点的资 ...
- CentOS 6.5 下编译安装 Nginx 1.8.0
转自:https://i.cnblogs.com/EditPosts.aspx?postid=8303227&update=1 安装编译依赖的包 yum -y install gcc gcc- ...
- MYSQL主从复制搭建及切换操作(GTID与传统)
结构如下: MYSQL主从复制方式有默认的复制方式异步复制,5.5版本之后半同步复制,5.6版本之后新增GTID复制,包括5.7版本的多源复制. MYSQL版本:5.7.20 操作系统版本:linux ...
- weboffice7
document.all.WebOffice1.ShowToolBar = false;