69,377
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
struct student
{
long no;
char name[20];
char add[30];
struct student *next;
};
typedef struct student LIST;
LIST *CreateList();
void DispList(LIST *h);
int main()
{
LIST *head;
head=CreateList();
DispList(head);
}
LIST* CreateList()
{
LIST *h,*prev,*cur;
int i,n;
h=NULL;
printf("输入节点个数:\n");
scanf("%d",&n);
for(i=0; i<n; i++) {
cur = (LIST*)malloc(sizeof(LIST));
cur->next = NULL;
if(h == NULL) {
h = cur;
prev = h;
} else {
prev->next = cur;
prev=cur;
}
printf("输入 学号 姓名 地址\n");
scanf("%ld %s %s",&cur->no,cur->name,cur->add);
}
return h;
}
void DispList(LIST *h)
{
LIST *p=h;
printf("学号 姓名 地址\n");
while(p) {
printf("%ld %s %s\n",p->no,p->name,p->add);
p = p->next;
}
}
参考一下吧
注意链表创建时的逻辑。否则会出现段错误if (h = NULL)
改成
if (h == NULL)