鱼C论坛

 找回密码
 立即注册
查看: 3842|回复: 8

链表嵌套问题

[复制链接]
发表于 2015-12-20 23:13:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
struct student
{
        int stu_number;
        char stu_name[10];
        int select_number;
        struct student *stu_next;
}stu;//学生信息录入
struct link
{
        int number;//课程编号
        char name[10];
        char nature[10];//课程性质
        int time;//总学时
        int time_class;//授课时间
        int score;
        int time_start;//开课时间
        struct student *pstu;
        struct link *next;//节点
};//课程录入

把学生信息录进去之后,大概这样录入
scanf("%d",&stu->stu_number);
scanf("%s",&stu->stu_name);
如何通过struct link 这个调用出学生信息,大概这样子
printf("%d   ",p->pstu->stu_number);
printf("%s   \n",p->pstu->stu_name);
录入没问题的,但是p->pstu->stu_name,语法不会报错,运行时会出错0xc0000005:Access Violation
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-12-21 07:35:32 | 显示全部楼层
你的头节点都没有指定。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-12-21 13:22:24 | 显示全部楼层
ravenhu13 发表于 2015-12-21 07:35
你的头节点都没有指定。

头结点没有指定??可以详细说一下吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-12-21 13:32:20 | 显示全部楼层
本帖最后由 DarkSE 于 2015-12-21 14:01 编辑
ravenhu13 发表于 2015-12-21 07:35
你的头节点都没有指定。


