《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 案例分析与实现 基本操作有入栈.出栈.读栈顶元素值.建栈.判断栈满.栈空 ...
随机推荐
- Android(java)学习笔记97:使用GridView以及重写BaseAdapter
1. BaseAdapter: 对于ListView.GridView.Gallery.Spinner等等,它是它们的适配器,直接继承自接口类Adapter的,使用BaseAdapter时需要重写很多 ...
- BestCoder Round #91 1002 Lotus and Horticulture
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6012 题意: 这几天Lotus对培养盆栽很感兴趣,于是她想搭建一个温室来满足她的研究欲望. Lotus ...
- 【转】jpg png区别和使用
为什么想整理这方面的类容,我觉得就像油画家要了解他的颜料和画布.雕塑家要了解他的石材一样,作为网页设计师也应该对图片格式的特性有一定了解,这样才能更好的表达你的创意和想法. 除此之外,我们在平时工作中 ...
- sublime text 3 python 控制台输出中文乱码解决方案
自建的python运行环境如下:python3 找到python3.sublime-build文件打开,在文件中加入"env": { "PYTHONIOENCODING& ...
- spring boot1.5.6 测试类1
package com.example.demo; import org.junit.Before;import org.junit.Test; import org.junit.runner.Run ...
- 数据库可视化工具简介以及pymysql的使用
1.可视化工具Navicat 我们自己开发测试时,可以使用该可视化工具,以图形界面的形式操作数据库 在生产环境中,为了显示自己的逼格,一般不建议使用它 官网下载:https://www.navicat ...
- java基础 序列化反序列化流 实现Serializable 接口 自动装载序列号到对象文本文件如修改不能反序列化对象文本,除非自定义long型常量 打印流
package com.swift.baseKnowledge; import java.io.File; import java.io.FileInputStream; import java.io ...
- MBProgressHUD 优雅地去提示
项目主页: MBProgressHUD 实例下载: 点击下载 快速上手: 当执行需要较长时间的任务时,使用MBProgressHUD最重要的一点是: 保证主线程是空闲的,这样可以使UI实时更新.因此: ...
- 一次完整的http请求处理过程
一次完整的HTTP请求需要的7个步骤 HTTP通信机制是在一次完整的HTTP通信过程中,Web浏览器与Web服务器之间将完成下列7个步骤: 1:建立TCP连接 在HTTP工作开始之前,Web浏览器首先 ...
- CentOS7下安装FTP
1.安装vsftpd yum install -y vsftpd 2.设置 使用命令systemctl status vsftpd.service查看ftp状态. 开启ftp systemctl st ...