基本思想是:
第一個棧 爲數組頭,棧增長方向向上;
第二個棧,數組尾,棧增長方向向下,
第三個棧,在數組中間,棧增長方向向下;
第一個與第二個棧不用搬遷,因爲他們都在數組的端,第三個棧,需要搬遷,來滿足三個棧對空間的需求,
搬遷的策略:
採用慵懶的策略,只有三個棧中其中一個進行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;
}
};