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

标签 c pointers struct linked-list malloc

我正在用 C 语言开发一个“简单”程序,我们在其中创建一个链表,其结构充当电影,其中存储标题、制作年份、评级 (1-5) 和指向下一个节点的指针。我们不允许向此结构添加任何内容,或定义函数。

最重要的是,我们(出于某种原因)需要将整个链表写入 main() 的主体中,这样就给问题增加了一层意大利面条。无论如何,在这个程序中,我们应该让用户输入“U”进行更新或输入“S”来搜索电影。更新会按照您的预期进行,您输入标题、年份、评级。由此,我们应该将节点插入链表的末尾

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

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

我假设这与我调用 malloc 的方式有关?或者与未正确分配节点有关?老实说,我不确定,不幸的是,实验室的助教也不知道出了什么问题 D:

这是我的代码:

#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 不再为空,并且您将无法按照您想要的方式插入第一部电影 - 将 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)

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

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

关于c - 比较 C 中链表中的值时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27053215/

相关文章:

连接两个字符数组 C

c - 在函数 C 内分配 void 指针

c - 为结构及其成员分配内存

c - 这个值是如何计算的?

c - 如何成功地将所有文件 IO 记录放入数组中?

c++ - 另一个线程中的 pthread_mutex_lock 和 pthread_mutex_lock

c - 无类型 union (没有类型标签)有什么用处吗?

c - 用C读取二进制文件

c++ - 没有定义的结构用作指针

c - 如何调用结构的根/父级