《Cracking the Coding Interview》——第3章:栈和队列——题目7
2014-03-19 03:20
题目:实现一个包含阿猫阿狗的先入先出队列,我要猫就给我一只来的最早的猫,要狗就给我一只来的最早的狗,随便要就给我一只来的最早的宠物。建议用链表实现。
解法:单链表可以实现一个简单的队列,这种有不同种类数据的队列,可以用if语句选择,也可以用一个堆做时间上的优化。对于来的时间,我用一个64位整数表示时间戳,在地球被太阳吃掉以前,这个数字是不大可能上溢的,所以问题应该不大。
代码:
// 3.7 Implement a queue that holds cats and dogs. If you want dog or cat, you get the oldest dog or cat. If you want a pet, you get the oldest of them all.
#include <iostream>
#include <string>
using namespace std; template <class T>
struct ListNode {
T val;
long long int id;
ListNode *next; ListNode(T _val = , long long int _id = ): val(_val), id(_id), next(nullptr) {};
}; template <class T>
class CatsAndDogs {
public:
CatsAndDogs() {
first_cat = last_cat = first_dog = last_dog = nullptr;
current_id = ;
} void enqueue(T val, int dog_or_cat) {
switch (dog_or_cat) {
case :
// cat
if (first_cat == nullptr) {
first_cat = last_cat = new ListNode<T>(val, current_id);
} else {
last_cat->next = new ListNode<T>(val, current_id);
last_cat = last_cat->next;
}
break;
case :
// dog
if (first_dog == nullptr) {
first_dog = last_dog = new ListNode<T>(val, current_id);
} else {
last_dog->next = new ListNode<T>(val, current_id);
last_dog = last_dog->next;
}
break;
}
++current_id;
} T dequeueAny() {
if (first_cat == nullptr) {
return dequeueDog();
} else if (first_dog == nullptr) {
return dequeueCat();
} else if (first_cat->id < first_dog->id) {
return dequeueCat();
} else {
return dequeueDog();
}
} T dequeueCat() {
T result; result = first_cat->val;
if (first_cat == last_cat) {
delete first_cat;
first_cat = last_cat = nullptr;
} else {
ListNode<T> *ptr = first_cat;
first_cat = first_cat->next;
delete ptr;
} return result;
} T dequeueDog() {
T result; result = first_dog->val;
if (first_dog == last_dog) {
delete first_dog;
first_dog = last_dog = nullptr;
} else {
ListNode<T> *ptr = first_dog;
first_dog = first_dog->next;
delete ptr;
} return result;
}
private:
ListNode<T> *first_cat;
ListNode<T> *first_dog;
ListNode<T> *last_cat;
ListNode<T> *last_dog;
long long int current_id;
}; int main()
{
CatsAndDogs<string> cd;
string val;
string type;
string cmd; while (cin >> cmd) {
if (cmd == "end") {
break;
} else if (cmd == "in") {
cin >> type;
if (type == "cat") {
cin >> val;
cd.enqueue(val, );
} else if (type == "dog") {
cin >> val;
cd.enqueue(val, );
}
} else if (cmd == "out") {
cin >> type;
if (type == "cat") {
cout << "cat=" << cd.dequeueCat() << endl;
} else if (type == "dog") {
cout << "dog=" << cd.dequeueDog() << endl;
} else if (type == "any") {
cout << "any=" << cd.dequeueAny() << endl;
}
}
} return ;
}
《Cracking the Coding Interview》——第3章:栈和队列——题目7的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- 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 ...
- 数据结构(c语言版,严蔚敏)第3章栈和队列
第3章栈和队列
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- Cracking the Coding Interview 150题(二)
3.栈与队列 3.1 描述如何只用一个数组来实现三个栈. 3.2 请设计一个栈,除pop与push方法,还支持min方法,可返回栈元素中的最小值.pop.push和min三个方法的时间复杂度必须为O( ...
- 数据结构(C语言版)-第3章 栈和队列
3.1 栈和队列的定义和特点3.2 案例引入3.3 栈的表示和操作的实现3.4 栈与递归3.5 队列的的表示和操作的实现3.6 案例分析与实现 基本操作有入栈.出栈.读栈顶元素值.建栈.判断栈满.栈空 ...
随机推荐
- Verilog分频器的设计
大三都要结束了,才发现自己太多东西没深入学习. 对于偶分频:(计数到分频数的一半就翻转) 注: 图中只用了一个计数器,当然也可以用多个: 图中只计数到需要分频的一半,当然也可计数到更多: 图中从第一个 ...
- COGS 182. [USACO Jan07] 均衡队形
★★ 输入文件:lineup.in 输出文件:lineup.out 简单对比时间限制:4 s 内存限制:128 MB 题目描述 农夫约翰的 N (1 ≤ N ≤ 50,000) 头奶牛 ...
- 页面文本超出后CSS实现隐藏的方法
text-overflow: ellipsis !important; white-space: nowrap !important; overflow: hidden !important; dis ...
- 解决Jenkins的错误“The Server rejected the connection: None of the protocols were accepted”
1. 配置节点,配置好节点后,在节点机上运行已下载文件,双击执行,提示"The Server rejected the connection: None of the protocols w ...
- C语言中volatile关键字的作用[转]
一.前言 1.编译器优化介绍: 由于内存访问速度远不及CPU处理速度,为提高机器整体性能,在硬件上引入硬件高速缓存Cache,加速对内存的访问.另外在现代CPU中指令的执行并不一定严格按照顺序执行,没 ...
- C# 复合赋值操作符
前面讲过如何使用算术操作符来创建新值.例如,以下语句使用操作符+来创建比变量answer大42的一个值,新值将写入控制台: Console.WriteLine(answer + 42); 前面还讲过如 ...
- sup inf max min
来自这里,觉得定义和举例都是最清楚的.http://www.math.illinois.edu/~ajh/347.summer14/completeness.pdf
- 初尝微信小程序2-基本框架
基本框架: .wxml :页面骨架 .wxss :页面样式 .js :页面逻辑 描述一些行为 .json :页面配置 创建一个小程序之后,app.js,app.json,app.wxss是必须的 ...
- BZOJ2752: [HAOI2012]高速公路(road)(线段树 期望)
Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 1820 Solved: 736[Submit][Status][Discuss] Descripti ...
- 基于mybatis设计简单信息管理系统1
驼峰式命名法 骆驼式命名法就是当变量名或函数名是由一个或多个单词连结在一起,而构成的唯一识别字时,第一个单词以小写字母开始:第二个单词的首字母大写或每一个单词的首字母都采用大写字母,例如:myFirs ...