《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 案例分析与实现 基本操作有入栈.出栈.读栈顶元素值.建栈.判断栈满.栈空 ...
 
随机推荐
- js  call(),apply(),对象冒充,改变变量作用域
			
1.apply(); function box(n1,n2){ return n1+n2; } function pox(n1,n2){ alert(box.apply(this,[n1,n2])); ...
 - 显示C++ vector中的数据
			
C++ 中的vector是一个容器数据类型,不能使用cout直接显示容器中的值. 以下程序中,myvector 是一个vector数据类型.将myvector替换为需要输出的vector. for(i ...
 - 腾讯云“动态加速”与“CDN”的区别——浅谈对“动态加速”的理解(可能有误)
			
CDN的劣势及“动态加速”产生背景 通常CDN对静态内容支持较好,若使用其加速动态内容,可能会导致异常(如导致无法登录).当然,可以将动态内容的在CDN节点上的缓存时间设置为0秒来解决.但这毕竟是用户 ...
 - Google搜索引擎使用小技巧
			
相信大家都知道,利用Google等搜索引擎进行信息查证是翻译过程中十分重要的一环.事实上,掌握信息搜索的技巧和方法,不仅对翻译工作大有帮助, 在网络信息时代,学会充分利用搜索引擎,在很多情况下都可以达 ...
 - Vue路由讲解
			
1>router-link和router-view组件 2>路由配置 a.动态路由 import Home from "@/views/Home.vue"; expor ...
 - 项目 XXX 受源代码管理。向源代码管理注册此项目时出错。建议不要对此项目进行任何修改
			
原本带vss或者svn管理的项目独立复制出来后,如果出现下面问题 解决办法: 使用记事本打开,项目csproj文件删除图中
 - Spring 学习之依赖注入
			
什么是依赖关系? 纵观所有的Java 应用,从基于Applet的小应用到多层次结构的企业级别的应用,他们都是一种典型的依赖性应用,也就是由一些互相协作的对象构成的,Spring把这种互相协作的关系称之 ...
 - 4.vue引入axios同源跨域
			
前言: 跨域方案有很多种,既然我们用到了Vue,那么就使用vue提供的跨域方案. 解决方案: 1.修改HttpRequestUtil.js import axios from 'axios' expo ...
 - 4、SpringBoot+Mybatis整合------一对多
			
开发工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis/tree/c00b56dbd51a1e26ab9fd99020 ...
 - linux下Tomcat配置提示权限不够解决办法
			
在终端输入命令 sudo chmod -R 777 /opt/Tomcat,那么Tomcat文件夹和它下面的所有子文件夹的属性都变成了777(读/写/执行权限)