棧的壓入和彈出測試(測試隊列)

/**
 * 測試隊列入棧和出棧順序
 * @author Administrator
 *
 */
public class QueenTest {


public static boolean isPopOrder(int[] pushA,int[] popA) {
Stack<Integer> stack=new Stack<>();
int i=0;
for (Integer integer : pushA) {
stack.push(integer);
while (!stack.isEmpty()&&stack.peek()==popA[i]) {
stack.pop();
i++;
}
}
if (stack.isEmpty()) {
return true;
}
return false;
}
public static void main(String[] args) {
int[] pushA={1,2,3,4,5};
int[] pushB={3,5,4,2,1};
boolean b = QueenTest.isPopOrder(pushA, pushB);
System.out.println(b);
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章