在C中比较链表中的值时出现问题

在C中比较链表中的值时出现问题,c,pointers,struct,linked-list,malloc,C,Pointers,Struct,Linked List,Malloc,我正在用C编写一个简单的程序,我们创建一个链表,链表的结构就像电影一样,它存储了电影的名称、制作年份、评级1-5以及指向下一个节点的指针。我们不允许在此结构中添加任何内容或定义函数 除此之外,出于某种原因,我们需要在main的主体中写入整个链接列表,这样就为问题增加了一层意大利面。无论如何,在这个程序中,我们应该让用户输入“U”进行更新,或者输入“S”搜索电影。更新完成了您预期的操作,您可以输入标题、年份和评级。由此,我们应该在链表的末尾插入节点 我们的搜索应该遍历这个链接列表,如果找到匹配项,

我正在用C编写一个简单的程序,我们创建一个链表,链表的结构就像电影一样,它存储了电影的名称、制作年份、评级1-5以及指向下一个节点的指针。我们不允许在此结构中添加任何内容或定义函数

除此之外,出于某种原因,我们需要在main的主体中写入整个链接列表,这样就为问题增加了一层意大利面。无论如何,在这个程序中,我们应该让用户输入“U”进行更新,或者输入“S”搜索电影。更新完成了您预期的操作,您可以输入标题、年份和评级。由此,我们应该在链表的末尾插入节点

我们的搜索应该遍历这个链接列表,如果找到匹配项,应该打印出电影、片名和年份

虽然我的代码的更新部分可以工作,但我似乎无法让搜索正常工作。我正在使用一个名为temp的电影结构,它从head开始,在列表中迭代,试图找到电影。在通过printf运行了一些测试之后,我发现无论我输入什么电影,temp都只是一个空节点

我想这和我给malloc打电话的方式有关?或者与未正确分配节点有关?老实说,我不确定,不幸的是,实验室的助教也不知道出了什么问题

这是我的密码:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct movie_node {
        char title[250];
        int year;
        unsigned char rating;
        struct movie_node *next;
};

typedef struct movie_node movie;

int main() {

        // variables
        int loop = 1;
        movie *head = NULL; // represents first
        movie *current; // represents the last node
        movie *temp; // used for traversing the linked list
        head = malloc(sizeof(movie));
        int amountOfMovies = 0; // increment after each addition
        char choice; // either 'u' (update) or 's' (search)
        // ask user for input
        while(loop) {

                printf("%d Movie(s) in the database. Update or search (U/S): ", amountOfMovies);
                scanf(" %c", &choice);
                /* CHOICE 1, UPDATE */
                if(choice == 'U') {

                        // case 1, head is null, we must create a new node
                        if(head == NULL) {
                                // get input
                                printf("Name of the movie: ");
                                scanf(" %[^\n]%*c", head->title);
                                printf("Year: ");
          scanf("%d", &head->year);
                                printf("Rating: ");
                                scanf("%hhu", &head->rating);
                                head->next = NULL;
                                head = current; // set head to point to current
                        } else {
                                current = head;
                                // need to find where current is
                                while(current != NULL) {
                                        current = current->next;
                                } // end while
                                // current is now at the null position, indicating empty node
                                current = malloc(sizeof(movie)); // allocate mem
                                // get user input
                                printf("Name of the movie: ");
                                scanf(" %[^\n]%*c", current->title);
                                printf("Year: ");
                                scanf("%d", &current->year);
                                printf("Rating: ");
                                scanf("%hhu", &current->rating);
                                current->next = NULL;
                        } // end else
                        // output movie
                        printf("Movie \"%s\" is added to the database.\n", current->title);
                        amountOfMovies++; // increment amount of movies in database
                } else if(choice == 'S') {
                /* CHOICE 2, SEARCH */
                        // temp string
                        char tempTitle[250];
                        // flag to let us know if we found something
                        bool found = false;
                        // temp linked list to traverse
                        temp = head;
                        temp = malloc(sizeof(movie));
                        // ask user for input
                        printf("Name of movie: ");
                        scanf(" %[^\n]%*c", tempTitle);
                        printf("NAME OF MOVIE IN HEAD: %s\n", temp->title);             // test, take out later
                        while(temp != NULL) {
                                printf("NAME OF CURRENT MOVIE TO COMPARE TO: %s\n", temp->title); // test
                                if(strcmp(temp->title, tempTitle) == 0) {
                                        // match
                                        printf("Year: %d\n", temp->year);
                                        printf("Rating: %hhu\n", temp->rating);
                                        found = true;
                                        break;
                                } else { // no match so far
                                        temp = temp->next;
                                        printf("HAVEN'T FOUND MATCH, NEXT TITLE TO CHECK IS: %s\n", temp->title); // test print
                                        found = false;
                                }  // end else
                        } // end while
                        if(found == false) { // no match confirmed
                                printf("Movie \"%s\" does not exist in the database.\n", tempTitle);
                        }
                } else { // choice is invalid
                        loop = 0; // exit
                } // end if-else

        } // end while
        // free all the nodes

        return 0;
}
注意:我唯一没有实现的是释放内存。。我不是百分之百确定我应该如何完成它

