鱼C论坛

 找回密码
 立即注册
查看: 1476|回复: 5

动态创建链表哪儿错了?

[复制链接]
发表于 2015-5-25 10:39:17 | 显示全部楼层 |阅读模式
18鱼币
       下面是本人编写的一个动态创建链表的程序,VC6.0编译时没有错,链接时也没有错。但是运行时就会弹出出错窗口,提示“忽视”“中止”或“取消”。不知这是为什么,就是找不到错误!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define LEN sizeof(struct student)

  4. int n=0;
  5. struct student
  6. {
  7. long num;
  8. float score;
  9. struct student *next;
  10. };

  11. int main()
  12. {
  13. struct student *head,*p1,*p2;
  14. head=NULL;

  15. p1=p2=(struct student *)malloc(sizeof(LEN));
  16. printf("Please input num: ");
  17. scanf("%ld",&p1->num);
  18. printf("Please input score: ");
  19. scanf("%f",&p1->score);

  20. while(p1->num!=0)
  21. {
  22. n=n+1;

  23. if(n==1)
  24. {
  25. head=p1;
  26. }

  27. else
  28. {
  29. p2->next=p1;
  30. }

  31. p2=p1;

  32. p1=(struct student *)malloc(sizeof(struct student));
  33. printf("Please input num: ");
  34. scanf("%ld",&p1->num);
  35. printf("Please input score: ");
  36. scanf("%f",&p1->score);

  37. }
  38. p2->next=NULL;
  39. return 0;
  40. }
复制代码



最佳答案

查看完整内容

#define LEN sizeof(struct student) p1=p2=(struct student *)malloc(sizeof(LEN));这句就相当于 p1=p2=(struct student *)malloc(sizeof(sizeof(struct student)));也就相当于 p1=p2=(struct student *)malloc(sizeof(4));也就相当于 p1=p2=(struct student *)malloc(4);//因为4是int,所以sizeof(4)就是4
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-5-25 10:39:18 | 显示全部楼层
#define LEN sizeof(struct student)
p1=p2=(struct student *)malloc(sizeof(LEN));这句就相当于
p1=p2=(struct student *)malloc(sizeof(sizeof(struct student)));也就相当于
p1=p2=(struct student *)malloc(sizeof(4));也就相当于
p1=p2=(struct student *)malloc(4);//因为4是int,所以sizeof(4)就是4
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-5-25 13:38:50 | 显示全部楼层
scanf("%f",&p->data.score);///////////问题在这里

这是由于scanf的一个bug造成,
在前面一行加入float pp=0;
使得scanf使用浮点数前先初始化过浮点数就OK.


这是 网上 找的 类似错误 楼主可以参考下哦
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-5-25 15:38:22 | 显示全部楼层

RE: 动态创建链表哪儿错了?

本帖最后由 ryxcaixia 于 2015-5-25 16:03 编辑


  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. struct student
  4. {
  5.         long num;
  6.         float score;
  7.         struct student *next;
  8. };

  9. // 链表输出函数
  10. void OutputLinklist(student* phead)
  11. {
  12.         while (phead->next != NULL)
  13.         {
  14.                 phead = phead->next;
  15.                 printf("num is %ld\n", phead->num);
  16.                 printf("scord is %f\n\n", phead->score);               
  17.         }

  18. }

  19. int main()
  20. {
  21.         // 头结点一般为空, 不放任何东西, 头结点的下一结点才会第一个数据结点
  22.         student *head = (student *)malloc(sizeof(student));
  23.         student* p2 = head;

  24.         int n = 0;
  25.         scanf("%d", &n);
  26.         while(n--)
  27.         {
  28.                 // 申请新结点并赋值
  29.                 student* p1=(struct student *)malloc(sizeof(struct student));
  30.                 printf("Please input num: ");
  31.                 scanf("%ld", &p1->num);
  32.                 printf("Please input score: ");
  33.                 scanf("%f", &p1->score);
  34.                 p1->next = NULL;

  35.                 // 前驱节点指向后继结点
  36.                 p2->next = p1;
  37.                 p2 = p1;
  38.         }  

  39.         OutputLinklist(head);

  40.         return 0;
  41. }
复制代码


楼主同学 我对于逻辑和层次 重写了下 这种表达链表可能更清晰些
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-5-25 21:37:52 | 显示全部楼层
#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct student)

int n=0;

//学生结构体(学生号,成绩)
struct student
{
long num;
float score;
struct student *next;
};

// 链表输出函数
void OutputLinklist(student* phead)
{
        while (phead)
        {
               
                printf("num is %ld\n", phead->num);
                printf("scord is %f\n\n", phead->score);   
                                phead = phead->next;            
        }
      

}

int main()
{
struct student *head,*p1,*p2;//*p2是表尾
head=NULL;

p1=p2=(struct student *)malloc(LEN);

//*p1结点输入
printf("Please input num: ");
scanf("%ld",&p1->num);
printf("Please input score: ");
scanf("%f",&p1->score);

while(p1->num)//以num=0作结束标志
{
n=n+1;

if(n==1)
{
head=p1;
}

else
{
p2->next=p1;
p2=p1;
}



//结点输入
p1=(struct student *)malloc(LEN);
printf("Please input num: ");
scanf("%ld",&p1->num);
printf("Please input score: ");
scanf("%f",&p1->score);

}
p2->next=NULL;
   OutputLinklist(head);
free(p1) ;
free(p2) ;
return 0;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-5-31 20:31:05 | 显示全部楼层
同意三楼
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-20 03:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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