-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_7.cpp
65 lines (56 loc) · 1.09 KB
/
3_7.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// 3_7.cpp
//
//
// Created by Pengyan Qin on 6/24/15.
//
//
#include <iostream>
#include <queue>
using namespace std;
// it is tricky to think of using separate queues for dog and cat
// and number the animals`
class shelter{
queue<int> cat, dog;
int order;
public:
shelter():order(0) {}
void enqueue(string s){
if(s == "cat")
cat.push(++order);
else{
dog.push(++order);
}
}
int dequeueDog(){
int i = dog.front();
dog.pop();
return i;
}
int dequeueCat(){
int i = cat.front();
cat.pop();
return i;
}
int dequeueAny(){
int tmp;
if(cat.front() < dog.front()){
tmp = cat.front();
cat.pop();
}
else{
tmp = dog.front();
dog.pop();
}
return tmp;
}
};
int main(){
shelter ms;
ms.enqueue("cat"); // 1
ms.enqueue("cat"); // 2
ms.enqueue("dog"); // 3
cout << ms.dequeueAny() << endl;
cout << ms.dequeueDog() << endl;
return 0;
}