2013-12-16 142 views
0
List_cell *currPtr = list_first_; 

if(list_first_ == nullptr){ 

    list_first_ = new_cell; 
} 
else{ 

    currPtr = list_first_; 
    while(currPtr->next != nullptr){ 
     currPtr = currPtr->next; 
     currPtr->next = new_cell; 
    } 
} 

這是什麼問題?它似乎並沒有進入while循環......感謝您的幫助!將項目添加到鏈接列表的末尾

+0

請顯示您的完整代碼。什麼是new_cell? –

回答

4

您不希望將currPtr-> next設置爲new_cell,直到您到達列表的末尾,否則您只需在列表中的第一個元素之後添加new_cell,然後丟失已存在的任何內容列表。

List_cell *currPtr = list_first_; 

if(list_first_ == nullptr){ 

    list_first_ = new_cell; 

} 
else{ 

    currPtr = list_first_; 
    while(currPtr->next != nullptr){ 
     currPtr = currPtr->next; 
    } 
    currPtr->next = new_cell; 
} 

只有當您到達結尾時,纔會將new_cell添加到列表中。

+1

非常感謝! – user2933157

+0

雖然我當然會推薦您使用標準庫中的列表,而不是爲了學習目的而創建自己的列表。 – user2711915

+0

這是我學校項目的一部分,它需要我使用鏈接列表:( – user2933157