鱼C论坛

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

关于建立动态链表的问题

[复制链接]
发表于 2015-3-15 23:47:37 | 显示全部楼层 |阅读模式

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

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

x
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <stdlib.h>
  4. #define LEN sizeof(struct student)
  5. struct student
  6. {
  7.         int num;
  8.         float score;
  9.         struct student *next;
  10. };
  11. int n;

  12. int main(void)
  13. {
  14.        
  15.         struct student *creat(void);             //声明建立链表的函数
  16.         void print(struct student *);           //声明输出链表的函数
  17.         struct student *head;
  18.         head = creat();                           //调用create函数
  19.         print(head);                             //调用print函数
  20.         system("pause");
  21.         return 0;
  22. }

  23. struct student *creat(void)               //定义建立链表的函数
  24. {
  25.         struct student *head = NULL, *p1 = NULL, *p2 = NULL;
  26.         p1 = p2 = (struct student *)malloc(LEN);
  27.         printf("please input the num : ");
  28.         scanf("%d", &(p1 -> num) );
  29.         printf("please input score : ");
  30.         scanf("%f", &(p1 -> score));
  31.         n = 0;
  32.         while (0 != p1 -> num)
  33.         {
  34.                 n++;
  35.                 if (1 == n)
  36.                 {
  37.                         head = p1;
  38.                 }
  39.                 else
  40.                 {
  41.                         p2->next = p1;
  42.                 }
  43.                         p2 = p1;
  44.                         p1 = (struct student *)malloc(LEN);
  45.                         printf("please input the num : ");
  46.                         scanf("%d", &(p1->num));
  47.                         printf("please input score : ");
  48.                         scanf("%f", &(p1->score));       
  49.         }
  50.         p2->next = NULL;
  51.         return head;
  52. }

  53. void print(struct student *head)             //定义输出链表的函数
  54. {
  55.         struct student *p;
  56.         p = head;
  57.         while (0 != p -> num)
  58.         {
  59.                 printf("成员%d的分数为%f\n", p -> num, p -> score);
  60.                 p = p->next;
  61.         }
  62. }
复制代码

先放上代码


每次输出完数据后,都会跳出,是哪里出问题了吗?

跳出该对话框

跳出该对话框
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2015-3-16 10:20:09 | 显示全部楼层
没人回答吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-19 11:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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