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. 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:
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.