The ticket line

Serve the people in a ticket line in the order they joined.

What to build

A queue of names that follows a list of commands.

Requirements

  • join NAME adds a person at the rear of the line.
  • serve removes the person at the front and prints the name, or prints nobody when the line is empty.
  • After all the commands, print waiting N, where N is the number of people still in the line.

Input

The first line has q. Each of the next q lines has one command. A name has 1 to 15 lowercase letters.

Output

One line for each serve, then the waiting line.

Constraints

1 ≤ q ≤ 100.

Example

Input:

5
join rahim
join nadia
serve
join karim
serve

Output:

rahim
nadia
waiting 1

Why it works

A queue serves the person who has waited longest. With at most 100 commands the rear can never pass the end of a 100-place array, so no wrapping is needed here.

Examples

Example 1 · Input
5
join rahim
join nadia
serve
join karim
serve
Expected output
rahim
nadia
waiting 1
Example 2 · Input
2
serve
join sumi
Expected output
nobody
waiting 1

Hints

Hint 1

Keep the names in an array with two indexes: front for the next person to serve, and rear for the next free place.

Hint 2

The line is empty when front equals rear.

Solution

Show one correct solution (try it yourself first)
#include <stdio.h>
#include <string.h>

char names[100][16];
int front = 0, rear = 0;

int main(void) {
    int q;
    scanf("%d", &q);
    for (int i = 0; i < q; i++) {
        char cmd[10];
        scanf("%9s", cmd);
        if (strcmp(cmd, "join") == 0) {
            scanf("%15s", names[rear]);
            rear++;
        } else {
            if (front == rear) {
                printf("nobody\n");
            } else {
                printf("%s\n", names[front]);
                front++;
            }
        }
    }
    printf("waiting %d\n", rear - front);
    return 0;
}

Lesson: Queues →