升序排序链表

时间:2018-10-06 02:48:11

标签: c++ list insert nodes

我无法根据其评分对链接列表进行排序。我得到了三个任务,如果列表为空,则添加第一个节点,如果传入的节点的等级小于第一个节点,则将其移到最前面。如果它大于最后一个值,则将其向后推,否则按正确的顺序放置节点。

我是积极的函数push(int r,字符串c),addFirst(int r,字符串c)和addAtFront(int r,字符串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

在不覆盖列表中较大的值的情况下,实现这些中间节点有很多麻烦。最后的情况使我发疯,我尝试了许多不同的事情。

1 个答案:

答案 0 :(得分:0)

我看到的是,每次创建新的SNode时,New_SNode-> next都不分配给列表的其余部分。 每次打印列表时,不会显示以前的SNode。

在开始时声明一个 SNode * tmp2;

您的for循环应为:

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

祝你好运。