面试题13 用两个栈实现队列 【栈】

#include <iostream>
#include <string>
#include <stack>
#include <queue>
#include <algorithm>
#define BUG cout << "here\n";
using namespace std;
const int N = 105;
struct Node {
    int value;
    Node* lchild;
    Node* rchild;
};
template<typename T> class CQueue {
    public :
        CQueue(void);
        ~CQueue(void);
        void appendTail(const T& Node);
        T deleteHead();
    private :
        stack<T> stack1;
        stack<T> stack2;
};
template<typename T> void CQueue<T>::appendTail(const T& element) {
    stack1.push(element);
}
template<typename T> T CQueue<T>::deleteHead() {
    if(!stack2.empty()) {
        T tmp = stack2.top();
        stack2.pop();
        return tmp;
    }
    else {
        if(stack1.empty()) {
            cout << "异常" << endl;
        }
        while(!stack1.empty()) { // 我感觉这么写更快呢!
            T tmp = stack1.top();
            stack1.pop();
            stack2.push(tmp);
        }
        T tmp = stack2.top();
        stack2.pop();
        return tmp;
    }
}
int main() {
    return 0;
}

原文地址:https://www.cnblogs.com/robbychan/p/3787143.html