我試圖讓升序排列, 但由於某些原因有序鏈表的代碼給我一個價值 如果有一個人能告訴我,爲什麼我會感激 看到這個函數:添加節點有序鏈表
void AddNode(Node *&head){
int temp;
Node *q;
Node *prev = 0;
Node *t;
cout <<"enter a number : ";
cin >> temp;
t = new Node ;
t->number = temp;
t->next = 0;
if (head == 0) {
head = t;
} else {
q = head;
while (q != 0){
prev = q;
if(q->number > t->number){
prev->next = t;
t->next = q;
break;
}
q = q->next; // counter
}
if (q == 0) // if it is the last value
q = t;
}
}
該參數??它聞起來臭臭的。無論如何,如果它編譯,那麼你需要_step-over_你的代碼,看看到底發生了什麼。在Visual Studio上使用F-10 –
當新節點插入前面時,不會更新'head'。而且,最後的'q = t'沒有任何用處 - 你正在更新一個局部變量,但沒有使用這個新值。 –
@FirstStep:如果使用Eclipse或Code :: Blocks,OP應該做什麼?也許OP應該使用*調試器*?除了Visual Studio還有其他IDE。 –