Description
一个初始化为空的队列,共有三种操作,pop,push,query
Input
pop操作, 输入pop, 输出并且删除队首的元素,如果没有则输出 empty!;
push操作,输入push d(d为一个整数);
query操作,输入query k(k为一个整数),输出队列中第k个元素,如果没有则输出 empty!.
Output
按要求输出
Sample Input
pop push 1 pop push 10 query 1
Sample Output
empty!
1
10
#include<stdio.h> #include<string.h> int queue[10000], front = 0, rear = 0; void pop ( void ) { if ( front == rear ) { printf ( "empty!\n" ); return ; } printf ( "%d\n", queue[front++] ); } void query ( void ) { int n; scanf ( "%d", &n ); if ( n + front > rear ) { printf ( "empty!\n" ); return ; } printf ( "%d\n", queue[front + n-1] ); } void push() { int n; scanf( "%d", &n ); queue[rear] = n; rear++; } int main ( ) { char str[10]; while ( scanf ( "%s", str ) == 1 ) { if ( !strcmp ( "pop", str ) ) pop ( ); if ( !strcmp ( "query", str ) ) query ( ); if ( !strcmp ( "push", str ) ) push ( ); } return 0; }