ZQUOJ-队列模拟

Description

Alice制作了一个"队列模拟系统",并定义了队列的操作命令。

以下是 队列 的几个命令以及其含义:

INIT         初始化队列
PRINT        输出一行:从头到尾输出队列每一个元素。元素之间用一个空格分隔
ENTER value  value进队
OUT          如果队列不空,则队首元素出队。如果队列是空的,则不做任何动作。
LENGTH       输出当前队列的元素个数

队列的每个元素都是字符型的。

Input

测试用例的第一行是一个整数 n ( 0 < n < 10000 ) ,表示这个测试用例有多少条命令。

接下来是n行,每行是一条命令。按顺序从头到尾执行每一条命令。

Output

对其中的PRINT和LENGTH命令,输出相应信息。

Sample Input

11
INIT
ENTER E
ENTER 5
ENTER t
LENGTH
ENTER 8
ENTER A
PRINT
OUT
LENGTH
PRINT

Sample Output

3
E 5 t 8 A
4

5 t 8 A

#include <bits/stdc++.h>
using namespace std;
typedef struct{
	char a[10006];
	int top;
	int last;
}node;
//node *L;
node *init()
{
	node *L=(node*)malloc(sizeof(node));
	L->top=L->last=0;
	return L;
}
void enter(node *L,char x)
{
	L->a[L->last]=x;
	L->last++;
}
void prin(node *L)
{
	for(int i=L->top;i<L->last-1;i++)
	  printf("%c ",L->a[i]);
	printf("%c",L->a[L->last-1]);
	printf("\n");
}
void out(node *L)
{
	if(L->top<L->last)
	  L->top++;
}
void length(node *L)
{
    printf("%d\n",L->last-L->top);	
}
int main()
{
	char com[10],ch;
	int t;
	node *L;
	scanf("%d",&t);
	getchar();
	while(t--)
	{
		scanf("%s",com);
		//gets(com);
		//getchar();
		if(com[0]=='I')  L=init();
		else if(com[0]=='E') {
			cin>>ch;
			//getchar();
			enter(L,ch);
		}
		else if(com[0]=='P') prin(L);
		else if(com[0]=='O') out(L);
		else if(com[0]=='L') length(L);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/honeycomb_1/article/details/80561636