1. 程式人生 > >指定棧的進棧順序,輸出所有可能的出棧順序

指定棧的進棧順序,輸出所有可能的出棧順序

public static void stackOut(int[] nums,int inNum,int outNum,LinkedList<Integer> stack,ArrayList<Integer> output)
	{
		if(inNum==nums.length&&outNum==nums.length)
		{
			System.out.println(output);
			num++;
		}
		else
		{
			LinkedList<Integer> stack1=(LinkedList<Integer>)stack.clone();
			LinkedList<Integer> stack2=(LinkedList<Integer>)stack.clone();
			
			if(inNum<nums.length)
			{
				stack1.push(nums[inNum]);
				stackOut(nums,inNum+1,outNum,stack1,output);
				stack1.pop();
			}
			
			
			ArrayList<Integer> output1=(ArrayList<Integer>)output.clone();
			if(inNum>outNum)
			{
				output1.add(stack2.pop());
				stackOut(nums,inNum,outNum+1,stack2,output1);
			}
		}
	}