• 《Cracking the Coding Interview》——第3章:栈和队列——题目7


    2014-03-19 03:20

    题目:实现一个包含阿猫阿狗的先入先出队列,我要猫就给我一只来的最早的猫,要狗就给我一只来的最早的狗,随便要就给我一只来的最早的宠物。建议用链表实现。

    解法:单链表可以实现一个简单的队列,这种有不同种类数据的队列,可以用if语句选择,也可以用一个堆做时间上的优化。对于来的时间,我用一个64位整数表示时间戳,在地球被太阳吃掉以前,这个数字是不大可能上溢的,所以问题应该不大。

    代码:

      1 // 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.
      2 #include <iostream>
      3 #include <string>
      4 using namespace std;
      5 
      6 template <class T>
      7 struct ListNode {
      8     T val;
      9     long long int id;
     10     ListNode *next;
     11     
     12     ListNode(T _val = 0, long long int _id = 0): val(_val), id(_id), next(nullptr) {};
     13 };
     14 
     15 template <class T>
     16 class CatsAndDogs {
     17 public:
     18     CatsAndDogs() {
     19         first_cat = last_cat =  first_dog = last_dog = nullptr;
     20         current_id = 0;
     21     }
     22     
     23     void enqueue(T val, int dog_or_cat) {
     24         switch (dog_or_cat) {
     25         case 0:
     26             // cat
     27             if (first_cat == nullptr) {
     28                 first_cat = last_cat = new ListNode<T>(val, current_id);
     29             } else {
     30                 last_cat->next = new ListNode<T>(val, current_id);
     31                 last_cat = last_cat->next;
     32             }
     33             break;
     34         case 1:
     35             // dog
     36             if (first_dog == nullptr) {
     37                 first_dog = last_dog = new ListNode<T>(val, current_id);
     38             } else {
     39                 last_dog->next = new ListNode<T>(val, current_id);
     40                 last_dog = last_dog->next;
     41             }
     42             break;
     43         }
     44         ++current_id;
     45     }
     46     
     47     T dequeueAny() {
     48         if (first_cat == nullptr) {
     49             return dequeueDog();
     50         } else if (first_dog == nullptr) {
     51             return dequeueCat();
     52         } else if (first_cat->id < first_dog->id) {
     53             return dequeueCat();
     54         } else {
     55             return dequeueDog();
     56         }
     57     }
     58     
     59     T dequeueCat() {
     60         T result;
     61         
     62         result = first_cat->val;
     63         if (first_cat == last_cat) {
     64             delete first_cat;
     65             first_cat = last_cat = nullptr;
     66         } else {
     67             ListNode<T> *ptr = first_cat;
     68             first_cat = first_cat->next;
     69             delete ptr;
     70         }
     71 
     72         return result;
     73     }
     74     
     75     T dequeueDog() {
     76         T result;
     77         
     78         result = first_dog->val;
     79         if (first_dog == last_dog) {
     80             delete first_dog;
     81             first_dog = last_dog = nullptr;
     82         } else {
     83             ListNode<T> *ptr = first_dog;
     84             first_dog = first_dog->next;
     85             delete ptr;
     86         }
     87 
     88         return result;
     89     }
     90 private:
     91     ListNode<T> *first_cat;
     92     ListNode<T> *first_dog;
     93     ListNode<T> *last_cat;
     94     ListNode<T> *last_dog;
     95     long long int current_id;
     96 };
     97 
     98 int main()
     99 {
    100     CatsAndDogs<string> cd;
    101     string val;
    102     string type;
    103     string cmd;
    104     
    105     while (cin >> cmd) {
    106         if (cmd == "end") {
    107             break;
    108         } else if (cmd == "in") {
    109             cin >> type;
    110             if (type == "cat") {
    111                 cin >> val;
    112                 cd.enqueue(val, 0);
    113             } else if (type == "dog") {
    114                 cin >> val;
    115                 cd.enqueue(val, 1);
    116             }
    117         } else if (cmd == "out") {
    118             cin >> type;
    119             if (type == "cat") {
    120                 cout << "cat=" << cd.dequeueCat() << endl;
    121             } else if (type == "dog") {
    122                 cout << "dog=" << cd.dequeueDog() << endl;
    123             } else if (type == "any") {
    124                 cout << "any=" << cd.dequeueAny() << endl;
    125             }
    126         }
    127     }
    128     
    129     return 0;
    130 }
  • 相关阅读:
    NOIP模拟测试7
    BigInt类
    bzoj 2733 [HNOI2012]永无乡 并查集+平衡树
    bzoj 2752 [HAOI2012]高速公路(road) 线段树
    bzoj 1584 Cleaning Up 打扫卫生 dp
    201709 半集训
    [SHOI2014]概率充电器 dp
    NOIP2016 天天爱跑步
    [HNOI2011] 数学作业
    [Poi2012]Festival
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3610467.html
Copyright © 2020-2023  润新知