1,使用两个栈模拟出队列
package com.tulun.src09;
import java.util.Stack;
/**
- @author Richard
- @date 2019/9/16 0016-20:36
*/
//使用两个栈模拟出队列
public class myStack{
}
class TestDemo{
public static void main(String[] args) {
//Stack stack = new Stack<>();
//stack.peek();
Integer[] arr={1,2,3,4,5,6};
twoStackToOneQueue(arr,new Stack(),new Stack());
}
public static void twoStackToOneQueue(T[] arr, Stack stack1,Stack stack2){
for (int i=0;i<arr.length;i++){
stack1.push(arr[i]);//数组中的元素依次进入第一个栈
}
while (!stack1.isEmpty()){
stack2.push(stack1.peek());//将第一个栈中的元素依次出栈,再依次进入第二个栈
stack1.pop();
}
while (!stack2.isEmpty()){
System.out.print(stack2.peek()+" ");//将第二个栈中的元素依次出栈
stack2.pop();
}