zoukankan      html  css  js  c++  java
  • 面试题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;
    }

  • 相关阅读:
    读《疯狂Ajax讲义》重点
    Qt Library 链接库
    html验证码
    java开发webservice
    QT TCP/IP
    Android 开发技术流程
    jquery使用
    多台服务之间共享Session
    JS 混淆加密
    html中的表格 bootstrap-table
  • 原文地址:https://www.cnblogs.com/robbychan/p/3787143.html
Copyright © 2011-2022 走看看