设计了一个一个数组存储两个栈的功能,但是输出时的顺序不对,请大侠过目~
#include <iostream>
using namespace std;
template <class Elem>
class double_stack
{
private:
int maxsize;
int top1;
int top2;
Elem *listarray;
public:
double_stack(int size)
{
maxsize=size;
top1=-1;
top2=maxsize;
listarray= new Elem[maxsize];
}
bool push_top1(Elem number)
{
if (top1+(maxsize-top2)+1==maxsize)
{
cout<<"full!";
return false;
}
listarray[++top1]=number;
return true;
}
bool push_top2(Elem number)
{
if (top1+(maxsize-top2)+1==maxsize)
{
cout<<"full!";
return false;
}
listarray[--top2]=number;
return true;
}
Elem pop_top1()
{
if (top1==-1)
{
cout<<"empty!";
return Elem(-1);
}
else
{
return listarray[top1--];
}
}
Elem pop_top2()
{
if (top2==maxsize)
{
cout<<"empty!";
return Elem(-1);
}
else
return listarray[top2++];
}
};
int main()
{
double_stack<int> s(4);
s.push_top1(1);
s.push_top1(2);
s.push_top2(3);
s.push_top2(4);
cout<<s.pop_top1()<<","<<s.pop_top1()<<","<<s.pop_top2()<<","<<s.pop_top2();
system("pause");
return 0;
}
请问各位大侠在cout的时候,输出顺序不对,请问错哪了,谢谢