非常感谢您的帮助。

问题在于您的malloc呼叫。首先,你要:

movie *head = NULL;
// ...
head = malloc(sizeof(movie));
这意味着head不再为null,您将无法按您想要的方式插入第一部电影-将malloc移动到其他地方

其次,下面几行代码,您可以:

current = head; // <- this is OK
// ...
current = malloc(sizeof(movie)); // allocate mem <- this is NOT OK, for the same reason as before
此外,你们也可以阅读这样的电影标题:scanf%249s,head->title

如果你知道如何从那里开始,请告诉我

除了代码中的问题外,还有一个问题:实验室的助教也不知道出了什么问题

要修复的样本

movie *new_node(void){//aggregate the creation of a new node
    movie *node = malloc(sizeof(*node));//error check omit
    printf("Name of the movie: ");
    scanf(" %249[^\n]%*c", node->title);
    printf("Year: ");
    scanf("%d", &node->year);
    printf("Rating: ");
    scanf("%hhu", &node->rating);
    node->next = NULL;
    return node; 
}

int main() {
    int loop = 1;
    movie *head = NULL; 
    movie *current; 
    movie *temp; 
    //head = malloc(sizeof(movie));//"if(head == NULL) {" don't work 
    int amountOfMovies = 0; 
    char choice; 

    while(loop) {
        printf("%d Movie(s) in the database. Update or search (U/S): ", amountOfMovies);
        scanf(" %c", &choice);

        if(choice == 'U') {
            if(head == NULL) {
                current = head = new_node();//need set to current
            } else {
                current = head;
                while(current->next != NULL) {//"current != NULL" can't link
                    current = current->next;
                }
                current = current->next = new_node(); 
            } 
            printf("Movie \"%s\" is added to the database.\n", current->title);
            amountOfMovies++; 
        } else if(choice == 'S') {
            char tempTitle[250];
            bool found = false;//need <stdbool.h>
            temp = head;
            //temp = malloc(sizeof(movie));//Unnecessary
            printf("Name of movie: ");
            scanf(" %249[^\n]%*c", tempTitle);
            //printf("NAME OF MOVIE IN HEAD: %s\n", temp->title);            
            while(temp != NULL) {
                //printf("NAME OF CURRENT MOVIE TO COMPARE TO: %s\n", temp->title); 
                if(strcmp(temp->title, tempTitle) == 0) {
                    printf("Year: %d\n", temp->year);
                    printf("Rating: %hhu\n", temp->rating);
                    found = true;
                    break;
                } else { 
                    temp = temp->next;
                    printf("HAVEN'T FOUND MATCH, NEXT TITLE TO CHECK IS: %s\n", temp->title); 
                    //found = false;//Unnecessary
                }  
            } 
            if(found == false) { 
                printf("Movie \"%s\" does not exist in the database.\n", tempTitle);
            }
        } else { 
            loop = 0; 
        } 
    } 
    // free all the nodes
    return 0;
}

尝试为每个任务编写函数。这样,您的代码将易于阅读和调试。您的代码包括:ifhead==NULL{printfName of the movie:;scanf%[^\n]]*c,head->title;这意味着您正在访问一个空指针,这是一个坏主意™. 您应该只需要一批代码来进行输入。您可能需要创建一个函数来进行输入,并将指针传递给应读取数据的变量结构。您还应该强化代码,以防用户在程序中键入“EOF”control-D或control-Z来测试输入操作的结果。程序执行temp=head;然后temp=mallocsizeofmovie;。这难道不是毫无意义的逻辑吗?