基本思想是:
第一个栈 为数组头,栈增长方向向上;
第二个栈,数组尾,栈增长方向向下,
第三个栈,在数组中间,栈增长方向向下;
第一个与第二个栈不用搬迁,因为他们都在数组的端,第三个栈,需要搬迁,来满足三个栈对空间的需求,
搬迁的策略:
采用慵懒的策略,只有三个栈中其中一个进行push操作时,如果没有足够的空间,才搬迁第三个栈;
代码:
class threeStackUsingOneArray
{
private :
const static int length=5;
int intArray[length];
int stackOneLocation;
int stackTwoLocation;
int stackThreeLocation;
int stackThreeEndLocation;
public:
static int state; //记录状态 0(正常),-1(push 栈没有多余的空间),-2(pop 栈中没有元素)
threeStackUsingOneArray()
{
stackOneLocation=0;
stackTwoLocation=length-1;
stackThreeLocation=(stackOneLocation+stackTwoLocation)/2;
stackThreeEndLocation=stackThreeLocation;
}
void print()
{
cout<<endl;
for(int i=0;i<length;i++)
cout<<intArray[i]<<" ";
cout<<endl;
}
/*
push 操作,
input:
stachNum 栈的编号。
value: 值;
*/
int push(int stackNum,int value)
{
int freeSpace;
state=0;
if(1==stackNum)
{
if(stackOneLocation<=stackThreeLocation)
{
intArray[stackOneLocation]=value;
stackOneLocation++;
}
else //栈没有空间,需要调整中间栈的位置
{
freeSpace=(stackTwoLocation-stackOneLocation+1)+(stackThreeLocation-stackThreeEndLocation);
if(freeSpace<=0)
state=-1;
else if(freeSpace==1)
adjust(&intArray[stackThreeLocation],1);
else
adjust(&intArray[stackThreeLocation],freeSpace/2);
intArray[stackOneLocation]=value;
stackOneLocation++;
}
}
else if(2==stackNum)
{
if(stackTwoLocation>=stackThreeEndLocation)
{
intArray[stackTwoLocation]=value;
stackTwoLocation--;
}
else
{
freeSpace=(stackTwoLocation-stackOneLocation+1)+(stackThreeLocation-stackThreeEndLocation);
if(freeSpace<=0)
state=-1;
else if(freeSpace==1)
adjust(&intArray[stackThreeLocation],-1);
else
adjust(&intArray[stackThreeLocation],-freeSpace/2);
intArray[stackTwoLocation]=value;
stackTwoLocation--;
}
}
else if(3==stackNum)
{
if(stackThreeLocation>=stackOneLocation)
{
intArray[stackThreeLocation]=value;
stackThreeLocation--;
}
else
{
freeSpace=(stackTwoLocation-stackOneLocation+1)+(stackThreeLocation-stackThreeEndLocation);
if(freeSpace<=0)
state=-1;
else if(freeSpace==1)
adjust(&intArray[stackThreeLocation],1);
else
adjust(&intArray[stackThreeLocation],freeSpace/2);
intArray[stackOneLocation]=value;
stackThreeLocation--;
}
}
return state;
}
int pop(int stackNum)
{
int value=0;
state=0;
if(1==stackNum)
{
cout<<" pop 1 :"<< stackOneLocation<<" ";
if(stackOneLocation>0)
value=intArray[--stackOneLocation];
else
{
state=-2; //站内没有元素
}
}
else if(2==stackNum)
{
if(stackTwoLocation<length-1)
{
value=intArray[++stackTwoLocation];
}
else
{
state=-2; //站内没有元素
}
}
else if(3==stackNum)
{
if((stackThreeLocation - stackThreeEndLocation)>0)
{
value=intArray[++stackThreeLocation];
}
else
{
state=-2; //栈内没有元素
}
}
return value;
}
/*
调整中间那个栈位置,让正在操作的栈有空间
*/
private :
void adjust(int *des,int off)
{
int endLocation=stackThreeEndLocation;
int beginLocation=stackThreeLocation;
if(off>0)
{
for(int location=stackThreeEndLocation+off;endLocation>=stackThreeLocation;endLocation--)
{
intArray[location--]=intArray[endLocation--];
}
}
else
{
for(int location=stackThreeLocation+off;beginLocation>=stackThreeEndLocation;beginLocation++)
{
intArray[location++]=intArray[beginLocation++];
}
}
stackThreeEndLocation +=off;
stackThreeLocation +=off;
}
};