新的字符串拼接(OJ)
状态:runtime error
保存一下静态队列实现的代码,在Dev和ACwing都可以过
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct{
char ch[10000];
int front,rear;
}SqQueue;
void InitQueue(SqQueue *Q,char s[]){
strcpy(Q->ch,s);
Q->front=-1;
Q->rear=Q->front+strlen(s);
}
SqQueue Reverse(SqQueue Q){
SqQueue Q2; Q2.front=Q2.rear=-1;
while(Q.rear!=Q.front){
Q2.rear++;
Q2.ch[Q2.rear]=Q.ch[Q.rear];
Q.rear--;
}
return Q2;
}
SqQueue Insert_st(SqQueue *Q,char s){
SqQueue Q2; Q2.front=Q2.rear=-1;
Q2.rear++;
Q2.ch[Q2.rear]=s;
while(Q->front<Q->rear){
Q2.rear++;
Q->front++;
Q2.ch[Q2.rear]=Q->ch[Q->front];
}
return Q2;
}
void Insert_end(SqQueue *Q,char s){
Q->rear++;
Q->ch[Q->rear]=s;
}
int main()
{
SqQueue Q;
char s[10000];
scanf("%s",s);
InitQueue(&Q,s);
int n;
scanf("%d",&n);
while(n--){
int op;
scanf("%d",&op);
if(op == 1)
Q=Reverse(Q);
else if(op == 2){
int pos; char x;
scanf("%d",&pos);
getchar();
scanf("%c",&x);
if(pos == 1) Q=Insert_st(&Q,x);
else if(pos == 2) Insert_end(&Q,x);
}
}
Q.front++;
while(Q.front<=Q.rear){
printf("%c",Q.ch[Q.front]);
Q.front++;
}
}