链表输出错误
#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;
}
}
...全文
相关推荐
C语言 基于链表的日历格式解析 及指定格式输出.rar
先读取日历(文本格式),获取各个事件。每读取一个事件,按时间顺序插入链表。根据程序启动时给定的起止时间参数,输出对应的事件。
数据结构链表作业
北航数据结构链表操作实践作业.要求:从文件中读入一组成绩,并倒序打印至另一文件中。数据用链表存储。
数据结构(链表操作)
本程序是数据结构作业,实现链表的创建,删除,逆序输出等操作。
C语言 链表使用示例
c语言_链表实例讲解(两个经典例子) 建立一个学生成绩的线性链表,对其实现插入,删除,输出,最后销毁。
C语言数据结构实现链表逆序并输出
主要介绍了C语言数据结构实现链表逆序并输出的相关资料,需要的朋友可以参考下


