【剑指offer 面试题7】用两个栈实现队列
#include <iostream>
#include <stack>
using namespace std; template <typename T> class SQueue
{
public:
SQueue():cnt(){}; void push(const T& node);
T pop();
int getSize(); private:
stack<T> pushstack;
stack<T> popstack; int cnt;
}; template <typename T> void SQueue<T>::push(const T& node)
{
pushstack.push(node); cnt++;
} template <typename T> T SQueue<T>::pop()
{
if(popstack.empty())
{
while(!pushstack.empty())
{
T tmp = pushstack.top();
pushstack.pop();
popstack.push(tmp);
}
} if(popstack.empty())
{
throw new exception();
} T ret = popstack.top();
popstack.pop(); cnt--; return ret;
} template <typename T> int SQueue<T>::getSize()
{
return cnt;
} int main()
{
SQueue<int> Queue;
Queue.push();
Queue.push();
Queue.push(); cout<<"currnet size:"<<Queue.getSize()<<endl; cout<<Queue.pop()<<endl; cout<<"currnet size:"<<Queue.getSize()<<endl; cout<<Queue.pop()<<endl;
cout<<Queue.pop()<<endl;
}
【剑指offer 面试题7】用两个栈实现队列的更多相关文章
- 剑指Offer:面试题7——用两个栈实现队列(java实现)
题目描述:用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 首先定义两个栈 Stack<Integer> stack1 = new Stack<I ...
- 剑指Offer - 九度1512 - 用两个栈实现队列
剑指Offer - 九度1512 - 用两个栈实现队列2013-11-29 21:23 题目描述: 用两个栈来实现一个队列,完成队列的Push和Pop操作.队列中的元素为int类型. 输入: 每个输入 ...
- 剑指offer第二版-9.用两个栈实现队列
描述:使用两个栈实现一个队列.队列中实现尾部插入和头部删除函数. 思路:stack1负责插入,stack2负责弹出,如果stack2为空了,将stack1的元素依次弹出并存放到stack2中,之后对s ...
- 剑指offer【05】- 用两个栈实现队列(java)
题目:用两个栈实现队列 考点:栈和队列 题目描述:用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 解题思路:每次psuh是时先将stack2清空放入stck1(保 ...
- 剑指offer(9)——用两个栈实现队列
题目: 用两个栈实现一个队列.队列的声明如下,请实现它的两个函数appendTail和deleteHead,分别完成在队列尾部插入结点和在队列头部删除结点的功能. 思路: 首先定义两个栈stack1. ...
- JS 剑指Offer(六) 用两个栈实现队列
题目:用两个栈实现队列,实现它的两个函数appendTail和deleteHead,分别完成在队列尾部插入整数和在队列头部删除整数,若队列中没有元素deleteHead返回-1 分析:在队列的尾部插入 ...
- 剑指Offer-【面试题07:两个栈实现队列】
package com.cxz.question7; import java.util.Stack; /** * 用两个栈实现一个队列.队列的声明如下,请实现它的两个函数appendTail 和del ...
- 剑指offer(5)用两个栈实现队列
题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 题目分析 栈是先进后出,队列是先进先出,因此两个栈,一个用来push,一个用来pop,同时注意下两个栈不 ...
- 【剑指Offer】5、用两个栈实现队列
题目描述: 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 解题思路: 本题的基本意图是:用两个后入先出的栈来实现先入先出的队列.对于这个问题,我 ...
- 【剑指Offer】05、用两个栈实现队列
题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 题解一: //stack2有元素就pop,没有元素就将stack1中所有元素倒进来再pop public ...
随机推荐
- spring利用注解来注册bean到容器
1.spring利用注解来定义bean,或者利用注解来注册装配bean.包括注册到ioc中,装配包括成员变量的自动注入. 1.spring会自动扫描所有类的注解,扫描这些注解后,spring会将这些b ...
- maven 环境搭建 Myeclipse配置
一:Maven的下载安装 准备工作: 1)安装环境 Windows xp 2)需安装JDK ,并配置环境变量(略) 3) Maven版本3.0.5 4)下载地址:http://mirror.bit.e ...
- 243. Shortest Word Distance
题目: Given a list of words and two words word1 and word2, return the shortest distance between these ...
- 46. Permutations
题目: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the fo ...
- SQL Server数据库多种方式查找重复记录
摘要:SQL Server是一个关系数据库管理系统,SQL Server数据库的应用是很多的,SQL Server数据库赢得了广大用户的青睐,本文将主要为大家介绍关于SQL Server数据库中查找重 ...
- eclipse(STS,myeclipse)老是报ThreadPoolExecutor$Worker.run()
资料地址:http://stackoverflow.com/questions/6290470/eclipse-debugger-always-blocks-on-threadpoolexecutor ...
- vmware shared holder 虚拟机设置共享目录
1, 安装 vm-tools http://askubuntu.com/questions/29284/how-do-i-mount-shared-folders-win7-host-in-ubunt ...
- mac更新node
今天在用 yeoman 的时候,提示对 npm 和 node 的版本有要求,为了决绝以后遇到的一些类似的问题,我决定定期对 node 和 npm 进行更新. npm的更新: $ sudo npm in ...
- uva 10131 Is Bigger Smarter ? (简单dp 最长上升子序列变形 路径输出)
题目链接 题意:有好多行,每行两个数字,代表大象的体重和智商,求大象体重越来越大,智商越来越低的最长序列,并输出. 思路:先排一下序,再按照最长上升子序列计算就行. 还有注意输入, 刚开始我是这样输入 ...
- JAVA操作数据库插入中文表中显示乱码的解决方法
String dbUrl = "jdbc:mysql://localhost:3306/BookDB?useUnicode=true&characterEncoding=GB2312 ...