簡體   English   中英

測試鏈表中的指針

[英]Testing the pointers in a linked list

當我運行此代碼時,我有一個奇怪的結果

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;
}

這是我正在檢查movietitle的代碼,如果它們在鏈表中,我正在打印有關電影的詳細信息。 但是,作為輸出,它只是打印出那條線

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

我無法理解任何人可以幫助的原因是什么?

檢查以確保您的字符串確實相等,並且其中一個字符串中沒有隱藏的\\r\\n\\n或尾隨空格。

同樣如評論中所述,您可能希望循環終止條件為cur != NULL而不是cur->next != NULL

有兩種可能性:

  1. 您的列表中沒有標題為mtitle
  2. 你有一個帶有該標題的電影,但它是你列表中的最后一部電影。 一旦你到達最后一部電影(其next == NULL ),你的while循環就會結束而不檢查它。

在不知道列表內容的情況下,我們無法知道這里的情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM