qq_46307172 2021-04-05 17:57 采纳率: 100%
浏览 33
已采纳

有没有大佬知道这是怎么回事啊?

#include <stdio.h>
struct link {
	int date;
	struct link* next;
};
struct linklist {
	struct link* heat, * p, * q;
};
void Add(struct linklist* a, int date) {
	struct link link1;
	link1.date = date;
	link1.next = NULL;
	if (a->heat == NULL) {
		a->heat = &link1;
		a->p = &link1;(没有这句程序就有问题)
	}
	else {
		link1.next = a->heat;
		a->heat = &link1;
	}
}
void Print(struct linklist a) {
	a.q = a.heat;
	for (; a.q; a.q = a.q->next) {
		printf("%d\n",a.q->date);
	}
}
void main() {
	struct linklist linklist1;
	linklist1.heat = NULL;
	Add(&linklist1, 10);
	Add(&linklist1, 12);
	Print(linklist1);
}

这是有a->p=@link1的运行结果,正常

 

这是没有a->p=@link1的运行结果,还报错了

  • 写回答

3条回答 默认 最新

  • CSDN专家-cpp_learner 2021-04-06 08:49
    关注

    将link1定义为指针就可以了!

    void Add(struct linklist* a, int date) {
    	struct link *link1 = new struct link;
    	link1->date = date;
    	link1->next = NULL;
    	if (a->heat == NULL) {
    		a->heat = link1;
    		//a->p = link1; //(没有这句程序就有问题)
    	} else {
    		link1->next = a->heat;
    		a->heat = link1;
    	}
    }

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

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