我正在尝试对链接列表进行排序。我有一个名为 head 的节点,它指向下一个节点,等等。
但是当我尝试按节点携带的值对节点进行排序时,我得到了排序工作,因为我看到它打印出 if 语句中的内容,但我没有取回链接列表。我哪里做错了?
Node* head;
void sortlist(){
Node * runner = head;
Node * runner2;
for(runner = head; runner->next != NULL; runner = runner->next){
for(runner2 = runner->next; runner2->next != NULL; runner2 = runner2->next){
if(runner->freq < runner2->freq){
cout<< runner->freq<< " is LT "<<runner2->freq<< endl;
Node * temp = runner;
runner = runner2;
runner2 = temp;
}
}
}
head = runner;
}
我只取回第一个节点。