【16】Set of stacks

Qustion:Imagine a (literal) stack of plates. If the stack gets too high, it might topple. There-fore, in real life, we would likely start a new stack when the previous stack exceedssome threshold. Implement a data structure SetOfStacks that mimics this. SetOf-Stacks should be composed of several stacks, and should create a new stack oncethe previous one exceeds capacity. SetOfStacks.push() and SetOfStacks.pop() shouldbehave identically to a single stack (that is, pop() should return the same values as itwould if there were just a single stack).

FOLLOW UP Implement a function popAt(int index) which performs a pop operation on a specificsub-stack. 

思路:1.用一个数组存每个stack。假设除了最后一个堆栈之外,所有的堆栈必须满栈。

    2.每次push的时候都是取stack数组中最后一个push,如果最后一个堆栈已满,则新建一个堆栈push。

    3.每次pop的时候也是对最后一个堆栈pop,如果pop后该堆栈为空,则在stack数组中移除该堆栈。

    4.每次popAt时,除对制定的堆栈pop外,如果不是最后一个则要将其之后的堆栈push栈底元素补充,使得满足1中假设。例如:如果从堆栈1中弹出一个元素,那么需要将堆栈2底部的元素压到堆栈1的顶端。堆栈3的元素要到堆栈2....


1.有大小限制的堆栈

package CareerCup;

public class CapacityStack 
{
	private int capacity;
	private int size;
	private Node top;
	private Node bottom;

	public CapacityStack(int capacity)
	{
		this.capacity = capacity;
		this.size = 0;
		top = null;
		bottom = null;
	}
	
	public boolean isFull()
	{
		return this.capacity==this.size;
	}
	
	public boolean isEmpty()
	{
		return this.size == 0;
	}
	
	public void push(int data) throws Exception
	{
		if(size >= capacity)
			throw new Exception("The stack is full!");
		size++;
		Node node = new Node(data);
		if(size == 1) bottom = node;
		node.next = top;
		top = node;
	}
	
	public int pop()
	{
		if(top!=null)
		{
			if(bottom == top) bottom = null; //当堆栈只有一个元素时,pop()后应将bottom置null
			Node node = top;
			top = top.next;
			size--;
			return node.data;
		}
		return Integer.MIN_VALUE;
	}
	
	public int removeBottom() throws Exception
	{
		if(bottom == null)
			throw new Exception("The stack is empty!");
		else if(bottom == top)
		{
			Node node = bottom;
			bottom = null;
			top = null;
			size--;
			return node.data;
			
		}
		else
		{
			Node node = top;
			Node nodePre = top;
			while(node!=bottom)
			{
				nodePre = node;
				node = node.next;
			}
			bottom = nodePre;
			bottom.next = null;
			size--;
			return node.data;
		}
	}
	
	/*从top到bottom顺序打印*/
	public void print()
	{
		Node node = top;
		while(node!=null)
		{
			System.out.println(node.data);
			if(node!=bottom)
			System.out.println("|");
			node = node.next;
		}
		
	}
}

2.set of stacks

package CareerCup;

import java.util.ArrayList;

public class SetOfStacks 
{
	private ArrayList<CapacityStack> stacks = new ArrayList<CapacityStack>();
	private int capacity;
	public SetOfStacks(int cap)
	{
		capacity = cap;
	}
	
	public CapacityStack getLastStack()
	{
		if(stacks.size()==0) return null;
		return stacks.get(stacks.size()-1);
	}
	
	public void push(int data) throws Exception
	{
		CapacityStack last = getLastStack();
		if(last!=null && !last.isFull())
			last.push(data);
		else
		{
			CapacityStack stack = new CapacityStack(this.capacity);
			stack.push(data);
			stacks.add(stack);
		}
	}
	
	public int pop()
	{
		CapacityStack last = getLastStack();
		int res = last.pop();
		if(last.isEmpty()) stacks.remove(stacks.size()-1);
		return res;
	}
	
	public int popAt(int index) throws Exception
	{
		return leftShift(index-1,true);
	}
	
	public int leftShift(int index, boolean flag) throws Exception
	{
		CapacityStack stack = stacks.get(index);
		int res = Integer.MIN_VALUE;
		
		if(flag) res = stack.pop();
		else res = stack.removeBottom();
		
		if(stack.isEmpty())
			stacks.remove(index);
		else if(index+1 < stacks.size())
		{
			int resRightBottom = leftShift(index+1,false);
			stack.push(resRightBottom);
			System.out.println("index:"+index+"\t"+"resRightBottom:"+resRightBottom);
		}
		return res;
	}
	
	public void print()
	{
		for(int i=0;i<stacks.size();i++)
		{
			System.out.println("The stack "+i+":");
			CapacityStack stack = stacks.get(i);
			stack.print();
		}
	}
	
	public static void main(String[] args) throws Exception
	{
		int capacity = 3;
		SetOfStacks ss = new SetOfStacks(capacity);
		
		for(int i=1;i<=9;i++)
		{
			ss.push(i);
		}
		
		ss.print();
		ss.pop();
		ss.print();
		ss.popAt(2);
		ss.print();
	}
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值