链表输出错误

starcat starcat 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#include <stdlib.h>
#include <stdio.h>
struct node
{
int num;
struct node *next;
}
main()
{
struct node *creat();
void print();
struct node *head;
head = NULL;
head = creat(head);
print(head);
}
struct node *creat(struct node *head)
{
struct node *p1, *p2;
p1 = p2 = (struct node *)malloc(sizeof(struct node));
scanf("%d", &p1 -> num);
p1 -> next = NULL;
while(p1 -> num != -1)
{
if(head == NULL)
head = p1;
else
p2 -> next = p1;
p2 = p1;
p1 = (struct node *)malloc(sizeof(struct node));
scanf("%d", &p1 -> num);
p1 -> next = NULL;//这一句网上那个例子里没有,自己加上去的。
}
// p1 -> next = NULL;为什么放在这里不行呢?
return head;
}
void print(struct node *head)
{
struct node *temp;
temp = head;
while(temp != NULL)
{
printf("%3d", temp -> num);
temp = temp -> next;
}
}
...全文

相关推荐

评论 11
收藏 已收藏
分享
返回顶部
starcat
C语言 70,039 成员 243,237 内容