谓我心忧 发表于 2014-5-17 16:55:43

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

代码如下:
/*链表练习*/
#include <stdio.h>
#include <stdlib.h>                //提供malloc()原型
#include <string.h>                //提供strcpy()原型
#define TSIZE 45

struct film
{
        char title;
        int rating;
        film *next;
};                                                //忘记这个分号会死的很惨

int main()
{
        struct film *head;
        struct film *prev, *current;
        char input;
       
        puts("Enter first movie title: ");
        while(gets(input) != NULL && input != '\0')
        {
                current = (struct film*)malloc(sizeof(struct film));
                if(head == NULL)
                        head = current;
                else
                        prev->next = current;
                current->next = NULL;
                strcpy(current->title, input);
                puts("Enter your rating <0~10>: ");
                scanf("%d", ¤t->rating);
                while(getchar() != '\n')
                        continue;
                puts("Enter next movie title (empty line to stop): ");
                prev = current;
        }
       
        /*给出电影列表*/
       
        if(head == NULL)
                printf("No data entered");
        else
                printf("Here is the movie list: \n");
        current = head;
        while(current != NULL)
        {
                printf("Movie: %sRating: %d\n", current->title, current->rating);
                current = current->next;
        }
       
        /*任务已完成,因此释放所分配的内存*/
       
        current = head;
        while(current != NULL)
        {
                free(current);
                current = current->next;
        }
       
        printf("BYE BYE");
       
        return 0;
}
没发现逻辑上有什么问题,但是最后链表无法正常输出,求大神解答,不胜感激.

HHR 发表于 2014-5-17 16:55:44

/*链表练习*/
#include <stdio.h>
#include <stdlib.h>                //提供malloc()原型
#include <string.h>                //提供strcpy()原型
#define TSIZE 45

struct film
{
        char title;
        int rating;
        film *next;
};                                                //忘记这个分号会死的很惨

int main()
{
        struct film *head = NULL;
        struct film *prev = NULL, *current = NULL;
        char input;
       
        puts("Enter first movie title: ");
        while(gets(input) != NULL && input != '\0')
        {
                current = (struct film*)malloc(sizeof(struct film));
                if(head == NULL)
                        head = current;
                else
                        prev->next = current;
                current->next = NULL;

                strcpy(current->title, input);

                puts("Enter your rating <0~10>: ");
        //        scanf("%d", ¤t->rating);
                scanf("%d", &current->rating);
               
                while(getchar() != '\n')
                        continue;
                puts("Enter next movie title (empty line to stop): ");
                prev = current;
        }
       
        /*给出电影列表*/
       
        if(head == NULL)
                printf("No data entered");
        else
                printf("Here is the movie list: \n");
        current = head;
        while(current != NULL)
        {
                printf("Movie: %sRating: %d\n", current->title, current->rating);
                current = current->next;
        }
       
        /*任务已完成,因此释放所分配的内存*/

        current = head;
        while(current != NULL)
        {
                /*
                free(current);          // free 之后, 索引不到 ->next
                current = current->next;
                */
                prev = current ->next;
                free( current );
                current = prev;
        }

        printf("BYE BYE");
       
        return 0;
}

oggplay 发表于 2014-5-17 17:22:34

本帖最后由 oggplay 于 2014-5-17 17:27 编辑

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

友情提示:尽量用fgets()函数替代gets()

牡丹花下死做鬼 发表于 2014-5-17 20:07:05

scanf("%d", ¤t->rating);目测这不对吧
页: [1]
查看完整版本: 求助,这个链表程序哪里错了...