Ticket Queue

Lab Java Intermediate 20 points

Simulate a queue with commands join NAME, serve (print the served name or empty) and peek (print who's next or empty). At the end print the remaining queue as a list.

Input

n, then n commands.

Output

Outputs, then the queue.

Example

Input

6
join ann
join bob
peek
serve
join cy
serve

Output

ann
ann
bob
[cy]
Sample input available Sample output available
Editor
Java

Hints

offer adds at the back, poll removes from the front (null when empty).

Solution explanation

poll/peek return null when empty; remove/element throw instead.

Show solution
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Queue<String> queue = new ArrayDeque<String>();
        for (int i = 0; i < n; i++) {
            String cmd = sc.next();
            if (cmd.equals("join")) {
                queue.offer(sc.next());
            } else if (cmd.equals("serve")) {
                String who = queue.poll();
                System.out.println(who == null ? "empty" : who);
            } else {
                String who = queue.peek();
                System.out.println(who == null ? "empty" : who);
            }
        }
        System.out.println(queue);
    }
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD