Result: Accepted
Time: 3ms
Memory: 1124kB
#include<stdio.h>
#include<string.h>
int main()
{
int n,m,d,f,j,p,i;
int a[10];
char s[20];
scanf("%d%d",&n,&m);
f=0;
j=0;
getchar();
while(m--)
{
gets(s);
if(s[4]=='L')
{
i=strlen(s)-1;
d=s[i]-'0';
i--;
while(s[i]!=' ')
{
d+=(s[i]-'0')*10;
i--;
}
}
else
{
i=strlen(s)-1;
d=s[i]-'0';
i--;
while(s[i]!=' ')
{
d+=(s[i]-'0')*10;
i--;
}
}
if(strncmp(s,"pushLeft",8)==0)
{
if(f<n)
{
for(p=j;p>0;--p)
a[p]=a[p-1];
a[0]=d;
j++;
printf("Pushed in left: %d\n",d);
f++;
}
else
printf("The queue is full\n");
}
else if(strncmp(s,"pushRight",9)==0)
{
if(f<n)
{
a[j]=d;
j++;
printf("Pushed in right: %d\n",d);
f++;
}
else
printf("The queue is full\n");
}
else if(strcmp(s,"popLeft")==0)
{
if(f>0)
{
printf("Popped from left: %d\n",a[0]);
for(p=0;p<j-1;++p)
a[p]=a[p+1];
j--;
f--;
}
else
printf("The queue is empty\n");
}
else if(strcmp(s,"popRight")==0)
{
if(f>0)
{
printf("Popped from right: %d\n",a[j-1]);
j--;
f--;
}
else
printf("The queue is empty\n");
}
}
return 0;
}