简体   繁体   English

升序排序链表

[英]Sorting linked list in ascending order

I am having trouble sorting a linked list based on their ratings. 我无法根据其评分对链接列表进行排序。 I am given three tasks, if the list is empty I add the first node, if the passed in node's rating is less than the first node, I move it to the front. 我得到了三个任务,如果列表为空,则添加第一个节点,如果传入的节点的等级小于第一个节点,则将其移到最前面。 If it is greater than the last value, I push it to the back, otherwise I put the node in the its right order. 如果它大于最后一个值,则将其向后推,否则按正确的顺序放置节点。

I am positive functions push(int r, string c), addFirst(int r, string c), and addAtFront(int r, string c) work correctly. 我是积极的函数push(int r,string c),addFirst(int r,string c)和addAtFront(int r,string c)正常工作。 I am having trouble implementing the case where the node belongs in between the lowest and highest value. 我在实现节点属于最低值和最高值之间的情况时遇到了麻烦。

The sorting function is below: 排序功能如下:

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);
        }
    }
    }

}

Here is the loop in main as the test: 这是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();
    }

My output: 我的输出:

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

The output should be: 输出应为:

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

I'm having a lot of trouble implementing those intermediate nodes without overwriting the larger values in lists. 我在实现这些中间节点而又不覆盖列表中较大的值时遇到很多麻烦。 That last else case is driving me crazy, I've tried many different things. 最后的情况使我发疯,我尝试了许多不同的事情。

What I see, is that every time you create a new SNode, New_SNode->next is not asigned to the rest of the list. 我看到的是,每次创建新的SNode时,New_SNode-> next都不会分配给列表的其余部分。 Every time you print the list, previous SNode do not appear. 每次打印列表时,不会显示以前的SNode。

At the begining declare a SNode *tmp2; 首先声明一个SNode * tmp2;

your for loop should be : 您的for循环应为:

 tmp2 = tmp->next;
 tmp->next = new SNode(r,c);
 tmp->next->next->tmp2;

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM