#include<iostream>
#include <deque>
using namespace std;
enum stackNumber
{
<span style="white-space:pre"> </span>one, two
};
template<typename T>
class dualStack
{
public:
<span style="white-space:pre"> </span>dualStack();
<span style="white-space:pre"> </span>void push(const T & item, stackNumber n );
<span style="white-space:pre"> </span>void pop(stackNumber n);
<span style="white-space:pre"> </span>const T & top(stackNumber n) const;
<span style="white-space:pre"> </span>bool empty(stackNumber n) const;
<span style="white-space:pre"> </span>int size(stackNumber n) const;
private:
<span style="white-space:pre"> </span>deque<T> dualStackElements;
<span style="white-space:pre"> </span>int count1, count2;
};
template<typename T>
dualStack<T>::dualStack(){ count1 = 0; count2 = 0; }
template<typename T>
void dualStack<T>::push(const T & item, stackNumber n)
{
<span style="white-space:pre"> </span>if (n == one)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>dualStackElements.push_front(item);
<span style="white-space:pre"> </span>count1++;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if (n == two)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>dualStackElements.push_back(item);
<span style="white-space:pre"> </span>count2++;
<span style="white-space:pre"> </span>}
}
template<typename T>
void dualStack<T>::pop(stackNumber n)
{
<span style="white-space:pre"> </span>if (n == one)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>if (count1 == 0)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>cout << "栈已空\n";
<span style="white-space:pre"> </span>return;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>dualStackElements.pop_front();
<span style="white-space:pre"> </span>count1--;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if (n == two)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>if (count2 == 0)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>cout << "栈已空\n";
<span style="white-space:pre"> </span>return;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>dualStackElements.pop_back();
<span style="white-space:pre"> </span>count2--;
<span style="white-space:pre"> </span>}
}
template<typename T>
const T & dualStack<T>::top(stackNumber n) const
{
<span style="white-space:pre"> </span>const T temp = NULL;
<span style="white-space:pre"> </span>if (n == one)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>if (this->empty(one))
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>cout << "栈已空";
<span style="white-space:pre"> </span>return temp;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>return dualStackElements.front();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if (n == two)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>if (this->empty(two))
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>cout << "栈已空";
<span style="white-space:pre"> </span>return temp;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>return dualStackElements.back();
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
}
template<typename T>
bool dualStack<T>::empty(stackNumber n) const
{
<span style="white-space:pre"> </span>if (n == one)
<span style="white-space:pre"> </span>return count1==0;
<span style="white-space:pre"> </span>if (n == two)
<span style="white-space:pre"> </span>return count2==0;
}
template<typename T>
int dualStack<T>::size(stackNumber n) const
{
<span style="white-space:pre"> </span>if (n == one)
<span style="white-space:pre"> </span>return count1;
<span style="white-space:pre"> </span>if (n == two)
<span style="white-space:pre"> </span>return count2;
}<span style="font-family:Arial, Helvetica, sans-serif;"><span style="white-space: normal;">
</span></span>