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;
    }

  • 相关阅读:
    react 采坑记录
    理解JS 模块化
    MongoDB使用教程
    scss
    gulp 使用教程
    node.js 简单入门
    jQuery
    jQuery
    php+mysql+bootstrap 实现成绩管理系统
    SVN的commit功能用bat实现
  • 原文地址:https://www.cnblogs.com/robbychan/p/3787143.html
Copyright © 2011-2022 走看看