If it was working correctly, that would be the simplest way to make sure I got the correct output. |
I think it's possible that what's happening is that it puts the string that's greater than the one before it before the other string when they're in the correct order and side-by-side like that. Though I could be wrong. |
I think it's possible that what's happening is that it puts the string1 that's greater than2 the one before it3 before the other string when they're in the correct order and side-by-side like that4. Though I could be wrong.5 |
I know my other question has nothing to do with this, but I was trying to do that exercise and thought I'd ask about it here. So I'd be better making a different for it, after all? |
And I managed to fix the problem of the memory leak in that other program. |
1. In this case, that would be the name string in the second node being inserted through add_ordered() in that particular scenario, so "Zeus". |
2. Then "not less than". |
5. It should be now. |
|
|
I was talking about the two gods being inserted through add_ordered(). First is Odin, second is Zeus. And as for the not less than, I meant that Zeus is not less than Odin, but that it's still going before it. That's what I was saying. |
It correctly inserts Odin after Apollo, but then it only compares Zeus with Apollo and inserts it after Apollo. |
Doesn't what I quoted also mean that because the code doesn't compare Odin and Zeus, and only Apollo and Zeus, it just inserts Zeus after Apollo? |
I was talking about the two gods being inserted through add_ordered(). First is Odin, second is Zeus. And as for the not less than, I meant that Zeus is not less than Odin, but that it's still going before it. That's what I was saying. |
And as for what's wrong in my pseudocode. Please let me know what it is so I can fix it. |
Isn't Odin not seen because there's no loop to see where to best insert the subsequent nodes? |
It just inserts it after comparing it with the first node it sees. |
I know you tried to tell me where the problem is several times, but I didn't understand those hints. My bad, there. |
Who told you that you would need use either insert() or add() in any loop? |
This was one of the hints, right? |
TheIdeasMan and/or someone else had told me I might need both of my inserting functions, |
so I thought I might need to use one or both of them in a loop. But is that one of the problems in my code? |
I don't need to use either one of the inserting functions in the loop that traverses the list? |
Then where do I insert the node? |
After the loop? |
But that would insert it before or after the last node, wouldn't it? |
|
|
I also tried doing it on my Windows notepad editor and it seems fine (if I did it correctly). |
if # of nodes in list is 0 |
|
|
I care about the number of nodes because the trouble here is happening partly because it only looked at the first node regardless of how many nodes were in the list. If it knows there's more than node when there is more than one node, I'll have one less problem to worry about. |
As for how I'll know how many nodes there are. I made a function that counts them, if you recall. |
That's two separate while loops. One for making sure we start at the head, and the other for traversing the list to find the right place for the node. |
while current node is less than node to insert and next pointer is not null |
Here's another massive hint: I reckon we only need two comparisons: NextPtr with nullptr ; and a less than . They both form the condition for the while loop. If either of them is true, insert the item after the current pointer, otherwise move to the next item. There we go, 1 line of pseudo code. Only a few lines of actual code. Note I didn't actually have any if statements. |
while (trav->m_god.name() < n->m_god.name() && trav->m_succ)
as the loop condition. Unless I misunderstood something?
|
|
So how about this? |
Then I was told later that I'd have to do while (trav->m_god.name() < n->m_god.name() && trav->m_succ) as the loop condition. Unless I misunderstood something? |
I put the comparison and insertion after the loop. |
|
|
|
|
Because putting inside or before the loop didn't work and my manual test seemed to show that putting it after the loop would work. |
|
|
|
|