//静态队列实现 #include#include #include 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);//Q->rear指向队尾元素 } //通过队列实现 //反转将head与tail换位 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 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++; } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)