鱼C论坛

 找回密码
 立即注册
查看: 2410|回复: 3

求助,这个链表程序哪里错了...

[复制链接]
发表于 2014-5-17 16:55:43 | 显示全部楼层 |阅读模式
10鱼币
代码如下:
  1. /*链表练习*/
  2. #include <stdio.h>
  3. #include <stdlib.h>                //提供malloc()原型
  4. #include <string.h>                //提供strcpy()原型
  5. #define TSIZE 45

  6. struct film
  7. {
  8.         char title[TSIZE];
  9.         int rating;
  10.         film *next;
  11. };                                                //忘记这个分号会死的很惨

  12. int main()
  13. {
  14.         struct film *head;
  15.         struct film *prev, *current;
  16.         char input[TSIZE];
  17.        
  18.         puts("Enter first movie title: ");
  19.         while(gets(input) != NULL && input[0] != '\0')
  20.         {
  21.                 current = (struct film*)malloc(sizeof(struct film));
  22.                 if(head == NULL)
  23.                         head = current;
  24.                 else
  25.                         prev->next = current;
  26.                 current->next = NULL;
  27.                 strcpy(current->title, input);
  28.                 puts("Enter your rating <0~10>: ");
  29.                 scanf("%d", ¤t->rating);
  30.                 while(getchar() != '\n')
  31.                         continue;
  32.                 puts("Enter next movie title (empty line to stop): ");
  33.                 prev = current;
  34.         }
  35.        
  36.         /*给出电影列表*/
  37.        
  38.         if(head == NULL)
  39.                 printf("No data entered");
  40.         else
  41.                 printf("Here is the movie list: \n");
  42.         current = head;
  43.         while(current != NULL)
  44.         {
  45.                 printf("Movie: %s  Rating: %d\n", current->title, current->rating);
  46.                 current = current->next;
  47.         }
  48.        
  49.         /*任务已完成,因此释放所分配的内存*/
  50.        
  51.         current = head;
  52.         while(current != NULL)
  53.         {
  54.                 free(current);
  55.                 current = current->next;
  56.         }
  57.        
  58.         printf("BYE BYE");
  59.        
  60.         return 0;
  61. }
复制代码

没发现逻辑上有什么问题,但是最后链表无法正常输出,求大神解答,不胜感激.

最佳答案

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-5-17 16:55:44 | 显示全部楼层
  1. /*链表练习*/
  2. #include <stdio.h>
  3. #include <stdlib.h>                //提供malloc()原型
  4. #include <string.h>                //提供strcpy()原型
  5. #define TSIZE 45

  6. struct film
  7. {
  8.         char title[TSIZE];
  9.         int rating;
  10.         film *next;
  11. };                                                //忘记这个分号会死的很惨

  12. int main()
  13. {
  14.         struct film *head = NULL;
  15.         struct film *prev = NULL, *current = NULL;
  16.         char input[TSIZE];
  17.        
  18.         puts("Enter first movie title: ");
  19.         while(gets(input) != NULL && input[0] != '\0')
  20.         {
  21.                 current = (struct film*)malloc(sizeof(struct film));
  22.                 if(head == NULL)
  23.                         head = current;
  24.                 else
  25.                         prev->next = current;
  26.                 current->next = NULL;

  27.                 strcpy(current->title, input);

  28.                 puts("Enter your rating <0~10>: ");
  29.         //        scanf("%d", ¤t->rating);
  30.                 scanf("%d", &current->rating);
  31.                
  32.                 while(getchar() != '\n')
  33.                         continue;
  34.                 puts("Enter next movie title (empty line to stop): ");
  35.                 prev = current;
  36.         }
  37.        
  38.         /*给出电影列表*/
  39.        
  40.         if(head == NULL)
  41.                 printf("No data entered");
  42.         else
  43.                 printf("Here is the movie list: \n");
  44.         current = head;
  45.         while(current != NULL)
  46.         {
  47.                 printf("Movie: %s  Rating: %d\n", current->title, current->rating);
  48.                 current = current->next;
  49.         }
  50.        
  51.         /*任务已完成,因此释放所分配的内存*/

  52.         current = head;
  53.         while(current != NULL)
  54.         {
  55.                 /*
  56.                 free(current);          // free 之后, 索引不到 ->next
  57.                 current = current->next;
  58.                 */
  59.                 prev = current ->next;
  60.                 free( current );
  61.                 current = prev;
  62.         }

  63.         printf("BYE BYE");
  64.        
  65.         return 0;
  66. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-5-17 17:22:34 | 显示全部楼层
本帖最后由 oggplay 于 2014-5-17 17:27 编辑

看看我的编译器警告(warning)就知道了               也就是你的31行  scanf("%d", current->rating);

友情提示:尽量用fgets()函数替代gets()
抓图33.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-5-17 20:07:05 | 显示全部楼层
  1. scanf("%d", ¤t->rating);
复制代码
目测这不对吧
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 06:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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