因此,我正在处理此链接列表分配,并真的很感谢帮助打印出链接列表的元素。 到目前为止,我已经使用了一个数组来定义链表的值,但是每当我尝试使用单独的打印函数打印头值时,它总是会出现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