繁体   English   中英

升序排序链表

[英]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.

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