因此,我正在處理此鏈接列表分配,並真的很感謝幫助打印出鏈接列表的元素。 到目前爲止,我已經使用了一個數組來定義鏈表的值,但是每當我嘗試使用單獨的打印函數打印頭值時,它總是會出現NULL。現在我想這可能與一些全球和當地的問題有關,但我不確定。如果是範圍問題,那麼有關如何全局定義鏈表值的建議將不勝感激。鏈接列表打印NULL - C++
下面是我的打印功能和我的構建函數的代碼。我現在想完成的所有工作都是從打印功能中打印出構建函數中定義的頭值。頭部值似乎通過構建函數是一致的,但只要我執行打印函數,它就變爲NULL。 任何想法?
void CommunicationNetwork::buildNetwork(){
std::string a[10] = {"Los Angeles","Phoenix", "Denver", "Dallas","St. Louis", "Chicago", "Atlanta", "Washington, D.C.", "New York", "Boston"};
City *head = new City;
head ->cityName=a[0];
head ->next = NULL;
City *current = head;
int i =1;
while(i<10){
City *tmp = new City;
tmp ->cityName = a[i];
tmp ->next = NULL;
current ->next = tmp;
current = current->next;
i++;
}
City *tail = current;
City *tmp2 = new City;
tmp2 = head;
current = tmp2;
//cout<<"head = "<<head->cityName<<endl;
while(current != NULL){
cout<<current->cityName<<" -> ";
current = current->next;
}
if(current == NULL){
cout<<"NULL"<<endl;
}
cout<<head->cityName<<endl;
}
void CommunicationNetwork::printNetwork(){
cout<<"===Current Path==="<<endl;
cout<<head->cityName<<endl;
cout<<"=================="<<endl;
}
您應該在操作員周圍放置空間,例如'「cout << current-> cityName << " ->」;' –
當您使用調試器,並逐步通過每個語句時,哪個語句導致問題? –
一切都在運行,但是我的printNetwork()函數在輸出「Los Angeles」時打印出NULL。 – Konig