简体   繁体   English

测试链表中的指针

[英]Testing the pointers in a linked list

I have a weird result when I run this code 当我运行此代码时,我有一个奇怪的结果

void MovieDatabase:: showMovie(const string mtitle){
    cout << "The informations about the" << mtitle << endl;
    for(Movie* cur = headMovie; cur ->next !=NULL; cur= cur->next){
        if(cur -> title == mtitle){
            cout <<"Movie title is "<<  cur ->title << endl;    
            //cout < "The name of director is " << cur -> firstNameOfDirector << endl;
            cout << "The last name of director is " << cur -> lastNameOfDirector <<endl;
            cout << "The year that the movie is released " << cur -> year << endl;
            cout << "The day that the movie is released " << cur -> day << endl;
            cout << "The month that the movie is released " << cur -> month << endl;
        }
    }
    cout << endl;
}

Here is the code that I'm checking for the movietitle and if they are in the linked list i'm printing detailed information about the film. 这是我正在检查movietitle的代码,如果它们在链表中,我正在打印有关电影的详细信息。 However, as an output it just prints the line that is 但是,作为输出,它只是打印出那条线

cout << "The informations about the" << mtitle << endl;`

I could not understand what is the reason can anyone help? 我无法理解任何人可以帮助的原因是什么?

Check to make sure your strings really are equal, and there's not a hidden \\r or \\n or trailing spaces in one of them. 检查以确保您的字符串确实相等,并且其中一个字符串中没有隐藏的\\r\\n\\n或尾随空格。

Also as mentioned in the comments, you probably want your loop termination condition to be cur != NULL and not cur->next != NULL . 同样如评论中所述,您可能希望循环终止条件为cur != NULL而不是cur->next != NULL

There are two possibilities: 有两种可能性:

  1. You have no movies in your list whose title is mtitle . 您的列表中没有标题为mtitle
  2. You have a movie with that title, but it is the last movie in your list. 你有一个带有该标题的电影,但它是你列表中的最后一部电影。 Your while loop ends as soon as you get to the last movie (the one whose next == NULL ) without checking it. 一旦你到达最后一部电影(其next == NULL ),你的while循环就会结束而不检查它。

Without knowing the contents of your list we cannot know which is the case here. 在不知道列表内容的情况下,我们无法知道这里的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM