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
| #include "stdafx.h" #include <iostream> #include <stack>
using namespace std;
template<typename T> class Queue { stack<T> s1,s2; public: void push(T e) { s1.push(e); }
T pop() { while(s1.size()>1) { s2.push(s1.top()); s1.pop(); } T e = s1.top(); s1.pop(); while(!s2.empty()) { s1.push(s2.top()); s2.pop(); } return e; }
bool empty() { return s1.empty(); } };
int main() { #ifdef LOCAL freopen("d:\\data.in", "r", stdin); #endif Queue<int> q; q.push(1); q.push(2); q.push(3); while(!q.empty()) { cout << q.pop(); } return 0; }
|