BEI-TIAN-XUAN 2021-02-25 14:05 采纳率: 66.7%
浏览 62
已采纳

C语言链队销毁问题(求求帮帮孩子)

 

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

出问题的代码:

Status DestroyQueue(LinkQueue Q)  //从队头结点依次销毁
{
	while (Q.front)
	{
		Q.rear = Q.front->next;
		free(Q.front);
		Q.front = Q.rear;
	}
	return OK;
}

运行后如下:

全部源码:

#include<stdio.h>
#include<stdlib.h>
#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;
}
  • 写回答

2条回答 默认 最新

  • 爱晚乏客游 2021-02-25 15:09
    关注

    内存泄漏,比如说你的队列里面有5个,在删除玩第五个之后,按道理来说你的Q.front->next=null,但是我debug发现你的Q.front->next!=null,你看看你前面的插入或者删除哪里出错了吧

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

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分