我想知道這段代碼是否正確地模仿了使用鏈接列表的向量的「at」函數。這個概念是教我們鏈表,我不確定是否把pos ++放在正確的位置。有人可以幫我解決問題,並告訴我每一行都在做什麼,所以我知道它是如何從while循環退出的?截至目前,這是令人困惑的。謝謝!模仿向量的C++鏈接列表
這裏是整個項目的引擎收錄:http://pastebin.com/wyNQx3GP
謝謝你們
// This returns the countyElectionResults result at a particular point
// in the list.
// This is analogous to the at method in the vector class.
countyElectionResults countyElectionList::at(int place){
if(head == NULL){
countyElectionResults * name = NULL;
return * name;
}
else{
countyElectionResults * current = head;
int pos = 0;
while(pos != place && current->getNextResult() != NULL){
current = current->getNextResult();
}
pos++;
return * current;
}
cout << "Not Found" << endl;
}
但它仍然有效,無論出於某種原因,我假設?你知道爲什麼嗎? – tekman22
這是因爲列表結束時出現循環的其他條件。如果沒有立場pos你的pos將成爲地方? –
不知道哪些情況下,它不會工作..只測試它,整個程序編譯並運行良好,即使在它使用(在)函數的情況下?有任何想法嗎?謝謝! – tekman22