SDUT 2335 - 数据结构实验之队列一:排队买饭

时间:2021-07-08 10:40:41

传送门SDUT 2335 - 数据结构实验之队列一:排队买饭

队列基础问题.

#include <cstdio>
#include <cstring>
using namespace std;
int queue[30000];
int main()
{
    freopen("input.txt", "r", stdin);
    int m, n;
    int z;
    int i;
    char temp[10];
    int front, rear;
    scanf("%d", &m);
    for (i = 1; i <= m; i++)
        scanf("%d", &queue[i]);
    front = 1;
    rear = m + 1;
    scanf("%d", &n);
    for (i = 0; i < n; i++)
    {
        scanf("%s", temp);
        if (strcmp(temp, "JOIN") == 0)
            scanf("%d", &queue[rear++]);
        else if (strcmp(temp, "ASK") == 0)
        {
            scanf("%d", &z);
            printf("%d\n", queue[front + z - 1]);
        }
        else if (strcmp(temp, "LEAVE") == 0)
        {
            scanf("%d", &z);
            for (int k = front - 1 + z; k < rear  - 1; k++)
            {
                queue[k] = queue[k + 1];
            }
            rear--;
        }
        else if (strcmp(temp, "FINISH") == 0)
        {
            scanf("%d", &z);
            front += z;
        }
        else if (strcmp(temp, "LENGTH") == 0)
        {
            printf("%d\n", rear - front);
        }
    }
    return 0;
}