[問題] Singly linked list 輸入新資料時即排序

看板C_and_CPP (C/C++)作者 (兩金)時間15年前 (2011/03/09 15:01), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) Dev C++ 餵入的資料(Input): double 鍵盤輸入 錯誤結果(Wrong Output): 用紙筆畫出圖看不出來有什麼問題, 但新增第二個值後執行的框框死當 程式碼(Code):(請善用置底文網頁, 記得排版) http://0rz.tw/dLcLL 未更改的原始碼, 藍字為小弟更改的部份, 即以下程式碼 感謝各位撥冗檢閱 m(_._)m Node* Node::putOneNode (Node* x, double d) { Node* temp = NULL; Node* Lg; // 最大值 if (temp = new Node()) { temp->nextPtr = x; temp->nr = d; } // 讓Lg紀錄第一次輸入時的值 if (temp->nextPtr == NULL) Lg = temp; // 若輸入值 >= 最大值, 回傳該值 if (temp->nr >= Lg->nr){ Lg = temp; return temp; } // 若輸入值 < 最大值, 回傳最大值 並判斷將輸入值鑲嵌至 link list 何處 else{ return Lg; Node* temp2 = Lg; Node* temp1 = Lg->nextPtr; while (temp->nr < temp1->nr && temp1->nextPtr != NULL){ temp2 = temp1; temp1 = temp1->nextPtr; } if(temp1->nextPtr == NULL) temp1->nextPtr = temp; else{ temp->nextPtr = temp1; temp2->nextPtr = temp; } } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 24.218.24.178
文章代碼(AID): #1DToN9kr (C_and_CPP)
文章代碼(AID): #1DToN9kr (C_and_CPP)