У меня проблемы с сортировкой связанного списка на основе их рейтингов.Мне дано три задания: если список пуст, я добавляю первый узел, если переданный рейтинг узла меньше, чем первый узел, я перемещаю его вперед.Если оно больше, чем последнее значение, я толкаю его назад, иначе я помещаю узел в правильном порядке.
Я уверен, что функции 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
У меня много проблем с реализацией этих промежуточных узлов без перезаписибольшие значения в списках.Этот последний случай сводит меня с ума, я пробовал много разных вещей.