2014-03-18 05:33

题目:用两个栈来实现一个队列。

解法:栈是反的,队列是正的,反了再反就正过来了。所以,请看代码。操作中时间复杂度有O(1)的,有O(n)的,但均摊下来时间符合O(1)。

代码:

 // 3.5 Implement a queue MyQueue using two stacks.
#include <climits>
#include <cstdio>
#include <cstring>
#include <stack>
using namespace std; template<class T>
class MyQueue {
public:
bool empty() {
return s1.empty() && s2.empty();
} void push(const T &val) {
s1.push(val);
} void pop() {
if (s2.empty()) {
while (!s1.empty()) {
s2.push(s1.top());
s1.pop();
}
}
s2.pop();
} T front() {
if (s2.empty()) {
while (!s1.empty()) {
s2.push(s1.top());
s1.pop();
}
} return s2.top();
} T back() {
if (s1.empty()) {
while (!s2.empty()) {
s1.push(s2.top());
s2.pop();
}
} return s1.top();
}
private:
stack<T> s1, s2;
}; int main()
{
MyQueue<int> qq;
char s[];
int op; while (scanf("%s", s) == ) {
if (strcmp(s, "end") == ) {
break;
} else if (strcmp(s, "push") == ) {
scanf("%d", &op);
qq.push(op);
} else if (strcmp(s, "pop") == ) {
qq.pop();
} else if (strcmp(s, "front") == ) {
printf("%d\n", qq.front());
} else if (strcmp(s, "back") == ) {
printf("%d\n", qq.back());
}
} return ;
}

《Cracking the Coding Interview》——第3章:栈和队列——题目5的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  6. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  7. 数据结构(c语言版,严蔚敏)第3章栈和队列

    第3章栈和队列

  8. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  9. Cracking the Coding Interview 150题(二)

    3.栈与队列 3.1 描述如何只用一个数组来实现三个栈. 3.2 请设计一个栈,除pop与push方法,还支持min方法,可返回栈元素中的最小值.pop.push和min三个方法的时间复杂度必须为O( ...

  10. 数据结构(C语言版)-第3章 栈和队列

    3.1 栈和队列的定义和特点3.2 案例引入3.3 栈的表示和操作的实现3.4 栈与递归3.5 队列的的表示和操作的实现3.6 案例分析与实现 基本操作有入栈.出栈.读栈顶元素值.建栈.判断栈满.栈空 ...

随机推荐

  1. PHP : 封装跳转函数,实现三个页面的跳转

    具体实现:有a,b两个页面,一个跳转页面c,在a执行完后先进行c页面的提示,再跳转到b 1.文件设计: 2.c页面封装方法内容(function.php): a页面内容(a.html): a页面的后台 ...

  2. SIP MGCP和H323的区别

    在Windows中内置的NetMeeting就是典型的H.323协议客户端,而比较常见的SIP系统是微软开发的MSN Messenger系统.首先,由用户A向SIP服务器发出呼叫请求,请求的信息包含自 ...

  3. matlab所需插件

    1

  4. test image

    Most of these images are in PBM or PGM format and compressed with GNU Zip and GNU TAR Note: These pa ...

  5. CentOS上安装mongodb

    安装mongodb pymongo修改yum源vi /etc/yum.repos.d/10gen.repo[10gen]name=10gen Repositorybaseurl=http://down ...

  6. URL中传递JSON数据

    有关于JSON如何在前后之间进行传递,转换成对象等操作,请查看  C#Json数据交互   (这篇文章主要介绍了如何转化Json格式的数据,以及如何使用) 我们在URL中传递数据一般都是 XX.asp ...

  7. jquery 标签中的属性操作

    .arrt() 获取匹配的元素集合中的第一个元素的属性值,或设置每一个元素中的一个或多个属性值. .attr(attributeName) $("em").attr("t ...

  8. JavaScript_DOM学习篇_图片切换小案例

    今天开始学习DOM操作,下面写一个小案例来巩固下知识点. DOM: document object model (文档对象模型) 根据id获取页面元素 : 如: var xx = document.g ...

  9. 转:mysql远程连接 Host * is not allowed to connect to this MySQL server

    在本机登入mysql后,更改"mysql"数据库里的"user"表里的"host"项,从"localhost"改为'%' ...

  10. Ubuntu 下安装GIMP

    1.Add GIMP PPA Open terminal from Unity Dash, App launcher, or via Ctrl+Alt+T shortcut key. When it ...