m0_58203183 2022-07-24 21:41 采纳率: 85.7%
浏览 40
已结题

为什么在提取第一个元素后,再输出第一个元素两者一样,第一个元素出栈后,下一次的一第一个元素,不应该是第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

  • 写回答

2条回答 默认 最新

  • flower980323 2022-07-25 09:52
    关注

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

    img


    如有帮助,请采纳

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 8月2日
  • 已采纳回答 7月25日
  • 创建了问题 7月24日

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看