#include<stdio.h>
#include <stdlib.h>
#define MaxQSize 7
typedef int ElemType;
typedef struct{
ElemType *base;
int rear;
int length;
}Queue;
Queue InitQueue(){
Queue Q;
Q.base=(ElemType*)malloc(MaxQSize*sizeof(ElemType));
Q.rear=0;
Q.length=0;
return Q;
}
void EnQueue(Queue &Q,ElemType x){
if(Q.length==MaxQSize){
printf("队满");
exit(0);
}
Q.base[Q.rear]=x;
Q.rear=(Q.rear+1)%MaxQSize;
Q.length++;
}
ElemType DeQueue(Queue &Q){
ElemType x;
if(Q.length==0) {
printf("队空");
exit(0);
}
x=Q.base[(Q.rear+MaxQSize-Q.length+1)%MaxQSize];
Q.length--;
return x;
}
int main(){
Queue Q=InitQueue();
char c[20];
int i,j;
printf("输入字符:\n");
scanf("%s",c);
for(i=0;c[i]!='\0';i++){
EnQueue(Q,c[i]);
}
printf("%c ",DeQueue(Q));
printf("%c ",DeQueue(Q));
printf("%c ",DeQueue(Q));
printf("%c ",DeQueue(Q));
return 0;
}
调试:
输入字符:
acbdefg
a c b d