一个数组实现三个栈

基本思想是:

第一个栈 为数组头,栈增长方向向上;

第二个栈,数组尾,栈增长方向向下,

第三个栈,在数组中间,栈增长方向向下;

第一个与第二个栈不用搬迁,因为他们都在数组的端,第三个栈,需要搬迁,来满足三个栈对空间的需求,

搬迁的策略:

采用慵懒的策略,只有三个栈中其中一个进行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;
	}
	
};


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值