|
|
发表于 2012-8-20 16:16:26
|
显示全部楼层
- #include "stdio.h" //标准库的头文件最好用<>
- #include"malloc.h"
- #include"stdlib.h"
- #define LEN sizeof(struct stud)
- struct stud *creat();
- struct stud
- {
- long a;//定义的是long型变量
- struct stud *next;
- };
- int n;
- void main()
- {
- struct stud *qq;
- qq=creat();
- do
- {
- printf("%ld\n",qq->a);//改为ld%
- qq=qq->next;
- }while(qq!=0);
-
-
- }
- struct stud *creat()
- {
- struct stud *head;struct stud *p1,*p2;
- p1=p2=(struct stud *)malloc(LEN);
- scanf("%ld",&p1->a);//改为ld%
- n=0;head=p1;//这里先把p1给head,下面的循环不执行的话返回是空指针,会出错
- while(p1->a!=0)
- {
- n++;
- if(n==1) head=p1;
- else p2->next=p1;
- p2=p1;
- p1=(struct stud *)malloc(LEN);
- scanf("%ld",&p1->a);//改为ld%
- }
- p2->next=0;
- printf("%d\n",n);
- return head;
-
- }
复制代码 |
|