鱼C论坛

 找回密码
 立即注册
查看: 3458|回复: 2

关于谭浩强那本书动态链表的综合操作的错误原因

[复制链接]
发表于 2012-8-3 12:05:39 | 显示全部楼层 |阅读模式

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

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

x
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define LEN sizeof(struct student)

  4. struct student
  5. {
  6.     long num;
  7.     float score;
  8.     struct student *next;
  9. };//after define the struct type ,you must take leave a ";"

  10. int n;

  11. struct student *create(void)// Here struct student could be looked as int, float and...
  12.                             //so the returned  value is the struct student type
  13. {
  14.     struct student *p1, *p2, *head;
  15.     n = 0;
  16.     p1 = p2 = (struct student *) malloc(LEN);
  17.     scanf("%ld %f", &p1->num, &p1->score);
  18.     head = NULL;
  19.     while(p1->num != 0)
  20.     {
  21.         n = n+1;
  22.         if(n == 1)
  23.         {
  24.             head = p1;
  25.         }
  26.         else
  27.         {
  28.             p2->next = p1;//This is the key algorithm 1
  29.         }
  30.         p2 = p1;//                                    2
  31.         p1 = (struct student *) malloc(LEN);//         3
  32.         scanf("%ld %f", &p1->num, &p1->score);
  33.     }
  34.     p2->next = NULL;
  35.     return head;
  36. }

  37. void print(struct student *head)
  38. {
  39.     struct student *p;
  40.     printf("\nNow, These %d records are:\n", n);
  41.     p =head;
  42.     if(head != NULL)
  43.     {
  44.         do
  45.         {
  46.             printf("%ld %8.1f\n", p->num, p->score);
  47.             p = p->next;
  48.         }while(p != NULL);
  49.     }
  50. }

  51. struct student *del(struct student *head, long num)
  52. {
  53.     struct student *p1, *p2;
  54.     if(head == NULL)
  55.     {
  56.         printf("\nlist null!\n");
  57.         return head;
  58.     }
  59.     p1 =head;
  60.     while(num != p1->num && p1->next != NULL)
  61.     {
  62.         p2 = p1;
  63.         p1 = p1->next;
  64.     }
  65.     if(num == p1->num)
  66.     {
  67.         if(p1 == head)
  68.         {
  69.             head = p1->next;
  70.         }
  71.         else
  72.         {
  73.             p2->next = p1->next;
  74.         }
  75.         printf("delete:%ld\n", num);
  76.         n = n-1;
  77.     }
  78.     else
  79.     {
  80.         printf("%ld not been found!\n", num);
  81.     }
  82.     return head;
  83. }

  84. struct student *insert(struct student *head, struct student *stud)
  85. {
  86.     struct student *p0, *p1, *p2;
  87.     p1 = head;
  88.     p0 = stud;
  89.     if(head == NULL)
  90.     {
  91.         head = p0;
  92.         p0->next = NULL;
  93.     }
  94.     else
  95.     {
  96.         while((p0->num > p1->num) && (p1->next != NULL))
  97.         {
  98.             p2 = p1;
  99.             p1 = p1->next;
  100.         }
  101.         if(p0->num <= p1->num)
  102.         {
  103.             if(head == p1)
  104.             {
  105.                 head = p0;
  106.             }
  107.             else
  108.             {
  109.                 p2->next = p0;
  110.             }
  111.             p0->next = p1;
  112.         }
  113.         else
  114.         {
  115.             p1->next = p0;
  116.             p0->next = NULL;
  117.         }
  118.         n = n+1;
  119.         return head;
  120.     }
  121. }

  122. int main()
  123. {
  124.     struct student *head, *stu;
  125.     long del_num;
  126.     printf("intput record:\n");
  127.     head = creat();
  128.     print(head);
  129.     printf("\ninput the deleted number: ");
  130.     scanf("%ld", &del_num);
  131.     while(del_num != 0)
  132.     {
  133.         head = del(head, del_num);
  134.         print(head);
  135.         printf("intput the deleted number:");
  136.         scanf("%ld", &del_num);
  137.     }
  138.     printf("input the inseted record: ");
  139.     stu = (struct student *) malloc(LEN);
  140.     scanf("%ld %f", &stu->num, &stu->score);
  141.     while(stu->num != 0)
  142.     {
  143.         head = insert(head, stu);
  144.         print(head);
  145.         printf("input the insert record:");
  146.         stu = (struct student *) malloc(LEN);
  147.         scanf("%ld %f", &stu->num, &stu->score);
  148.     }
  149.     return 0;
  150. }
复制代码
我电脑的系统的Win7的,此程序基本上全是按照书上敲的。

