http://www.cnblogs.com/wanghui9072229/archive/2011/11/22/2259391.html
老题目了,4月份面腾讯实习的时候问了这道题,都不用写代码的。
1 #include <iostream> 2 #include <queue> 3 #include <climits> 4 #include <algorithm> 5 #include <memory.h> 6 #include <stdio.h> 7 #include <ostream> 8 #include <vector> 9 #include <list> 10 #include <cmath> 11 #include <string> 12 #include <stdexcept> 13 #include <stack> 14 using namespace std; 15 16 class MyQueue 17 { 18 stack<int> stackpush; 19 stack<int> stackpop; 20 public: 21 MyQueue() 22 { 23 24 } 25 ~MyQueue() 26 { 27 28 } 29 void enqueue(const int &elem) 30 { 31 stackpush(elem); 32 } 33 int dequeue() 34 { 35 if(stackpop.size() == 0) 36 { 37 if(stackpush.size() == 0) 38 throw std::out_of_range("queue is empty"); 39 else 40 { 41 while(!stackpush.empty()) 42 { 43 stackpop.push(stackpush.top()); 44 stackpush.pop(); 45 } 46 int val = stackpop.top(); 47 stackpop.pop(); 48 return val; 49 } 50 } 51 else 52 { 53 int val = stackpop.top(); 54 stackpop.pop(); 55 return val; 56 } 57 } 58 }; 59 60 int main() 61 { 62 63 return 0; 64 }