c语言打链队中元素,C语言链队销毁问题(求求帮帮孩子)

这篇博客主要展示了C语言中链队列的实现,包括初始化、入队、出队、取头元素和销毁操作。在销毁队列的过程中出现了一些问题,导致程序无法正常工作。博主分享了出现问题的代码段,并寻求帮助来解决销毁队列时的错误。
摘要由CSDN通过智能技术生成

那位大佬知道啊!我调试一直不行,那位好心得大佬帮帮忙啊!我实在是不行了,孩子求求了。出问题的代码

出问题的代码:Status DestroyQueue(LinkQueue Q) //从队头结点依次销毁

{

while (Q.front)

{

Q.rear = Q.front->next;

free(Q.front);

Q.front = Q.rear;

}

return OK;

}

运行后如下:

bVcOYY3

全部源码:#include

#include

#define OK 1

#define FALSE 0

typedef int ElemType;

typedef int Status;

typedef struct QNode

{

ElemType data;

struct QNode* next;

}QNode, *Queueptr;

typedef struct

{

Queueptr front; //队头指针

Queueptr rear; //队尾指针

}LinkQueue;

Status InitQueue(LinkQueue* Q); //初始化

Status InsertQueue(LinkQueue* Q, ElemType e); //入队

Status DeletQueue(LinkQueue* Q, ElemType* e); //出队

Status GetHead(LinkQueue Q); //取头结点元素

Status DestroyQueue(LinkQueue Q); //销毁

int main()

{

LinkQueue Q;

ElemType e, a;

int n;

if (InitQueue(&Q) == 1)

printf("构建链队成功!\n");

printf("入队元素个数:");

scanf("%d", &n);

printf("请输入入队元素:");

for (int i = 0; i < n; i++)

{

scanf("%d", &e);

InsertQueue(&Q, e);

}

printf("入队成功!\n");

a=GetHead(Q);

if (a != 0)

printf("队头元素为:%d\n", a);

else

printf("队空!无头元素!\n");

if (DeletQueue(&Q, &e) == 0)

printf("队为空!无法出队!\n");

else

printf("出队元素为:%d\n", e);

DestroyQueue(Q);

printf("销毁完毕!");

return 0;

}

Status InitQueue(LinkQueue* Q)

{

Q->front = Q->rear = (Queueptr)malloc(sizeof(QNode));

if (!Q->front)

exit(0);

Q->front->next = NULL;

return OK;

}

Status InsertQueue(LinkQueue* Q, ElemType e)

{

Queueptr P;

P = (Queueptr)malloc(sizeof(QNode));

if (!P)

exit(0);

P->data = e;

Q->rear->next = P;

Q->rear = P;

return OK;

}

Status GetHead(LinkQueue Q)

{

if (Q.front == Q.rear)

return FALSE;

return (Q.front->next->data);

}

Status DeletQueue(LinkQueue* Q, ElemType* e)

{

Queueptr P;

if (Q->front == Q->rear) //队空

return FALSE;

P = Q->front->next;

*e = P->data;

Q->front->next = P->next;

if (Q->rear == P) //当删除到尾结点时

Q->rear = Q->front;

free(P);

return OK;

}

Status DestroyQueue(LinkQueue Q) //从队头结点依次销毁

{

while (Q.front)

{

Q.rear = Q.front->next;

free(Q.front);

Q.front = Q.rear;

}

return OK;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值