能运行 不过当出来黑框的时候出现了程序停止工作。。。
不知道错误原因啊,求大神!!!!
未命名.jpg
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-8-3 16:02:14 | 显示全部楼层
  1. #include <stdio.h>

  2. #include <stdlib.h>

  3. #define LEN sizeof(struct student)


  4. struct student

  5. {
  6.        
  7.     long num;
  8.        
  9.     float score;
  10.        
  11.     struct student *next;
  12.        
  13. };//after define the struct type ,you must take leave a ";"


  14. int n;


  15. struct student *creat(void)                //函数名与下面调用的不一致 你写成了  create



  16. {
  17.        
  18.     struct student *p1, *p2, *head;
  19.        
  20.     n = 0;
  21.        
  22.     p1 = p2 = (struct student *) malloc(LEN);
  23.        
  24.     scanf("%ld %f", &p1->num, &p1->score);
  25.        
  26.     head = NULL;
  27.        
  28.     while(p1->num != 0)
  29.                
  30.     {
  31.                
  32.         n = n+1;
  33.                
  34.         if(n == 1)
  35.                        
  36.         {
  37.                        
  38.             head = p1;
  39.                        
  40.         }
  41.                
  42.         else
  43.                        
  44.         {
  45.                        
  46.             p2->next = p1;//This is the key algorithm 1
  47.                        
  48.         }
  49.                
  50.         p2 = p1;//                                    2
  51.                
  52.         p1 = (struct student *) malloc(LEN);//         3
  53.                
  54.         scanf("%ld %f", &p1->num, &p1->score);
  55.                
  56.     }
  57.        
  58.     p2->next = NULL;
  59.        
  60.     return head;
  61.        
  62. }


  63. void print(struct student *head)

  64. {
  65.        
  66.     struct student *p;
  67.        
  68.     printf("\nNow, These %d records are:\n", n);
  69.        
  70.     p =head;
  71.        
  72.     if(head != NULL)
  73.                
  74.     {
  75.                
  76.         do
  77.                
  78.         {
  79.                        
  80.             printf("%ld %8.1f\n", p->num, p->score);
  81.                        
  82.             p = p->next;
  83.                        
  84.         }while(p != NULL);
  85.                
  86.     }
  87.        
  88. }


  89. struct student *del(struct student *head, long num)

  90. {
  91.        
  92.     struct student *p1, *p2;
  93.        
  94.     if(head == NULL)
  95.                
  96.     {
  97.                
  98.         printf("\nlist null!\n");
  99.                
  100.         return head;
  101.                
  102.     }
  103.        
  104.     p1 =head;
  105.        
  106.     while(num != p1->num && p1->next != NULL)
  107.                
  108.     {
  109.                
  110.         p2 = p1;
  111.                
  112.         p1 = p1->next;
  113.                
  114.     }
  115.        
  116.     if(num == p1->num)
  117.                
  118.     {
  119.                
  120.         if(p1 == head)
  121.                        
  122.         {
  123.                        
  124.             head = p1->next;
  125.                        
  126.         }
  127.                
  128.         else
  129.                        
  130.         {
  131.                        
  132.             p2->next = p1->next;
  133.                        
  134.         }
  135.                
  136.         printf("delete:%ld\n", num);
  137.                
  138.         n = n-1;
  139.                
  140.     }
  141.        
  142.     else
  143.                
  144.     {
  145.                
  146.         printf("%ld not been found!\n", num);
  147.                
  148.     }
  149.        
  150.     return head;
  151.        
  152. }


  153. struct student *insert(struct student *head, struct student *stud)

  154. {
  155.        
  156.     struct student *p0, *p1, *p2;
  157.        
  158.     p1 = head;
  159.        
  160.     p0 = stud;
  161.        
  162.     if(head == NULL)
  163.                
  164.     {
  165.                
  166.         head = p0;
  167.                
  168.         p0->next = NULL;
  169.                
  170.     }
  171.        
  172.     else
  173.                
  174.     {
  175.                
  176.         while((p0->num > p1->num) && (p1->next != NULL))
  177.                        
  178.         {
  179.                        
  180.             p2 = p1;
  181.                        
  182.             p1 = p1->next;
  183.                        
  184.         }
  185.                
  186.         if(p0->num <= p1->num)
  187.                        
  188.         {
  189.                        
  190.             if(head == p1)
  191.                                
  192.             {
  193.                                
  194.                 head = p0;
  195.                                
  196.             }
  197.                        
  198.             else
  199.                                
  200.             {
  201.                                
  202.                 p2->next = p0;
  203.                                
  204.             }
  205.                        
  206.             p0->next = p1;
  207.                        
  208.         }
  209.                
  210.         else
  211.                        
  212.         {
  213.                        
  214.             p1->next = p0;
  215.                        
  216.             p0->next = NULL;
  217.                        
  218.         }
  219.                
  220.         n = n+1;
  221.     }
  222.         return head;                //函数返回的时候 注意是在整个函数最后返回
  223. }


  224. int main()

  225. {
  226.        
  227.     struct student *head, *stu;
  228.        
  229.     long del_num;
  230.        
  231.     printf("intput record:\n");
  232.        
  233.     head = creat();
  234.        
  235.     print(head);
  236.        
  237.     printf("\ninput the deleted number: ");
  238.        
  239.     scanf("%ld", &del_num);
  240.        
  241.     while(del_num != 0)
  242.                
  243.     {
  244.                
  245.         head = del(head, del_num);
  246.                
  247.         print(head);
  248.                
  249.         printf("intput the deleted number:");
  250.                
  251.         scanf("%ld", &del_num);
  252.                
  253.     }
  254.        
  255.     printf("input the inseted record: ");
  256.        
  257.     stu = (struct student *) malloc(LEN);
  258.        
  259.     scanf("%ld %f", &stu->num, &stu->score);
  260.        
  261.     while(stu->num != 0)
  262.                
  263.     {
  264.                
  265.         head = insert(head, stu);
  266.                
  267.         print(head);
  268.                
  269.         printf("input the insert record:");
  270.                
  271.         stu = (struct student *) malloc(LEN);
  272.                
  273.         scanf("%ld %f", &stu->num, &stu->score);
  274.                
  275.     }
  276.        
  277.     return 0;
  278.        
  279. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2012-8-3 18:40:28 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-9-28 03:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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