/*
一个数组有两个栈来共享所以为共享栈。
所以有两个top,一个数组。
*/
#include <iostream>
using namespace std;
#define Maxsize 20
typedef int elemType;
class shareStack{
public:
elemType shareArray[Maxsize];
int top1;
int top2;
public:
/*init the shareStack*/
void initShareStack(){
top1=-1;
top2=Maxsize;
}
/*constructor*/
shareStack(){
initShareStack();
}
/*stack1 is full and stack2 is full .
the condition is same*/
bool isFull(){
if(top1+1==top2)
return true;
return false;
}
/*push the element into the stack ,flag=1,Ôòpush to stack1
else if flag=2,push to stack2
if the stack is full ,return false.
else return true;
*/
bool push(elemType element, int flag){
if(1==flag){
if(isFull()) return false;
top1++;
shareArray[top1]=element;
return true;
}
else if(2==flag){
if(isFull()) return false;
top2--;
shareArray[top2]=element;
return true;
}
}
/* judge the stack whether is empty.*/
bool isEmpty(int flag){
if(1==flag){
/*stack 1 is empty or not*/
if(-1==top1)
return true;
else return false;
}
else if(2==flag){
/*stack 2 is empty or not*/
if(Maxsize==top2)
return true;
else return false;
}
}
/* get the top element of stack flag*/
bool getTop(elemType& element,int flag){
if(1==flag){
if(isEmpty(1))
return false;
element=shareArray[top1];
return true;
}
else if(2==flag){
if(isEmpty(2))
return false;
element=shareArray[top2];
return true;
}
}
/*pop the top element*/
bool pop(int flag){
if(1==flag){
if(isEmpty(1))
return false;
top1--;
return true;
}
else if(2==flag){
if(isEmpty(2))
return false ;
top2++;
return true;
}
}
/* get the length of stack flag*/
int getSize(int flag){
if(1==flag){
return top1+1;
}
else if(2==flag){
return Maxsize-top2;
}
}
};
int main()
{
shareStack stack;
cout<<"push 3,5,4 into stack1 , push 8,9,7,6 into stack2"<<endl;
stack.push(3,1);
stack.push(5,1);
stack.push(4,1);
stack.push(8,2);
stack.push(9,2);
stack.push(7,2);
stack.push(6,2);
int size2,size1;
size2=stack.getSize(2);
size1=stack.getSize(1);
cout<<"the size1 is : "<<size1<<endl<<"the size2 is : "<<size2<<endl;
elemType element1,element2;
stack.getTop(element1,1);
stack.pop(2);
stack.getTop(element2,2);
cout<<"the top ele of stack1 is : "<<element1<<endl<<"pop ,and get top of stack2 "
<<element2<<endl;
size2=stack.getSize(2);
size1=stack.getSize(1);
cout<<"the size1 is : "<<size1<<endl<<"the size2 is : "<<size2<<endl;
cout << "Hello world!" << endl;
return 0;
}
一个数组有两个栈来共享所以为共享栈。
所以有两个top,一个数组。
*/
#include <iostream>
using namespace std;
#define Maxsize 20
typedef int elemType;
class shareStack{
public:
elemType shareArray[Maxsize];
int top1;
int top2;
public:
/*init the shareStack*/
void initShareStack(){
top1=-1;
top2=Maxsize;
}
/*constructor*/
shareStack(){
initShareStack();
}
/*stack1 is full and stack2 is full .
the condition is same*/
bool isFull(){
if(top1+1==top2)
return true;
return false;
}
/*push the element into the stack ,flag=1,Ôòpush to stack1
else if flag=2,push to stack2
if the stack is full ,return false.
else return true;
*/
bool push(elemType element, int flag){
if(1==flag){
if(isFull()) return false;
top1++;
shareArray[top1]=element;
return true;
}
else if(2==flag){
if(isFull()) return false;
top2--;
shareArray[top2]=element;
return true;
}
}
/* judge the stack whether is empty.*/
bool isEmpty(int flag){
if(1==flag){
/*stack 1 is empty or not*/
if(-1==top1)
return true;
else return false;
}
else if(2==flag){
/*stack 2 is empty or not*/
if(Maxsize==top2)
return true;
else return false;
}
}
/* get the top element of stack flag*/
bool getTop(elemType& element,int flag){
if(1==flag){
if(isEmpty(1))
return false;
element=shareArray[top1];
return true;
}
else if(2==flag){
if(isEmpty(2))
return false;
element=shareArray[top2];
return true;
}
}
/*pop the top element*/
bool pop(int flag){
if(1==flag){
if(isEmpty(1))
return false;
top1--;
return true;
}
else if(2==flag){
if(isEmpty(2))
return false ;
top2++;
return true;
}
}
/* get the length of stack flag*/
int getSize(int flag){
if(1==flag){
return top1+1;
}
else if(2==flag){
return Maxsize-top2;
}
}
};
int main()
{
shareStack stack;
cout<<"push 3,5,4 into stack1 , push 8,9,7,6 into stack2"<<endl;
stack.push(3,1);
stack.push(5,1);
stack.push(4,1);
stack.push(8,2);
stack.push(9,2);
stack.push(7,2);
stack.push(6,2);
int size2,size1;
size2=stack.getSize(2);
size1=stack.getSize(1);
cout<<"the size1 is : "<<size1<<endl<<"the size2 is : "<<size2<<endl;
elemType element1,element2;
stack.getTop(element1,1);
stack.pop(2);
stack.getTop(element2,2);
cout<<"the top ele of stack1 is : "<<element1<<endl<<"pop ,and get top of stack2 "
<<element2<<endl;
size2=stack.getSize(2);
size1=stack.getSize(1);
cout<<"the size1 is : "<<size1<<endl<<"the size2 is : "<<size2<<endl;
cout << "Hello world!" << endl;
return 0;
}