[英]Sorting linked list in ascending order
我無法根據其評分對鏈接列表進行排序。 我得到了三個任務,如果列表為空,則添加第一個節點,如果傳入的節點的等級小於第一個節點,則將其移到最前面。 如果它大於最后一個值,則將其向后推,否則按正確的順序放置節點。
我是積極的函數push(int r,string c),addFirst(int r,string c)和addAtFront(int r,string c)正常工作。 我在實現節點屬於最低值和最高值之間的情況時遇到了麻煩。
排序功能如下:
void SLL::insertInOrder(int r, string c){
SNode *tmp = new SNode(r,c);
if(first == NULL){
addFirst(tmp->rating,tmp->comments);
}
else if(tmp->rating < first->rating){
addAtFront(r,c);
}
else if(tmp->rating > last->rating){
push(r,c);
}
else{
for(tmp =first; tmp->next != NULL; tmp = tmp->next){
if(tmp->rating < tmp->next->rating){
tmp->next = new SNode(r,c);
}
}
}
}
這是main中的循環作為測試:
int r[10] = {9,8,4,5,11,10,3,6,8,2};
string s[10] = {"really good!","loved it","mediocre",
"okay, not great","best book ever!", "awesome!",
"boring","not bad","definitely worth reading", "terrible!"};
SLL *list = new SLL();
for (int i = 0; i < 10; i++){
list->insertInOrder(r[i],s[i]);
list->printSLL();
}
我的輸出:
Rating: 9,Comments: really good!
Rating: 8,Comments: loved it
Rating: 9,Comments: really good!
Rating: 4,Comments: mediocre
Rating: 8,Comments: loved it
Rating: 9,Comments: really good!
Rating: 4,Comments: mediocre
Rating: 5,Comments: okay, not great
Rating: 4,Comments: mediocre
Rating: 5,Comments: okay, not great
Rating: 4,Comments: mediocre
Rating: 10,Comments: awesome!
Rating: 3,Comments: boring
Rating: 4,Comments: mediocre
Rating: 10,Comments: awesome!
Rating: 3,Comments: boring
Rating: 6,Comments: not bad
Rating: 3,Comments: boring
Rating: 8,Comments: definitely worth reading
Rating: 2,Comments: terrible!
Rating: 3,Comments: boring
Rating: 8,Comments: definitely worth reading
輸出應為:
Rating: 9,Comments: really good!
Rating: 8,Comments: loved it
Rating: 9,Comments: really good!
Rating: 4,Comments: mediocre
Rating: 8,Comments: loved it
Rating: 9,Comments: really good!
Rating: 4,Comments: mediocre
Rating: 5,Comments: okay, not great
Rating: 8,Comments: loved it
Rating: 9,Comments: really good!
Rating: 4,Comments: mediocre
Rating: 5,Comments: okay, not great
Rating: 8,Comments: loved it
Rating: 9,Comments: really good!
Rating: 11,Comments: best book ever!
Rating: 4,Comments: mediocre
Rating: 5,Comments: okay, not great
Rating: 8,Comments: loved it
Rating: 9,Comments: really good!
Rating: 10,Comments: awesome!
Rating: 11,Comments: best book ever!
Rating: 3,Comments: boring
Rating: 4,Comments: mediocre
Rating: 5,Comments: okay, not great
Rating: 8,Comments: loved it
Rating: 9,Comments: really good!
Rating: 10,Comments: awesome!
Rating: 11,Comments: best book ever!
Rating: 3,Comments: boring
Rating: 4,Comments: mediocre
Rating: 5,Comments: okay, not great
Rating: 6,Comments: not bad
Rating: 8,Comments: loved it
Rating: 9,Comments: really good!
Rating: 10,Comments: awesome!
Rating: 11,Comments: best book ever
我在實現這些中間節點而又不覆蓋列表中較大的值時遇到很多麻煩。 最后的情況使我發瘋,我嘗試了許多不同的事情。
我看到的是,每次創建新的SNode時,New_SNode-> next都不會分配給列表的其余部分。 每次打印列表時,不會顯示以前的SNode。
首先聲明一個SNode * tmp2;
您的for循環應為:
tmp2 = tmp->next;
tmp->next = new SNode(r,c);
tmp->next->next->tmp2;
祝好運。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.