《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 案例分析与实现 基本操作有入栈.出栈.读栈顶元素值.建栈.判断栈满.栈空 ...
随机推荐
- 2017.10.28 QB模拟赛 —— 上午
题目链接 T1 1e18 内的立方数有 1e6个 直接枚举可过 二分最优 考场用set 死慢.. #include <cstdio> int t; long long p; int ma ...
- 双击易语言没有反应,按住shift再双击可解决
参考资料:http://tieba.baidu.com/p/2987732743 的7楼.
- MySQL入门很简单: 3 操作数据库
登陆:mysq -u root -p 0409 1). 创建, 删除数据库 SHOW DATABASES; 显示已经存在的数据率 CREATE DATABASES 数据库名: 创建数据库 DROP D ...
- 数黑格有多少个,模拟题,POJ(1656)
题目链接:http://poj.org/problem?id=1656 #include <stdio.h> #include <iostream> #include < ...
- nginx里面的rewrite配置
哎,我需要静静,刚刚在去怎么优化dom层级,发现更新完代码,层级又蹭蹭蹭的往上涨,顿时没脾气了,还是把昨天的nginx配置总结下,增加点动力,昨天前天两天都在搞这个问题,也是搞的没脾气,网上查了很多资 ...
- iOS内存管理部分内容
Objective-C 高级编程 iOS与OS X多线程和内存管理第一章部分讲述了关于ARC的内容,还讲述了关于修饰符的问题,还讲了好多底层的实现的内容,这些底层实现却往往是在面试的过程中经常被遇到的 ...
- 3.初识Cron表达式
Cron: 计划任务,是任务在约定的时间执行已经计划好的工作,这是表面的意思.在Linux中,我们经常用到 cron 服务器来完成这项工作.cron服务器可以根据配置文件约定的时间来执行特定的作务. ...
- 一篇SSM框架整合友好的文章(一)
转载请标明出处: http://blog.csdn.net/forezp/article/details/53730333 本文出自方志朋的博客 最近实在太忙,之前写的<rxjava系列文章&g ...
- 洛谷P2759 奇怪的函数(log 二分)
题目描述 使得 x^xxx 达到或超过 n 位数字的最小正整数 x 是多少? 输入输出格式 输入格式: 一个正整数 n 输出格式: 使得 x^xxx 达到 n 位数字的最小正整数 x 输入输出样例 输 ...
- (三)、python运算符和基本数据类型
运算符 1.算数运算: 2.比较运算: 3.赋值运算: 4.逻辑运算: 5.成员运算: 基本数据类型 1.数字 int(整形) # python3里不管数字有多长都叫整形# python2里分为整形和 ...