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