题解#Head and Tail of the Queue#
Head and Tail of the Queue
https://www.nowcoder.com/practice/4acc8bb985b84b68b935ce26c8f4d1b9
import java.util.ArrayDeque; import java.util.Scanner; public class Main { public static void main(String[] args) { ArrayDeque deque = new ArrayDeque(); Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { String name = scanner.next(); // 初始化队列中的数据 deque.offerLast(name); } // write your code here...... Boolean flag = true; //表示奇偶,奇偶出队分别是首尾 while (!deque.isEmpty()) { //队列没有人为止 if (flag) { //弹队首 System.out.println(deque.getFirst()); deque.removeFirst(); flag = false; //交替 } else { //弹队尾 System.out.println(deque.getLast()); deque.removeLast(); flag = true; //交替 } } } }