我一直在掙扎幾個小時,結束了這個問題。我的目標是僅使用指針對鏈表進行排序(我不能將鏈表放入vec或數組中,然後進行排序)。我得到了指向列表頭節點的指針。我可以調用指針的唯一方法是head-> next(next node)和head-> key(存儲在節點中的int值,用於比較)。我一直在過度使用我的白板,並嘗試幾乎所有我能想到的東西。排序鏈接列表C++與指針
Node* sort_list(Node* head)
{
Node* tempNode = NULL;
Node* tempHead = head;
Node* tempNext = head->next;
while(tempNext!=NULL) {
if(tempHead->key > tempNext->key) {
tempNode = tempHead;
tempHead = tempNext;
tempNode->next = tempNode->next->next;
tempHead->next = tempNode;
tempNext = tempHead->next;
print_list(tempHead);
}
else {
tempHead = tempHead->next;
tempNext = tempNext->next;
}
}
return head;
}
發佈您正在嘗試修復的代碼。我們不介意讀者 - 沒有看到您嘗試過的內容,就沒有辦法提供幫助。 – Yuushi
你有什麼嘗試?你在找人爲你寫代碼嗎? – edtheprogrammerguy
[code](http://pastebin.com/af3Npif4) 對不起,我發貼時忘了粘貼我的代碼。過去5個小時我嘗試了很多東西。如果你批評我的代碼,那很好,但一般的想法也有幫助。 print_list方法接受一個節點並將其中的節點打印到列表的末尾。 – dclark