循环队列想要插入一个元素结果变成了一个新的队列。为什么呀,求大神解答谢谢。
#include <stdio.h>#include <stdlib.h>
#define MAXSIZE 10
#define OK 1
#define NO 2
typedef int Status;
typedef char ElemType;
typedef struct Queue
{
ElemType *base;//队列基址
int front;
int rear;
}Queue;
Status InitQueue(Queue *q)//初始化一个空队列
{
q->base=(ElemType*)malloc(MAXSIZE*sizeof(ElemType));
q->front=q->rear=0;
return OK;
}
void EnQueue(Queue *q,ElemType e)//插入元素e为新的队尾元素
{
if(((q->rear+1)%MAXSIZE)==q->front)
{
printf("\nThe queue is full!\n");
}
q->base[q->rear]=e;
q->rear=(q->rear+1)% MAXSIZE;
}
Status DeQueue(Queue *q,ElemType *e)//删除队头元素用e返回其值
{
if(q->front!=q->rear)
{
q->front=(q->front+1)%MAXSIZE;
*e=q->base[q->front];
return OK;
}
else
return NO;
}
void Print(Queue *q)//打印整个队列
{
if(((q->rear+1)%MAXSIZE)==q->front)
{
printf("\nThe queue is full!\n");
}
while(q->front!=q->rear)
{
printf("%c",q->base[q->front]);
q->front=(q->front+1)% MAXSIZE;
}
}
Status DestroyQueue(Queue *q)//删除队列
{
if (q == NULL)
return NO;
if (q->base)
{
free(q->base);
}
printf("The queue has been destroyed\n");
return OK;
}
int main()
{
Queue q;
ElemType e,a;
InitQueue(&q);
printf("Enter some characters:");
e=getchar();
while(e!='\n')
{
EnQueue(&q,e);
e=getchar();
}
printf("Print this queue:");
Print(&q);
printf("\nEnter one character'x':");
EnQueue(&q,'x');
Print(&q);
printf("\nDletes the first character\n");
DeQueue(&q,&a);
printf("%c\n",a);
DestroyQueue(&q);
return 0;
}