免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1639 | 回复: 5
打印 上一主题 下一主题

[应用] 一个链表问题,大侠指导下! [复制链接]

论坛徽章:
2
2015年迎新春徽章
日期:2015-03-04 09:57:09C
日期:2016-10-25 16:17:59
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-06-17 11:53 |只看该作者 |倒序浏览
  1.   /*main.c*/
  2.   1 #include<stdio.h>
  3.   2 #include<stdlib.h>
  4.   3 #include"myfile.h"
  5.   4 struct student
  6.   5 {
  7.   6         char name[20];
  8.   7         float score;
  9.   8         struct student *next;
  10.   9 };
  11. 10 int main()
  12. 11 {
  13. 12         struct student *p,*p1,*head;
  14. 13         int n,i;
  15. 14         head=NULL;
  16. 15         p=(struct student *)malloc(sizeof(struct student));
  17. 16         head=p;
  18. 17         printf("please input the first data name and score:\n");
  19. 18         scanf("%s%f",(*p).name,p->score);
  20. 19         (*p).next=NULL;
  21. 20         printf("please input the number you want to add:n=?\n");
  22. 21         scanf("%d",&n);
  23. 22         for(i=0;i<n;i++)
  24. 23         {
  25. 24                 p1=(struct student *)malloc(sizeof(struct student));
  26. 25                 scanf("%s%f",(*p1).name,p1->score);
  27. 26                 (*p).next=p1;
  28. 27         }
  29. 28         (*p1).next=NULL;
  30. 29         void printdata(head);
  31. 30 }
  32. /*printdata.c*/
  33. 1 #include<stdio.h>
  34.   2 void printdata(struct student *head)
  35.   3 {
  36.   4         struct student *p;
  37.   5         p=head;
  38.   6         while(p!=NULL)
  39.   7         {
  40.   8                 printf("%s:%f\n",p->name,p->score);
  41.   9                 p=p->next;
  42. 10         }
  43. 11 }
  44. /*myfile.h*/
  45. void printdata(struct student *head);
复制代码
编译器的信息也贴出来,前辈指导下!

012.jpg (59.5 KB, 下载次数: 24)

012.jpg

论坛徽章:
2
2015年迎新春徽章
日期:2015-03-04 09:57:09C
日期:2016-10-25 16:17:59
2 [报告]
发表于 2010-06-18 10:13 |只看该作者
搞不出来了:
  1. 文件一:mian.c
  2. 1 #include<stdio.h>
  3.   2 #include<stdlib.h>
  4.   3 #include"myfile.h"
  5.   4 struct student
  6.   5 {
  7.   6         char name[20];
  8.   7         float score;
  9.   8         struct student *next;
  10.   9 };
  11. 10 int main()
  12. 11 {
  13. 12         struct student *p,*p1,*head;
  14. 13         int n,i;
  15. 14         head=NULL;
  16. 15         p=(struct student *)malloc(sizeof(struct student));
  17. 16         head=p;
  18. 17         printf("please input the first data name and score:\n");
  19. 18         scanf("%s%f",(*p).name,&p->score);
  20. 19         (*p).next=NULL;
  21. 20         printf("please input the number you want to add:n=?\n");
  22. 21         scanf("%d",&n);
  23. 22         for(i=0;i<n;i++)
  24. 23         {
  25. 24                 p1=(struct student *)malloc(sizeof(struct student));
  26. 25                 scanf("%s%f",(*p1).name,&p1->score);
  27. 26                 (*p).next=p1;
  28.                     P=p1;
  29. 27         }
  30. 28         (*p1).next=NULL;
  31. 29         void printdata(head);
  32. 30 }
  33. 文件二:printdata.c
  34. 1 #include<stdio.h>
  35.   2 void printdata(struct student *head)
  36.   3 {
  37.   4         struct student *p;
  38.   5         p=head;
  39.   6         while(p!=NULL)
  40.   7         {
  41.   8                 printf("%s:%f\n",p->name,p->score);
  42.   9                 p=p->next;
  43. 10         }
  44. 11 }
  45. 文件三:myfile.h
  46. void printdata(struct student *head);
复制代码

论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
3 [报告]
发表于 2010-06-18 11:20 |只看该作者
printdata.c不认识那个结构呀,include myfile.h
结构定义放到myfile.h中

论坛徽章:
2
2015年迎新春徽章
日期:2015-03-04 09:57:09C
日期:2016-10-25 16:17:59
4 [报告]
发表于 2010-06-18 13:17 |只看该作者
为什么呢?结构体不是在主函数里声明过了。函数编译的时候不会加载printdata.c里的内容吗?

论坛徽章:
2
2015年迎新春徽章
日期:2015-03-04 09:57:09C
日期:2016-10-25 16:17:59
5 [报告]
发表于 2010-06-18 14:13 |只看该作者
又出新问题,子函数没有运行,帮看下:
  1. 文件一:
  2. 1 #include<stdio.h>
  3.   2 extern struct student
  4.   3 {            
  5.   4         char name[20];
  6.   5         float score;  
  7.   6         struct student *next;
  8.   7 };                           
  9.   8  void printdata(struct student *p)
  10.   9 {                                 
  11. 10         while(p!=NULL)
  12. 11         {                                            
  13. 12                 printf("%s:%f\n",(*p).name,p->score);
  14. 13                 p=p->next;                           
  15. 14         }                 
  16. 15 }
  17.                              
  18. 文件二:
  19. 1 #include<stdio.h>
  20.   2 #include<stdlib.h>
  21.   3 struct student
  22.   4 {
  23.   5         char name[20];
  24.   6         float score;
  25.   7         struct student *next;
  26.   8 };
  27.   9 void printdata(struct student *p);
  28. 10 int main()
  29. 11 {
  30. 12         struct student *p,*p1,*p2,*head;
  31. 13         int n,i;
  32. 14         head=NULL;
  33. 15         p=(struct student *)malloc(sizeof(struct student));
  34. 16         head=p;
  35. 17         printf("please input the first data name and score:\n");
  36. 18         scanf("%s%f",(*p).name,&p->score);
  37. 19         (*p).next=NULL;
  38. 20         printf("please input the number you want to add:n=?\n");
  39. 21         scanf("%d",&n);
  40. 22         printf("please input other datas name and score:\n");
  41. 23         for(i=0;i<n;i++)
  42. 24         {
  43. 25                 p1=(struct student *)malloc(sizeof(struct student));
  44. 26                 scanf("%s%f",(*p1).name,&p1->score);
  45. 27                 (*p).next=p1;
  46. 28                 p=p1;
  47. 29         }
  48. 30         (*p).next=NULL;
  49. 31         void printdata(head);
  50. 38 }
复制代码

论坛徽章:
5
摩羯座
日期:2014-07-22 09:03:552015元宵节徽章
日期:2015-03-06 15:50:392015亚冠之大阪钢巴
日期:2015-06-12 16:01:352015年中国系统架构师大会
日期:2015-06-29 16:11:2815-16赛季CBA联赛之四川
日期:2018-12-17 14:10:21
6 [报告]
发表于 2010-06-18 20:19 |只看该作者
楼上去C语言板块打打基础
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP