鱼C论坛

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

链表代码的错误,不知在那里,帮忙看看

[复制链接]
发表于 2011-8-6 11:19:59 | 显示全部楼层 |阅读模式

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

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

x

  1. 这段代码我运行了一下,创建链表和第一次打印链表没有问题,但是运行到  删除链表  就弹出了对话框提示  程序终止了,帮忙看看。谢谢
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #define LEN struct student
  6. struct student{

  7. int num;
  8. float score;
  9. struct student *next;

  10. };
  11. int n = 0;
  12. struct student *p1,*p2,*head;
  13. void main()
  14. {


  15. void print(struct student *);
  16. void dele(struct student *);
  17. p1= (struct student *)malloc(sizeof(LEN));

  18. head = p1;
  19. printf("请输入学生的数据:\n");
  20. scanf("%d",&p1->num);
  21. printf("score\n");
  22. scanf("%f",&p1->score);
  23. p1->next = NULL;
  24. while (p1->num)
  25. {
  26. n++;
  27. p2 = p1;
  28. p1 = (struct student *)malloc(sizeof(LEN));
  29. p2->next = p1;

  30. printf("请输入学生的数据:\n");
  31. scanf("%d",&p1->num);
  32. printf("score\n");
  33. scanf("%f",&p1->score);

  34. }
  35. p2->next = NULL;
  36. print(head);
  37. dele(head);
  38. //print(head);



  39. }
  40. void print(struct student *head)
  41. {
  42. int i ;
  43. for (i = 1; i<=n;i++)
  44. {
  45. printf("num: %d \t",head->num);
  46. printf("score:%f \t",head->score);
  47. head = head->next;
  48. if (i%2==0)
  49. printf("\n");

  50. }

  51. }
  52. void dele(struct student *p)
  53. {
  54. int find,boo=0,i;
  55. printf("please inpput the num\n");
  56. scanf("%d",&find);
  57. p1=p;

  58. for (i=1;i<=n;i++)
  59. {
  60. if(strcmp(find,p1->num)==0 && i==1)
  61. {
  62. head=p1->next;
  63. p1->next=NULL;
  64. boo=1;
  65. }
  66. if(strcmp(find,p1->num)==0 && i>1)
  67. {
  68. p2->next=p1->next;
  69. p1->next=NULL;
  70. boo=1;
  71. }
  72. else
  73. {

  74. p2=p1;
  75. p1=p1->next;
  76. }


  77. }
  78. if(boo==0)
  79. {
  80. printf("sorry,there is no data of your num\n");
  81. }


  82. }

复制代码

小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2011-8-6 11:21:50 | 显示全部楼层
补充一下    去掉  45行前面的   //      在看代码  
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2011-8-6 11:30:21 | 显示全部楼层
没学C  帮你顶下算了
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2011-8-6 14:28:21 | 显示全部楼层
谢谢 啊
,快来帮帮我吧
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2011-8-6 14:45:17 | 显示全部楼层
我把你那个代码前边稍微改了一下!你后边那个scanf也有错误!你自己调试吧!
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>

  4. typedef struct students{
  5.     int     num;
  6.     float     score;
  7.     struct     students *next;
  8. } st;
  9. int     n = 0;
  10. st         *p1,*p2,*head;

  11. int main(void)
  12. {
  13.     void print(st *);
  14.     void dele(st *);
  15.    
  16.     p1= (st *)malloc(sizeof(st));

  17.     head = p1;
  18.     printf("请输入学生的数据:\n");
  19.     scanf("%d",&p1->num);
  20.     printf("score\n");
  21.     scanf("%f",&p1->score);    /* <<<<<<<<<<---------------*/
  22.     /*p1->next = NULL;
  23.    
  24.     while (p1->num)
  25.     {
  26.         n++;
  27.         p2 = p1;
  28.         p1 = (st *)malloc(sizeof(st));
  29.         p2->next = p1;

  30.         printf("请输入学生的数据:\n");
  31.         scanf("%d",&p1->num);
  32.         printf("score\n");
  33.         scanf("%f",&p1->score);

  34.     }
  35.     p2->next = NULL;
  36.     print(head);
  37.     dele(head);*/
  38.     return 0;
  39. }

  40. /*
  41. void print(st *head)
  42. {
  43.     int i ;
  44.     for (i = 1; i<=n;i++) {
  45.         
  46.         printf("num: %d \t",head->num);
  47.         printf("score:%f \t",head->score);
  48.         head = head->next;
  49.         if (i%2==0)
  50.             printf("\n");
  51.     }

  52. }

  53. void dele(st *p)
  54. {
  55.     int find,boo=0,i;
  56.     printf("please inpput the num\n");
  57.     scanf("%d",&find);
  58.     p1=p;

  59.     for (i=1;i<=n;i++) {
  60.    
  61.         if(strcmp(find,p1->num)==0 && i==1) {
  62.         
  63.             head=p1->next;
  64.             p1->next=NULL;
  65.             boo=1;
  66.         }
  67.         if(strcmp(find,p1->num)==0 && i>1) {
  68.         
  69.             p2->next=p1->next;
  70.             p1->next=NULL;
  71.             boo=1;
  72.         }
  73.         else {

  74.             p2=p1;
  75.             p1=p1->next;
  76.         }
  77.     }
  78.     if(boo==0)
  79.         printf("sorry,there is no data of your num\n");
  80. }
  81. */
复制代码



小甲鱼最新课程 -> https://ilovefishc.com
发表于 2011-8-6 14:52:46 | 显示全部楼层
在啰唆一下!既然是给别人看的代码,有注释还是不较好!
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2011-8-6 15:05:15 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-7-7 15:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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