鱼C论坛

 找回密码
 立即注册
查看: 3716|回复: 6

谁来详解一下这个动态输入链表,越详细越好!

[复制链接]
发表于 2013-10-4 14:03:00 | 显示全部楼层 |阅读模式
20鱼币
#include "stdio.h"
#include "malloc.h"
#include "stdlib.h"
#define LEN sizeof(struct student)
struct student *creat();
void print (struct student *head);
struct student
{
int num;
float score;
struct student *next;
};
int n;
void main()
{
struct student *stu;
stu = creat();
print(stu);
printf("\n\n");
system("pause");
}
struct student *creat()
{
struct student *head;
struct student *p1,*p2;
p1=p2=(struct student *)malloc(LEN);
printf("please enter the num:");
scanf("%d",&p1->num);
printf("please enter the score:");
scanf("%f",&p1->score);
head = NULL;
n=0;
while (p1->num)
{
  n++;
  if(1==n)
  {
   head = p1;
  }
  else
  {
   p2->next=p1;
  }
  p2=p1;
  p1=(struct student *)malloc(LEN);
  printf("please enter the num:");
  scanf("%d",&p1->num);
  printf("please enter the score:");
  scanf("%f",&p1->score);
}
p2->next=NULL;
return head;
}
void print(struct student *head)
{
struct student *p;
printf("\nthere are %d records!\n\n",n);
p=head;
if (head)
{
  do
  {
   printf("学号为%d的成绩是:%f\n",p->num,p->score);
   p=p->next;
  }while (p);
}
}


虽然有点多,但我知道有人能轻松解决的!
各位大神,拜托了,最后可以每行都解释一下:lol:
printf和scanf就不用说了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-10-4 14:03:01 | 显示全部楼层
  1. #include "stdio.h"//包含三个头文件
  2. #include "malloc.h"
  3. #include "stdlib.h"
  4. #define LEN sizeof(struct student)//宏定义 LEN 为 sizeof(struct student)
  5. struct student *creat();//creat函数前置声明
  6. void print (struct student *head);//print函数前置声明
  7. struct student//定义类型
  8. {
  9.         int num;
  10.         float score;
  11.         struct student *next;
  12. };
  13. int n;//定义全局变量
  14. void main()
  15. {
  16.         struct student *stu;//定义存放student类型的指针
  17.         stu = creat(); //stu = 链表的头指针
  18.         print(stu); //用print函数输出链表
  19.         printf("\n\n");//两换行符
  20.         system("pause");//中断
  21. }
  22. struct student *creat()
  23. {
  24.         struct student *head;//定义存放student类型的指针
  25.         struct student *p1,*p2;
  26.         p1=p2=(struct student *)malloc(LEN);//把p1 p2 都指向malloc所分配的动态内存的地址
  27.         printf("please enter the num:");//接受输入
  28.         scanf("%d",&p1->num);
  29.         printf("please enter the score:");
  30.         scanf("%f",&p1->score);
  31.         head = NULL;//头指针指向空 也就是说这是个空链表
  32.         n=0;//讲全局变量赋值成0
  33.         while (p1->num)//只要p1->num 不等于0 就执行循环 也就是说上面的接受输入处 看你输入的num 是不是0
  34.                            //不是0就执行循环
  35.         {
  36.                 n++;//n = n + 1  计算有多少组数据
  37.                 if(1==n)//如果 n == 1 也就是说是第一次输入 之前那次如果输入不是0 进入循环的话那次也是算的
  38.                             //数据在那时候已经存入 n++  得到现在有一组数据了
  39.                 {
  40.                         head = p1; //让头指针指向我们动态分配的地址 为什么不用我说了吧除非你连链表是什么
  41.                                    //都不知道
  42.                 }
  43.                 else //如果不是第一次输入
  44.                 {
  45.                         p2->next=p1;//头指针当然不能变了 变得就是next这个指针 原因 也不多说了 除非你不知道什么
  46.                                     //是链表
  47.                 }
  48.                 p2=p1;//赋值语句不解释
  49.                 p1=(struct student *)malloc(LEN);//再次动态分配内存 接受输入 继续判断是否进入循环
  50.                 printf("please enter the num:");
  51.                 scanf("%d",&p1->num);
  52.                 printf("please enter the score:");
  53.                 scanf("%f",&p1->score);
  54.         }
  55.         p2->next=NULL;//结束后 应为p1->next指向的是那个输入num是0的内存 但由于不在进入循环 p2->
  56.                       //next并没有指向那块内存而是指向了最后一块输入num不是0的内存是正确的 把p2->
  57.                       //next 指向NULL 这样链表就完成了
  58.         return head;//把头指针返回即可 获得整条链表的数据
  59. }
  60. void print(struct student *head)
  61. {
  62.         struct student *p;
  63.         printf("\nthere are %d records!\n\n",n);//输出有多少个数据 用全局变量n计算
  64.         p=head;
  65.         if (head)//只要head不是NULL 也就是说不是空链表
  66.         {
  67.                 do
  68.                 {
  69.                         printf("学号为%d的成绩是:%f\n",p->num,p->score);//输出p指向数据 第一次就是头指针指向的
  70.                                                                         //之后 p=p->next;//把p指向下一个数据
  71.                         p=p->next;//把p指向下一个数据
  72.                 }while (p);//只要p 不是NULL 就是说只要不是最后一个数据就继续循环输出
  73.         }
  74. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-10-4 14:33:45 | 显示全部楼层
打了这么多字了 给最佳把 很累的
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-10-4 15:00:15 | 显示全部楼层

辛苦了,最佳答案必须是你的。
我就一菜鸟,看了你的答案还是是懂非懂的。
不过第48行p2=p1;有什么作用?
:dizzy:
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-10-4 16:06:18 | 显示全部楼层
tjf 发表于 2013-10-4 15:00
辛苦了,最佳答案必须是你的。
我就一菜鸟,看了你的答案还是是懂非懂的。
不过第48行p2=p1;有什么作用 ...

不好意思 刚刚出去了 要让p2也更上啊 最后是吧p2的next指向NULL啊
不跟上的话那中间的数据不就全都没了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-10-5 10:24:09 | 显示全部楼层
学习下,大鱼就是大鱼
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-10-5 13:59:53 | 显示全部楼层
牡丹花下死做鬼 发表于 2013-10-4 16:06
不好意思 刚刚出去了 要让p2也更上啊 最后是吧p2的next指向NULL啊
不跟上的话那中间的数据不就全都没了

原来如此,懂了,谢谢了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-12 19:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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