题意:

  用队列来实现栈。

思路:

  没有什么捷径,纯粹模拟。但是用一个队列就够了。

 class Stack {
/*
// Push element x onto stack.
void push(int x) { } // Removes the element on top of the stack.
void pop() { } // Get the top element.
int top() { } // Return whether the stack is empty.
bool empty() { }
*/
queue<int> que;
public:
void push(int x)
{
que.push(x);
}
void pop(void)
{
if(!que.empty())
{
int n=que.size(),pos=;
while(pos<n)
{
que.push(que.front());
que.pop();
pos++;
}
que.pop();
}
}
int top()
{
if(!que.empty())
{
int n=que.size(), pos=,x=;
while(pos<=n)
{
if(pos==n) x=que.front();
que.push(que.front());
que.pop();
pos++;
}
return x;
}
}
bool empty(void)
{
if(que.empty()) return true;
else return false;
}
};

AC代码

LeetCode Implement Stack using Queues (数据结构)的更多相关文章

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

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

  2. (leetcode)Implement Stack using Queues

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

  3. LeetCode——Implement Stack using Queues

    Description: Implement the following operations of a stack using queues. push(x) -- Push element x o ...

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

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

  5. 【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( ...

  6. 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 ...

  7. lc面试准备:Implement Stack using Queues

    1 题目 Implement the following operations of a stack using queues. push(x) -- Push element x onto stac ...

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

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

  9. Implement Queue by Two Stacks & Implement Stack using Queues

    Implement Queue by Two Stacks Implement the following operations of a queue using stacks. push(x) -- ...

随机推荐

  1. (巨坑)改了tpl文件之后,前端效果没反应

    通常前端修改很小的部分代码,并不会立即显示出效果.比如,原来是 ,修改后是 虽然只是加多了一个函数,但是作用很大,这种情况下,可能是由于浏览器缓存的原因,修改后的代码没有被重新加载.这个时候,只需要在 ...

  2. PHP获取指定时间的上个月

    主要用strtotime()这个函数 php 获得前一个月的月份 date("Y-m-d",strtotime("last month")); php获得给定时 ...

  3. C#学习笔记思维导图 一本书22张图

    阅读的书是<21天学通C#>博客中有下载 看看总结之后的模块 全部文件 初步展示 数据存储 继承模块 暂时就这些吧 全部思维导图22张打包下载

  4. mysql的部分命令图解

    1.查询有哪些库: show databases;  图中除了Carlton库之外,其它都是系统自带的. 要养成在命令后加入:的习惯 2.查看某个库的表 show tables; 3. 查看表的字段 ...

  5. checkbox 赋值给js 变量

    var banuid =[];//定义一个数组            $('input[name="banuid[]"]:checked').each(function(){//遍 ...

  6. ubuntu下 GCC编译程序出现 undefined reference to `std::ios_base::Init::Init()'问题

    网上的解释是:“ you need to add -lstdc++, or use 'g++' rather than 'gcc' as your driver program.”,也就是说如果想要使 ...

  7. bzoj 3687 bitset的运用

    题目大意: 小呆开始研究集合论了,他提出了关于一个数集四个问题:1. 子集的异或和的算术和.2. 子集的异或和的异或和.3. 子集的算术和的算术和.4. 子集的算术和的异或和.目前为止,小呆已经解决了 ...

  8. OpenLayers简单介绍以及简单实例

    OpenLayers是一个强大的JavaScript包,可以从它的官网免费下载.OpenLayers包含了很多强大的网页地图展示与操作功能,并且能够将不同源的图层展示在同一张地图中,支持各种第三方的地 ...

  9. OpenCV之响应鼠标(二):函数cvSetMouseCallback()和其副程式onMouse()的使用(OpenCV2.4.5)

    每當滑鼠在視訊視窗介面點擊一下的時候,都會有固定三個動作 1.點擊(Click) 2.放開(Down)3.滑動(move) 因此,程式執行滑鼠在點擊的時候onMouse()都會連續跑三次,代表滑鼠在點 ...

  10. Mac下SVN服务器环境的搭建和配置(除展示图片外,所有命令在Linux/Unix下适用)

    这几天领导没有安排工作,闲着没事就想把自己这两年做iOS开发时感觉知识有欠缺的地方想好好深入地补习一下,昨天和今天就计划好好学习下SVN和git的从创建和到原理,到命令,到界面的使用.一不小心被另一领 ...