鱼C论坛

 找回密码
 立即注册
查看: 2366|回复: 2

求超级详细注释,视频讲的太模糊

[复制链接]
发表于 2012-2-17 23:49:53 | 显示全部楼层 |阅读模式
20鱼币
  1. #include <stdio.h>#include <malloc.h>

  2. #define LEN sizeof(struct student)    //student结构大小;

  3. struct student *creat();            //创建链表;
  4. void print(struct student *head);    //打印链表;

  5. struct student
  6. {
  7.     int num;
  8.     float score;
  9.     struct student *next;
  10. };

  11. int n;//全局变量,用来记录存放多少数据;

  12. void main()
  13. {
  14.     struct student *stu;

  15.     stu = creat();
  16.     print(stu);

  17.     printf("\n\n");
  18.     getch();
  19. }

  20. struct student *creat()
  21. {
  22.     struct student *head;
  23.     struct student *p1, *p2;

  24.     p1 = p2 = (struct student *)mall(LEN);    //LEN是结构体大小;
  25.     printf("Please enter the Nun: ");
  26.     scanf("%d", &p1->num);
  27.     printf("Please enter the Score: ");
  28.     scanf("%f", &p1->score);

  29.     head = NULL;
  30.     n = 0;

  31.     while(p1->num)
  32.     {
  33.         n++;
  34.         if(1 == n)
  35.         {
  36.             head = p1;
  37.         }
  38.         else
  39.         {
  40.             p2->next = p1;
  41.         }

  42.         p2 = p1;
  43.         p1 = (struct student *)malloc(LEN);
  44.         printf("Please enter the Nun: ");
  45.         scanf("%d", &p1->num);
  46.         printf("Please enter the Score: ");
  47.         scanf("%f", &p1->score);
  48.     }
  49.     p2->next = NULL;
  50.     return head;
  51. }
复制代码

最佳答案

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2012-2-17 23:49:54 | 显示全部楼层
本帖最后由 Mr.C 于 2012-2-18 01:25 编辑
  1. #include <stdio.h>
  2. //用于包含输入输出函数
  3. #include <malloc.h>
  4. //用于包含malloc函数
  5. #define LEN sizeof(struct student)    //student结构大小;

  6. struct student *creat();            //创建链表;
  7. void print(struct student *head);    //打印链表;
  8. //创建student结构体模型
  9. struct student
  10. {
  11.         //学生ID号
  12.     int num;
  13.     //学生分数
  14.     float score;
  15.     //指向student类型的指针
  16.     struct student *next;
  17. };

  18. int n;//全局变量,用来记录存放多少数据;
  19. //主函数
  20. void main()
  21. {
  22.         //定义一个student结构体的指针变量
  23.     struct student *stu;
  24.         //调用create函数stu返回链表首地址
  25.     stu = creat();
  26.     //将首地址传给print函数,进行打印
  27.     print(stu);
  28.         //打印两个回车
  29.     printf("\n\n");
  30.     //让控制台窗口停留,以便观看输出结果
  31.     getch();
  32. }
  33. //以下是create函数的实现过程
  34. struct student *creat()
  35. {
  36.         //定义一个student结构体的指针变量head用来指向链表的首个地址
  37.     struct student *head;
  38.     //定义一个student结构体的指针的临时变量 用来传送地址
  39.     struct student *p1, *p2;
  40.         //将定义的指针变量初始化设置成LEN大小,这里函数名有误
  41.     p1 = p2 = (struct student *)mall(LEN);    //LEN是结构体大小;
  42.     //打印指示
  43.     printf("Please enter the Nun: ");
  44.     //对学号进行赋值
  45.     scanf("%d", &p1->num);
  46.     //打印指示
  47.     printf("Please enter the Score: ");
  48.     //对分数进行赋值
  49.     scanf("%f", &p1->score);
  50.         //将指向链表首地址的变量初始化
  51.     head = NULL;
  52.     //n 用来计数的,保存学生总数
  53.     n = 0;
  54.         //只要学号不为0 则重复赋值
  55.     while(p1->num)
  56.     {
  57.             //赋值前,学生总数加1
  58.         n++;
  59.         //通过学生总数来判断链表是否为空
  60.         if(1 == n)
  61.         {
  62.                 //如果是空,直接将正在输入的指针赋给头指针
  63.             head = p1;
  64.         }
  65.         else
  66.         {
  67.                 //从这里往下逻辑有点乱

  68.                         
  69.             p2->next = p1;
  70.         }
  71.                 //p1的值赋给p2 ,此时p1把p2又复盖了?
  72.         p2 = p1;
  73.         p1 = (struct student *)malloc(LEN);
  74.         printf("Please enter the Nun: ");
  75.         scanf("%d", &p1->num);
  76.         printf("Please enter the Score: ");
  77.         scanf("%f", &p1->score);
  78.     }
  79.     p2->next = NULL;
  80.     return head;
  81. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2012-2-18 00:38:04 | 显示全部楼层
好像蛮简单的吧
你想想每次申请一个结构体大小
结构体里有指向本身的结构体指针
让这些申请的空间可以知道一个地址就可以找到全部
每个结构体指向下一个结构体内存的地址不就行了么
代码实现就是这样
p2->next = p1;

p2是上一个地址
p1是当前地址
上一个的下一个指向当前
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-12 06:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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