判断栈的压入,弹出序列是否匹配

public static void judgeSeq(int[] pushSeq,int[] dataSeq){
		if(pushSeq==null||dataSeq==null) return;
		int pushLength = pushSeq.length;
		int dataLength = dataSeq.length;
		if(pushLength==0||dataLength==0||pushLength!=dataLength){
			return;
		}
		Stack<Integer> stack = new Stack();
		int i=0,j=0;
		stack.push(pushSeq[i++]);
		while(j<dataLength&&i<pushLength){
			if(stack.empty()){
				stack.push(pushSeq[i++]);
			}
			if(dataSeq[j]==stack.peek()){
				stack.pop();
				j++;
			}
			else {
				while(i<pushLength&&pushSeq[i]!=dataSeq[j]){
					stack.push(pushSeq[i++]);
				}
				if(i==pushLength) break;
				i++;j++;
			}
		}
		if(i==pushLength&&j==dataLength){
			System.out.println(true);
		}
		else System.out.println(false);
		
		
		
}




   

你可能感兴趣的:(判断栈的压入,弹出序列是否匹配)