栈底分别在数组的两端,规定数组大小为128,int类型。
可以实现的操作为验空、验满、分别向两个栈添加元素、分别删除栈顶元素、分别展示栈内所有元素。
代码如下:
#include<iostream>
using namespace std;
typedef int StackElement;
const int STACK_CAPACITY = 128;
class Stack
{
public:
Stack();
int size1()const;
int size2()const;
bool empty1()const;
bool empty2()const;
bool full()const;
void push1(const StackElement & item);
void push2(const StackElement & item);
void display1();
void display2();
void pop1();
void pop2();
int capacity()const;
StackElement top1()const;
StackElement top2()const;
private:
StackElement myArray[STACK_CAPACITY];
int myTop1;
int myTop2;
};
Stack::Stack():myTop1(-1),myTop2(-1)
{}
//-----------------------------------------------
bool Stack::empty1()const
{
return (myTop1==-1);
}
//-----------------------------------------------
bool Stack::empty2()const
{
return (myTop2==-1);
}
//-----------------------------------------------
int Stack::size1()const
{
return myTop1;
}
//-----------------------------------------------
int Stack::size2()const
{
return myTop2;
}
//-----------------------------------------------
int Stack::capacity()const
{
return STACK_CAPACITY;
}
//-----------------------------------------------
void Stack::push1(const StackElement & item)
{
if(myTop1+myTop2>=capacity()-2)
{
cerr<<"***Full Full***"<<endl;
exit(1);
}
if(myTop1<STACK_CAPACITY-1)
{
++myTop1;
myArray[myTop1]=item;
}
else
{
cerr<<"***Error Error***"<<endl;
exit(1);
}
}
//-----------------------------------------------
void Stack::push2(const StackElement & item)
{
if(myTop1+myTop2>=capacity()-2)
{
cerr<<"***Full Full***"<<endl;
exit(1);
}
if(myTop2<STACK_CAPACITY-1)
{
++myTop2;
myArray[STACK_CAPACITY-1-myTop2]=item;
}
else
{
cerr<<"***Error Error***"<<endl;
exit(1);
}
}
//-----------------------------------------------
void Stack::display1()
{
for(int i = myTop1;i>=0;i--)
cout<<myArray[i]<<" ";
cout<<endl;
}
//-----------------------------------------------
void Stack::display2()
{
for(int i = myTop2;i>=0;i--)
cout<<myArray[STACK_CAPACITY-1-i]<<" ";
cout<<endl;
}
//-----------------------------------------------
void Stack::pop1()
{
if(!empty1())
myTop1--;
else
cerr<<"***no element no element***"<<endl;
}
//-----------------------------------------------
void Stack::pop2()
{
if(!empty2())
myTop2--;
else
cerr<<"***no element no element***"<<endl;
}
//-----------------------------------------------
StackElement Stack::top1()const
{
if(!empty1())
return (myArray[myTop1]);
else
{
cerr<<"***Stack is empty -- returning gabage value***"<<endl;
StackElement garbage;
return garbage;
}
}
//-----------------------------------------------
StackElement Stack::top2()const
{
if(!empty2())
return (myArray[STACK_CAPACITY-1-myTop2]);
else
{
cerr<<"***Stack is empty -- returning gabage value***"<<endl;
StackElement garbage;
return garbage;
}
}
//-----------------------------------------------
bool Stack::full()const
{
return(capacity()<=myTop1+myTop2+2);
}
//-----------------------------------------------
int main()
{
Stack s;
cout<<"Two Stack created.Stack 1 Empty?"<<boolalpha<<s.empty1()<<endl;
cout<<"Stack 2 Empty?"<<boolalpha<<s.empty2()<<endl;
cout<<"please input a number to fill in Stack 1 :";
int numItems1;
cin>>numItems1;
for(int i=1;i<=numItems1;i++)
s.push1(i);
cout<<"please input a number to fill in Stack 2 :";
int numItems2;
cin>>numItems2;
for(int i=1;i<=numItems2;i++)
s.push2(i);
cout<<"Stack 1 contents : "<<endl;
s.display1();
cout<<"Stack 2 contents : "<<endl;
s.display2();
cout<<"Stack 1 empty ? "<<boolalpha<<s.empty1()<<endl;
cout<<"Stack 2 empty ? "<<boolalpha<<s.empty2()<<endl;
cout<<"Stack 1 's Top value is : "<<s.top1()<<endl;
cout<<"Stack 2 's Top value is : "<<s.top2()<<endl;
cout<<endl;
cout<<"The all Stack is full ? "<<s.full()<<endl;
s.pop1();
cout<<"delete Stack 1 a value,then the top value is : "<<s.top1()<<endl;
s.pop2();
cout<<"delete Stack 2 a value,then the top value is : "<<s.top2()<<endl;
}