STACK 非标准版

来源:互联网 发布:php soapclient 超时 编辑:程序博客网 时间:2024/04/29 21:37
 #include <iostream>using namespace std; #include<queue> //使用两个队列实现一个栈template<class T>class Stack{public:void Push(const T& d){if (!q1.empty()) q1.push(d);else                    q2.push(d);}void Pop(){if (!q1.empty())   _Pop(q1, q2);       else               _Pop(q2, q1);}  bool Empty(){if (q1.empty() && q2.empty()) return true;     else                       return false;}T& Top(){if (!q1.empty()) return q1.back();    else        return q2.back();}size_t Size(){if (!q1.empty())  return q1.size();    else        return q2.size();}protected:void _Pop(queue<T>& Q1, queue<T>& Q2){ while (Q1.size()>1){T& data = Q1.front();Q2.push(data);Q1.pop();}Q1.pop();}protected:queue<T>  q1 , q2;};int main(){cout << "Hello,C++ world of AnycodeX!" << endl;return 0;}

0 0
原创粉丝点击