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的更多相关文章

  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. kubernetes-控制器statefulset和Job(十三)

       从前面的学习我们知道使用Deployment创建的pod是无状态的,当挂载了Volume之后,如果该pod挂了,Replication Controller会再启动一个pod来保证可用性,但是由 ...

  2. Java面试不得不知的程序(二)

    [程序1]   题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 斐波那契数列:前面相邻两项之和,构 ...

  3. XML DTD约束 对xml文件的crud的查询Read Retrieve操作 xml递归遍历

    本地的dtd文档 xml中引入dtd文档 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE 书 ...

  4. BZOJ4008: [HNOI2015]亚瑟王(期望dp)

    Time Limit: 20 Sec  Memory Limit: 512 MBSec  Special JudgeSubmit: 1952  Solved: 1159[Submit][Status] ...

  5. Oracle字符集的查看查询和Oracle字符集的设置修改(转载)

    本文主要讨论以下几个部分:如何查看查询oracle字符集. 修改设置字符集以及常见的Oracle UTF8字符集和Oracle exp 字符集问题. 一.什么是Oracle字符集 Oracle字符集是 ...

  6. linux环境下安装 openOffice 并启动服务

    一.背景故事 这两天遇到一个大坑,客户要做office 文档在线预览功能,于是乎就要把office文档转换成pdf交给前端显示.      在某度找了一圈都说openOffice+jodconvert ...

  7. java从图片中识别文字

    package com.dream.common; import java.awt.image.BufferedImage; import java.io.File; import java.io.I ...

  8. php-5.6.26源代码 - opcode列表

    文件 php-5.6.26/Zend/zend_vm_opcodes.h #ifndef ZEND_VM_OPCODES_H #define ZEND_VM_OPCODES_H BEGIN_EXTER ...

  9. 2.1 <script>元素【JavaScript高级程序设计第三版】

    向 HTML 页面中插入 JavaScript 的主要方法,就是使用<script>元素.这个元素由 Netscape 创造并在 Netscape Navigator 2 中首先实现.后来 ...

  10. Codeforces Round #460 (Div. 2): D. Substring(DAG+DP+判环)

    D. Substring time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...