鱼C论坛

 找回密码
 立即注册
查看: 1317|回复: 1

又来求助问题了,这次是关于链表的

[复制链接]
发表于 2014-12-24 18:12:15 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 siberian_wolf 于 2014-12-24 19:18 编辑

终于快要到教程的结束部分了,好高兴的说。可是又被卡住了,想了2天2夜了,求鱼油帮忙。都说旁观者清,望指点迷津。以下是代码:
  1. /*这是正常版本*/

  2. #include<stdio.h>
  3. #include<malloc.h>
  4. #include<stdlib.h>

  5. #define LEN sizeof(struct student)  //student结构的大小

  6. int count_node;  //列表中的节点数

  7. struct student *creat_list();  //创建链表
  8. void print_list(struct student *head);   //打印链表
  9. struct student *del_node(struct student *head);  //删除节点
  10. struct student *insert_node(struct student *head, struct student *new_node); //插入节点

  11. struct student
  12. {
  13.       int num;
  14.       float score;
  15.       struct student *next;
  16. };   

  17. void main()
  18. {
  19.       struct student *stu, *insert, new_node;
  20.       
  21.       insert=stu = creat_list();
  22.       print_list(stu);
  23.       
  24. /*start*/      
  25.       printf("\nPlease input the num to insert:");
  26.       scanf("%d", &new_node.num);//记录:如果用指针输入,由于没有初始化,会导致出错
  27.       printf("\nPlease input the score:");
  28.       scanf("%f", &new_node.score);
  29. /*end*/

  30.       insert= insert_node(insert,&new_node);
  31.       print_list(insert);
  32.       
  33.       printf("\n\n");
  34.       system("pause");
  35.       
  36. }

  37. struct student *insert_node(struct student *head, struct student *new_node)
  38. {
  39.       struct student *p1, *p2, *p0;

  40.       p1 = head;
  41.       p0 = new_node;  


  42.       
  43.    
  44.       if(NULL == head)  //如果是空列表,直接插入
  45.       {
  46.             head = p0;
  47.             p0->next = NULL;      
  48.       }
  49.       else
  50.       {
  51.             while((p0->num > p1->num) && (p1->next != NULL))  //寻找插入点,假设按由小到大排序
  52.             {
  53.                   p2 = p1;
  54.                   p1 = p1->next;
  55.             }
  56.             
  57.             if(p0->num <= p1->num)
  58.             {
  59.                   if(head == p1)  //p1是头节点,插入头部
  60.                   {
  61.                         head = p0;
  62.                         p0->next = p1;
  63.                   }
  64.                   else           //普通情况,插入中间
  65.                   {
  66.                         p2->next = p0;
  67.                         p0->next = p1;
  68.                   }
  69.             }
  70.             else                 //p0最大,插入末尾
  71.             {
  72.                   p1->next = p0;
  73.                   p0->next = NULL;
  74.             }
  75.       }
  76.       
  77.       count_node+=1;
  78.       printf("\nInsert succeed!");
  79.       
  80.       return head;
  81. }



  82. struct student *creat_list()
  83. {
  84.       struct student *head, *p1, *p2;
  85.       
  86.       head=NULL;
  87.       count_node=0;
  88.       
  89.       /*定义新节点并赋值*/
  90.       
  91.       p1=(struct student *)malloc(LEN);  //LEN是student结构体的大小,(struct student *)是强制数据类型转换。
  92.       printf("Please enter the num:");
  93.       scanf("%d", &p1->num);
  94.       printf("Please enter the score:");
  95.       scanf("%f", &p1->score);
  96.       
  97.       p2=p1;    //p2移到最后一个节点
  98.       
  99.       
  100.       while(p1->num)
  101.       {
  102.             count_node++;    //统计节点的个数
  103.             if(1==count_node)
  104.             {
  105.                   head=p1;  //head指向第一个元素
  106.             }
  107.             else
  108.             {
  109.                   p2->next=p1;   //链表最后一个元素指向新元素
  110.             }
  111.             
  112.             p2=p1;    //p2移到链表末尾
  113.             
  114.             p1=(struct student *)malloc(LEN);      
  115.             printf("\n\nPlease enter the num:");
  116.             scanf("%d", &p1->num);
  117.             printf("Please enter the score:");
  118.             scanf("%f", &p1->score);  
  119.       }
  120.       
  121.       p2->next=NULL;
  122.       
  123.       return head;
  124. }



  125. void print_list(struct student *head)
  126. {
  127.       struct student *p;
  128.       p=head;
  129.       
  130.       printf("\n\nThere are %d records!\n\n", count_node);
  131.       
  132.       if(NULL != head)
  133.       {
  134.             do
  135.             {
  136.                   printf("学号为 %d 的学生的分数是: %.2f\n", p->num, p->score);
  137.                   p=p->next;
  138.             }while(p);
  139.       }
  140.       else
  141.       {
  142.             printf("无学生数据!");
  143.       }     
  144. }
复制代码


-----------------------------------------------------------------------------------------------------------------------------------------------------------
  1. /*问题版本*/

  2. #include<stdio.h>
  3. #include<malloc.h>
  4. #include<stdlib.h>

  5. #define LEN sizeof(struct student)  //student结构的大小

  6. int count_node;  //列表中的节点数

  7. struct student *creat_list();  //创建链表
  8. void print_list(struct student *head);   //打印链表
  9. struct student *del_node(struct student *head);  //删除节点

  10. struct student *insert_node(struct student *head); //插入节点

  11. struct student
  12. {
  13.       int num;
  14.       float score;
  15.       struct student *next;
  16. };   

  17. void main()
  18. {
  19.       struct student *stu, *insert, new_node;
  20.       
  21.       insert=stu = creat_list();
  22.       print_list(stu);
  23.       


  24.       insert= insert_node(insert);
  25.       print_list(insert);
  26.       
  27.       printf("\n\n");
  28.       system("pause");
  29.       
  30. }

  31. struct student *insert_node(struct student *head)
  32. {
  33.       struct student *p1, *p2, *p0;
复制代码

两个版本的区别是:把正常版本start-end部分(就是新插入节点的输入部分)挪到了插入函数(insert _node)内部,这样做的好处是每次需要添加新节点的时候就直接调用这个函数就好了,不需要在主函数中出现赋值语句。但是问题来了,移到内部后可以正常执行,但是执行完插入后再打印这个列表就出现了问题。请大家帮忙分析分析。(在写这个文章的时候突然想到了,难道跟变量的生存周期有关??)

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-12-24 19:01:22 | 显示全部楼层
本帖最后由 siberian_wolf 于 2014-12-24 19:19 编辑

哎,没人来吗?自己回去顺着思路想了一下,又翻看了,小甲鱼老师的课件,发现好像真的是变量的生存周期有问题。只要在问题版本定义new_node为static即可正常运行。或者是采用malloc函数方式申请空间就可以啦!!!这个问题困扰了我2天1夜突然间解决掉了,感觉好兴奋的!!保留此帖留作纪念,希望给后来者提供经验,也给自己留下一个难忘的节点。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-18 12:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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