【14】Use a single array to implement three stacks

Question:Describe how you could use a single array to implement three stacks.    

package CareerCup;

public class OneArrayThreeStack 
{
	int[] array;
	int top1;
	int top2;
	int top3Left;
	int top3Right;
	boolean isLeft;
	
	
	public OneArrayThreeStack(int size)
	{
		array = new int[size];
		top1 = 0;
		top2 = size-1;
		top3Left = size/2;
		top3Right = top3Left+1;
		isLeft = true;
	}

	public void push(int data,int type) throws Exception
	{
		if(type == 1)
		{
			if(top1==top3Left+1)
				throw new Exception("The stack1 is full!");
			
			array[top1] = data;
			top1++;
		}
		else if(type == 2)
		{
			if(top2==top3Right-1)
				throw new Exception("The stack2 is full!");
			
			array[top2] = data;
			top2--;
		}
		else if(type == 3)
		{
			if(isLeft)
			{
				if(top1-1==top3Left)
					throw new Exception("The stack3 is full!");
				
				array[top3Left] = data;
				top3Left--;
			}
			else
			{
				if(top2+1==top3Right)
					throw new Exception("The stack3 is full!");
				
				array[top3Right] = data;
				top3Right++;
			}
			
			isLeft = !isLeft;
		}
	}
	
	public int pop(int type) throws Exception
	{
		int res=-1;
		if(type == 1)
		{
			if(top1==0)
				throw new Exception("The stack1 is empty!");
			
			res = array[--top1];
			array[top1] = 0;
		}
		else if(type == 2)
		{
			if(top2==array.length-1)
				throw new Exception("The stack2 is empty!");
			res = array[++top2];
			array[top2] = 0;
		}
		else if(type == 3)
		{
			if(top3Right == top3Left+1)
				throw new Exception("The stack3 is empty!");
			if(isLeft)
			{
				res = array[--top3Right];
				array[top3Right] = 0;
			}
			else
			{
				res = array[++top3Left];
				array[top3Left] = 0;
			}
		}
		return res;
	}
	
	public void print()
	{
		System.out.print("Array:{");
		for(int i=0;i<array.length;i++)
			System.out.print(array[i]);
		System.out.print("}");
		System.out.println();
	}
	
	public void printValue()
	{
		System.out.println(top1+"\t"+top2+"\t"+top3Left+"\t"+top3Right+"\t"+isLeft);
	}
	
	public static void main(String[] args) throws Exception
	{
		int len = 9;
		OneArrayThreeStack ats = new OneArrayThreeStack(len);
		ats.push(1, 1);
		ats.printValue();
		ats.push(2, 2);
		ats.printValue();
		ats.push(3, 3);
		ats.printValue();
		ats.print();
		ats.push(1, 1);
		ats.printValue();
		ats.push(2, 2);
		ats.printValue();
		ats.push(3, 3);
		ats.printValue();
		ats.push(1, 1);
		ats.print();
		ats.printValue();
		ats.push(2, 2);
		ats.print();
		ats.printValue();
		ats.push(3, 3);
		ats.printValue();
		ats.print();
		ats.print();
		
		ats.pop(1);
		ats.printValue();
		ats.pop(2);
		ats.printValue();
		ats.pop(3);
		ats.printValue();
		ats.print();
		ats.pop(1);
		ats.printValue();
		ats.pop(2);
		ats.printValue();
		ats.pop(3);
		ats.printValue();
		ats.print();
		ats.pop(1);
		ats.printValue();
		ats.pop(2);
		ats.printValue();
		ats.pop(3);
		ats.printValue();
		ats.print();
		ats.print();
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值