整个代码是这样的
243和244行的输出错误

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct link *course_enroll();//课程录入//完成
  4. struct link *student_enroll();//学生录入//未完成
  5. void course_skim();//课程信息浏览//完成
  6. int check();//学分查询
  7. void event();//课程选修情况//完成
  8. void check_number();//学号查询
  9. void deletememory();
  10. struct student
  11. {
  12.         int stu_number;
  13.         char stu_name[10];
  14.         int select_number;
  15.         struct student *stu_next;
  16. }stu;
  17. struct link
  18. {
  19.         int number;//课程编号
  20.         char name[10];
  21.         char nature[10];//课程性质
  22.         int time;//总学时
  23.         int time_class;//授课时间
  24.         int score;
  25.         int time_start;//开课时间
  26.         struct student *stu;
  27.         struct link *next;//节点
  28. };

  29. main()
  30. {
  31.         int n,i,a=0;
  32.         struct link *head=NULL;
  33.         struct student *stu_head=NULL;
  34.         do
  35.         {
  36.                 system("cls");
  37.                 printf("请选择一下功能:\n");
  38.                 printf("0->课程信息录入系统\n1->学生选课信息录入系统\n");
  39.                 printf("2->课程信息浏览\n3->学分查询\n");
  40.                 printf("4->课程选修情况\n5->学号查询功能\n");
  41.                 do
  42.                 {
  43.                         scanf("%d",&n);
  44.                         if(n<0 || n>6)
  45.                         {
  46.                                 printf("输入错误,请重新输入:");
  47.                         }
  48.                 }while(n<0 || n>6);
  49.                 switch(n)
  50.                 {
  51.                         case 0:
  52.                         {
  53.                                 printf("欢迎进入课程信息录入系统:\n");
  54.                                 printf("请输入录入的课程的门数:\n");
  55.                                 scanf("%d",&i);
  56.                                 head=(course_enroll(head,i));
  57.                                 break;
  58.                         }
  59.                         case 1:
  60.                         {
  61.                                 printf("欢迎进入学生选课信息录入功能:\n");
  62.                                 stu_head=student_enroll(head,stu_head);
  63.                                 break;
  64.                         }
  65.                         case 2:
  66.                         {
  67.                                 printf("课程信息浏览:\n");
  68.                                 course_skim(head);
  69.                                 break;
  70.                         }
  71.                         case 3:
  72.                         {
  73.                                 printf("学分查询功能:\n");
  74.                                 check();
  75.                                 break;
  76.                         }
  77.                         case 4:
  78.                         {
  79.                                 printf("课程选修情况:\n");
  80.                                 event(head,stu_head);
  81.                                 break;
  82.                         }
  83.                         case 5:
  84.                         {
  85.                                 printf("学号查询功能:\n");
  86.                                 check_number(head,stu_head);
  87.                                 break;
  88.                         }
  89.                 }
  90.                 printf("是否退出选课系统?是输入1,否输入0\n");
  91.                 scanf("%d",&a);
  92.                 if(1==a)
  93.                 {
  94.                         deletememory(head,stu_head);
  95.                         exit(0);
  96.                 }
  97.         }while(0==a);
  98. }
  99. struct link *course_enroll(struct link *head,int i)//课程录入
  100. {
  101.         int j=1;
  102.         struct link *p=NULL;
  103.         struct link *pr=head;//指针指向head
  104.         system("cls");//清屏
  105.         printf("请按照以下格式输入课程信息:\n");
  106.         printf("----编号---名称---性质---学时---授课时间---学分---开课学期----\n");
  107.         for(j;j<=i;j++)//输入的课程的数目
  108.         {
  109.                 p=(struct link *)malloc(sizeof(struct link));//为struct link申请内存
  110.                 if(p==NULL)//检查申请内存是否为空
  111.                 {
  112.                         printf("系统错误!!");
  113.                         exit(0);
  114.                 }
  115.                 if(head==NULL)//检查head是否为空
  116.                 {
  117.                         head=p;//head为空,把第一个当做第一个链表
  118.                 }
  119.                 else//head 不为空
  120.                 {
  121.                         while (pr->next != NULL)//指向下一个,直到链尾为止
  122.                         {
  123.                                 pr=pr->next;
  124.                         }
  125.                         pr->next=p;//把申请的内存接在链尾后面
  126.                 }
  127.                 pr=p;//把空链表指向head
  128.                 scanf("%d",&pr->number);
  129.                 scanf("%s",&pr->name);
  130.                 scanf("%s",&pr->nature);
  131.                 scanf("%d",&pr->time);
  132.                 scanf("%d",&pr->time_class);
  133.                 scanf("%d",&pr->score);
  134.                 scanf("%d",&pr->time_start);
  135.                 pr->next=NULL;//最后一个的指针域定义为NULL
  136.         }
  137.         return head;
  138. }
  139. struct link *student_enroll(struct link *head,struct student *stu_head)//学生录入//学生选择课程的编号进而录入
  140. {
  141.         int number;
  142.         struct student *pstu=NULL;
  143.         struct student *pstur=stu_head;
  144.         struct link *p=head;
  145.         if(p==NULL)//head是否为空
  146.         {
  147.                 printf("NO ANY DATA !!\n");//没有数据可供选课
  148.         }
  149.         else
  150.         {
  151.                 printf("请输入选择的课程编号:");
  152.                 scanf("%d",&number);
  153.                 while(p!=NULL)//
  154.                 {
  155.                         if( number == p->number )//寻找存在的课程编号
  156.                         {
  157.                                 printf("请输入学生学号及姓名:");
  158.                                  pstu=(struct student *)malloc(sizeof(struct student));
  159.                                 if(pstu==NULL)//申请内存为空
  160.                                 {
  161.                                         printf("系统错误!!");
  162.                                         exit(0);
  163.                                 }
  164.                                 if(stu_head==NULL)//stu_head为空
  165.                                 {
  166.                                         stu_head=pstu;
  167.                                 }
  168.                                 else
  169.                                 {
  170.                                         while(pstur->stu_next != NULL)//stu_head不为空,寻找到链尾为止
  171.                                         {
  172.                                                 pstur=pstur->stu_next ;
  173.                                         }
  174.                                         pstur->stu_next =pstu;//申请的内存加入链尾
  175.                                 }
  176.                                 pstur=pstu;
  177.                                 scanf("%d",&pstu->stu_number);
  178.                                 scanf("%s",&pstu->stu_name);
  179.                                 pstu->stu_next=NULL;//新节点指针域定义为NULL
  180.                                 break;
  181.                         }
  182.                         p=p->next ;//当寻找不到符合的课程编号,则指向下一个继续寻找,直到链尾
  183.                 }
  184.         }
  185.         return stu_head;
  186. }
  187. void course_skim(struct link *head)//课程信息浏览
  188. {
  189.         struct link *p=head;
  190.         system("cls");
  191.         if(p==NULL)
  192.         {
  193.                 printf("NO ANY DATA !!\n");
  194.         }
  195.         else
  196.         {
  197.         printf("----编号---名称---性质---学时---授课时间---学分---开课学期----\n");
  198.         while(p != NULL)
  199.         {
  200.                 printf("%d   ",p->number);
  201.                 printf("%s   ",p->name);
  202.                 printf("%s   ",p->nature);
  203.                 printf("%d   ",p->time);
  204.                 printf("%d   ",p->time_class);
  205.                 printf("%d   ",p->score);
  206.                 printf("%d   \n",p->time_start);
  207.                 p = p->next;
  208.         }
  209.         }
  210. }
  211. int check()//学分查询
  212. {
  213.         printf("stupid  3\n");
  214. }
  215. void event(struct link *head,struct student *stu_head)//课程选修情况
  216. {
  217.         struct link *p=head;
  218.         struct student *pr=stu_head;
  219.         system("cls");
  220.         if(p==NULL)
  221.         {
  222.                 printf("NO ANY DATA !!\n");
  223.         }
  224.         else if(pr == NULL)
  225.         {
  226.                 printf("学生未进行选课\n");
  227.         }
  228.         else
  229.         {
  230.                 //输出课程信息
  231.                 printf("----编号---名称---性质---学时---授课时间---学分---开课学期---学生学号---学生姓名----\n");
  232.                 while(p != NULL )
  233.                 {
  234.                         printf("%d   ",p->number);
  235.                         printf("%s   ",p->name);
  236.                         printf("%s   ",p->nature);
  237.                         printf("%d   ",p->time);
  238.                         printf("%d   ",p->time_class);
  239.                         printf("%d   ",p->score);
  240.                         printf("%d   ",p->time_start);
  241.                         //输出课程里面的学生信息
  242.                         printf("%d   ",p->stu->stu_number);//通过head来输出学生信息
  243.                         printf("%s   \n",p->stu->stu_name);
  244.                         p = p->next;
  245.                 }
  246.         }
  247. }
  248. void check_number(struct link *head,struct student *stu_head)//5
  249. {
  250.         int number;
  251.         struct link *p=head;
  252.         struct student *pr=stu_head;
  253.         system("cls");
  254.         if(p==NULL)
  255.         {
  256.                 printf("NO ANY DATA !!\n");
  257.         }
  258.         else if(pr->stu_number == NULL)
  259.         {
  260.                 printf("未有选课记录!!\n");
  261.         }
  262.         else
  263.         {
  264.                 printf("请输入查询的学生学号:");
  265.                 scanf("%d",&number);
  266.                 do
  267.                 {
  268.                         if(number == (pr->stu_number))
  269.                         {
  270.                                 printf("%d   ",p->number);
  271.                                 printf("%s   \n",p->name);
  272.                         }
  273.                         pr=pr->stu_next ;
  274.                 }while( pr->stu_next != NULL);
  275.         }
  276. //        printf("foolish  5\n");
  277. }
  278. //清除分配的内存
  279. void deletememory(struct link *head,struct student *stu_head)
  280. {
  281.         struct link *p=head;
  282.         struct student *pstu=stu_head;
  283.         struct link *pr=NULL;
  284.         while(p != NULL)
  285.         {
  286.                 pr=p;
  287.                 p=p->next ;
  288.                 free(pr);
  289.         }
  290.         while(pstu!=NULL)
  291.         {
  292.                 pr=pstu;
  293.                 pstu=pstu->stu_next ;
  294.                 free(pr);
  295.         }
  296. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-12-21 14:41:23 | 显示全部楼层
明显是非法访问 你把测试案例 发给我 我帮你看看就知道了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-12-21 14:57:17 | 显示全部楼层
已经知道了  
  1. struct link
  2. {
  3.         int number;//课程编号
  4.         char name[10];
  5.         char nature[10];//课程性质
  6.         int time;//总学时
  7.         int time_class;//授课时间
  8.         int score;
  9.         int time_start;//开课时间
  10.         struct student *stu;                          //问题在这里  
  11.         struct link *next;//节点
  12. };
复制代码


这里只是一个指针  他并没有内存空间  而你 从头到尾也没有把学生信息的内存块 挂载到上面  这里是一个存放着未知的内存地址而且是不可访问的  导致你一运行到这里  就被win强制关闭了  解决办法是  把学生内存块挂载上  就行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2015-12-21 18:35:22 | 显示全部楼层
我刚学动态链表。。。。。。。。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-12-21 18:59:07 | 显示全部楼层
好多好多鱼 发表于 2015-12-21 14:57
已经知道了   

这里只是一个指针  他并没有内存空间  而你 从头到尾也没有把学生信息的内存块 挂载到上 ...

谢谢~~,解决了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-12-21 18:59:50 | 显示全部楼层
tzzyw 发表于 2015-12-21 18:35
我刚学动态链表。。。。。。。。。。

我也是刚学,老师比较坑而已
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-21 16:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表