鱼C论坛

 找回密码
 立即注册
查看: 2902|回复: 8

链表问题

[复制链接]
发表于 2012-2-21 14:35:41 | 显示全部楼层 |阅读模式
2鱼币
为什么insert插入结点,插入在头结点位置会有问题?求解:


  1. /*****************************************/
  2. /***********简单的数据链表程序************/
  3. /*****************************************/
  4. #include <stdio.h>
  5. #include <conio.h>
  6. #include <malloc.h>
  7. #define LEA sizeof(struct student)
  8. struct student
  9. {
  10. int num;
  11. float score;
  12. struct student *next;
  13. };
  14. struct student *expror();             //创建链表;
  15. void print(struct student *head);           //打印链表;
  16. struct student *del(struct student *head, int num);       //删除结点;
  17. struct student *insert(struct student *head, struct student *p);   //插入结点;
  18. void main()
  19. {
  20. struct student *p, *p1, *Max;
  21. int n = 0;

  22. p = expror();
  23. print(p);

  24. printf("Please input Number delete: ");
  25. scanf("%d", &n);
  26. print(del(p, n));

  27. p1 = (struct student *)malloc(LEA);
  28. printf("Please input Number insert: ");
  29. scanf("%d", &p1->num);
  30. printf("Please input Score insert: ");
  31. scanf("%f", &p1->score);
  32. Max = insert(p, p1);
  33. print(Max);
  34. getch();
  35. }
  36. struct student *expror()
  37. {
  38. struct student *p1, *p2, *head;
  39. int n = 0;
  40. head = NULL;

  41. p1 = p2 = (struct student *)malloc(LEA);
  42. printf("Please input Number: ");
  43. scanf("%d", &p1->num);
  44. printf("Please input Score: ");
  45. scanf("%f", &p1->score);

  46. while(p1->num)
  47. {
  48.   n++;
  49.   if(1 == n)
  50.   {
  51.    head = p1;
  52.   }
  53.   else
  54.   {
  55.    p2->next = p1;
  56.    p2 = p1;
  57.   }
  58.   p1 = (struct student *)malloc(LEA);
  59.   printf("Please input Number: ");
  60.   scanf("%d", &p1->num);
  61.   printf("Please input Score: ");
  62.   scanf("%f", &p1->score);
  63. }
  64. p2->next = NULL;
  65. return head;
  66. }
  67. void print(struct student *head)
  68. {
  69. while(head != NULL)
  70. {
  71.   printf("Number = %d\t\tScore = %f\n", head->num, head->score);
  72.   head = head->next;
  73. }
  74. }
  75. struct student *del(struct student *head, int num)
  76. {
  77. struct student *p1, *p2;
  78. p1 = head;
  79. if(head == NULL)
  80. {
  81.   printf("Fuck you!!!");
  82. }
  83. else
  84. {
  85.   while(p1->next != NULL && p1->num != num)
  86.   {
  87.    p2 = p1;
  88.    p1 = p1->next;
  89.   }
  90.   if(p1->num == num)
  91.   {
  92.    if(head == p1)
  93.    {
  94.     head = p1->next;
  95.    }
  96.    else
  97.    {
  98.     p2->next = p1->next;
  99.    }
  100.   }
  101. }
  102. return head;
  103. }
  104. struct student *insert(struct student *head, struct student *p)
  105. {
  106. struct student *p0, *p1, *p2;
  107. p1 = head;
  108. p0 = p;
  109. if(head == NULL)
  110. {
  111.   printf("Fuck you!!!");
  112. }
  113. else
  114. {
  115.   while(p0->num > p1->num && p1->next != NULL)
  116.   {
  117.    p2 = p1;
  118.    p1 = p1->next;
  119.   }
  120.   if(p0->num < p1->num)
  121.   {
  122.    if(head == p1)
  123.    {
  124.     head = p0;
  125.     p0->next = p1;
  126.    }
  127.    else
  128.    {
  129.     p2->next = p0;
  130.     p0->next = p1;
  131.    }
  132.   }
  133.   else
  134.   {
  135.    p1->next = p0;
  136.    p0->next = NULL;
  137.   }
  138. }
  139. return head;
  140. }
复制代码

最佳答案

查看完整内容

插到头结点后 你要把头结点改为新结点
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-2-21 14:35:42 | 显示全部楼层
插到头结点后 你要把头结点改为新结点
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-2-22 19:04:04 | 显示全部楼层
非常有问题。。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-2-22 19:37:40 | 显示全部楼层
student里的num是指的什么?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-9-8 11:36:32 | 显示全部楼层
谢谢分享,非常喜欢!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-9-21 22:50:01 | 显示全部楼层
写的很不错,谢谢分享
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-9-21 23:03:01 | 显示全部楼层
代码好乱...
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-9-21 23:07:21 | 显示全部楼层
代码加点注释啦,:mad:
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-9-22 16:37:55 | 显示全部楼层
把错误信息贴出来看看吧
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-14 17:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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