Problem Description
想想双向链表……双向队列的定义差不多,也就是说一个队列的队尾同时也是队首;两头都可以做出队,入队的操作。
现在给你一系列的操作,请输出最后队列的状态;
命令格式:
LIN X X表示一个整数,命令代表左边进队操作;
RIN X 表示右边进队操作;
ROUT
LOUT 表示出队操作;
Input
第一行包含一个整数M(M<=10000),表示有M个操作;
以下M行每行包含一条命令;
命令可能不合法,对于不合法的命令,请在输出中处理;
Output
输出的第一行包含队列进行了M次操作后的状态,从左往右输出,每两个之间用空格隔开;
以下若干行处理不合法的命令(如果存在);
对于不合法的命令,请输出一行X ERROR
其中X表示是第几条命令;
Example Input
8
LIN 5
RIN 6
LIN 3
LOUT
ROUT
ROUT
ROUT
LIN 3
Example Output
3
7 ERROR
#include<stdio.h>
#include<string.h>
#define MAXSIZE 20000
typedef struct node
{
int data[MAXSIZE] ;
int front;
int rear;
}sq;
int pd[20000]={0};
void change (sq * l,char str[],int i)
{
if(strcmp(str,"LIN")==0)
{
if( (l->rear+1)%MAXSIZE==l->front)
{
pd[i]=1;
}
else
{
scanf("%d",&l->data[l->front]);
l->front=(l->front-1+MAXSIZE)%MAXSIZE ;
}
}
else if(strcmp(str,"RIN")==0)
{
if( (l->rear+1)%MAXSIZE==l->front)
{
pd[i]=1;
}
else
{
l->rear=(l->rear+1)%MAXSIZE ;
scanf("%d",&l->data[l->rear]);
}
}
else if(strcmp(str,"LOUT")==0)
{
if(l->rear==l->front)
{
pd[i]=1;
}
else
{
l->front=(l->front+1)%MAXSIZE ;
}
}
else if(strcmp(str,"ROUT")==0)
{
if(l->front==l->rear)
{
pd[i]=1;
}
else
{
l->rear=(l->rear-1+MAXSIZE)%MAXSIZE ;
}
}
}
void show1(sq * l)
{
int i;
i=(l->front+1)%MAXSIZE ;
for(;i!=(l->rear)%MAXSIZE;)
{
printf("%d ",l->data[i]);
i=(i+1)%MAXSIZE ;
}
printf("%d\n",l->data[i]);
}
void show2(int n)
{
int i;
for(i=0;i<n;i++)
{
if(pd[i])
{
printf("%d ERROR\n",i+1);
}
}
}
int main()
{
int i,n;
char str[6] ;
sq l;
l.front=l.rear=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s",str);
change(&l,str,i);
}
show1(&l);
show2(n);
return 0;
}