C语言创建一个链表用来存放自己定义的结构体中的数据,用循环存入多组数据

如题所述

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<malloc.h>

typedef struct lianbiao
{
int a;
struct lianbiao* next;
} lb;
void print(lb *head);
main()
{
int n=5;
lb *head=NULL,*wei=NULL,*ls;
while(n--)
{
ls=malloc(sizeof(lb));
if(ls==NULL)
{
printf("error!\n");
exit(0);
}
scanf("%d",&ls->a);
ls->next=NULL;



if(head==NULL)
{
head=ls;
wei=ls;
}
else
{
wei->next=ls;
wei=ls;
}
}

print(head);
}


void print(lb *head)
{
lb* ls=head;
while(ls!=NULL)
{
printf("%d\n",ls->a );
ls=ls->next;
}
}

dev cpp 5.11 能过= =

追问

编译没有错误,运行时出错怎么回事,求大神指点

追答

你把代码文本贴上来我看看= =
你第二张图上的scanf弄得我好迷哦...结构体里的变量应该用&取指针吧
还有啊..node你申请了内存,但是node下的student你好像没有申请相应的内存啊

先这样吧...我还要整自己的事情呢....要是还有什么问题的话带上代码文本问吧...毕竟看图不方便.

追问

私信求解答不能上传文本

温馨提示:答案为网友推荐,仅供参考
相似回答