为什么在提取第一个元素后,再输出第一个元素两者一样,第一个元素出栈后,下一次的一第一个元素,不应该是第2个吗

#include "stdio.h"
#include "stdlib.h"
#include "io.h"
#include "math.h"
#include "time.h"
#if(1)
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 20 /* 存储空间初始分配*/

typedef int Status;
typedef char ElemType; /* SElemType类型根据实际情况而定,这里假设为int */

typedef struct QNode//队结点
{
ElemType data;
struct QNode *next;
}QNode,*Queueptr;

typedef struct //链队类型
{
Queueptr front;
Queueptr rear;
}LinkQueue;

Status INitQueue(LinkQueue *p)//初始化
{
p->front=p->rear=(Queueptr)malloc(sizeof(QNode));
if(!p->front)
{
exit(0);
}
p->front->next=NULL;
return OK;
}

Status ClearQueue(LinkQueue *p)//清空栈
{
if(p->front!=p->rear)
{
p->front=p->rear;
}
return OK;
}

Status QueueEmpty(LinkQueue *p)//判读是否为空
{
if(p->front==p->rear)
{
return TRUE;
}
else
{
return ERROR;
}
}

Status EnQueue(LinkQueue *p,ElemType *e)//插入元素
{
Queueptr q;
q=(Queueptr)malloc(sizeof(QNode));
if(!p->front)
{
exit(0);
}
q->data=*e;
q->next=NULL;
p->rear->next=q;
p->rear=q;
return OK;
}

Status DeQueue(LinkQueue *p,ElemType *e)//弹出元素
{
Queueptr q;
if(p->front==p->rear)
{
return ERROR;
}
q=p->front->next;
*e=q->data;
p->front->next=q->next;
if(p->rear==q)
{
p->rear=p->front;
}
free(q);

}

Status QueueLength(LinkQueue *p)//队列长度
{
return (p->rear-(p->front->next));
}

Status GetHead(LinkQueue *p,ElemType *e)//队列不为空输出第一个元素
{
if(QueueEmpty(p))
{
*e=p->front->next->data;
return OK;
}
else
{
return ERROR;
}
}

Status StackTraverse(LinkQueue *p)//打印出元素
{
Queueptr q;
q=p->front->next;
while(q)
{
printf("%c ",q->data);
q=q->next;
}
printf("\n");
return OK;
}
Status Destory(LinkQueue *p)//销毁队列
{
while(p->front)
{
p->rear=p->front->next;
free(p->front);
p->front=p->rear;
}
return OK;
}
int main()
{
LinkQueue p;
ElemType c;
ElemType e;

INitQueue(&p);

printf("输入一串字符串(以#结尾):\n");
scanf("%c",&c);
while(c!='#')
{
    EnQueue(&p,&c);
    scanf("%c",&c);
}
printf("链栈的长度为: %d" ,QueueLength(&p));
printf("\n");


printf("依次弹出元素:");
StackTraverse(&p);
printf("\n");

printf("弹出元素:");
DeQueue(&p,&e);
printf(" %c \n",e);

printf("链队是否为空: %d(1是,0否)\n",QueueEmpty(&p));

printf("再次弹出元素:");
GetHead(&p,&e);
printf(" %c \n",e);
system("pause");

return OK;

}
#endif

img

再次弹出的时候调错函数了GetHead(&p,&e);改为DeQueue(&p, &e);,可以使用StackTraverse(&p);打印整个队列查看内容
修改后运行截图

img


如有帮助,请采纳

在调用GetHead(&p,&e);之前,我调用了DeQueue(&p, &e);函数,这个时候第一个元素不是已经弹出了并释放空间,并且指向了第二个元素,这个时候调用GetHead(&p,&e);弹出的第一元素为什么不是第2个