|
|
|
|
|
|
I also checked it in my debugger. |
In the debugger, even after the last while-loop, the list still seemed to only have Odin in it. |
trav = trav->m_succ
line within the loop condition so that it definitely updates it after each iteration. It's actually harder for me to tell if it'll really be updated if it's inside the loop body.
|
|
|
|
|
|
|
|
I'm not being confused. Why do you think so? |
When I run it in the debugger, both loops get skipped over completely. So I think they aren't being run. And if they aren't run, of course nothing would be in the list aside from the first node, either. |
I don't know what to try, exactly. I'm not that good at debugging. |
Are you thinking I'm confused because I answered more of your questions than you thought I would? |
I don't know what to try, exactly. I'm not that good at debugging. |
Are you thinking I'm confused because I answered more of your questions than you thought I would? |
An obvious thing to do would be to look at the values of the variables that control the loop, to see why the loop isn't being run, and in what way those values differ from the values you expect them to have. |
Why are you trying to print the entire "list" inside that loop? Wouldn't it be better just to see the values held in the single instance in question instead? |
Could it be that the reason those loops aren't running is because trav->m_prev and trav->m_succ are never non-NULL in this case? |
@jlb: And I did answer your questions earlier, didn't I? |
The only ones I didn't answer from that one post were these two: (but instead of answering them, I just did what you said I should do): |
Please explain what you think that second loop should be doing, in English. Then explain the purpose of each line in that second loop, in detail. |
As for what I think the loop should do. It should go through loop as long as the name string in the new node is less than the name string in the current node and the end of the list hasn't been reached yet. And it should insert the new node into the list before the current node if the condition is true or more on to the next node. |
The purpose of the first line in the loop body is to insert the new node where it should go. The purpose of the second line is to move on to the next node in the list. |
So then why was I told I'd have to use both insert() and add() in the same loop? Who told you that you would need use either insert() or add() in any loop? |
Didn't I show you output from print() twice already? Or do you think I made that up? |
I do think it's strange that each of the "god" instances passed to print() have the same address. Why did that happen? I wonder if it has something to do with why the loops aren't running. |
The variable that controls loop, for the second one, would be trav (since trav->m_succ is being tested for nullptr to see if we can still go forward through the list). |
And the name of the god in the current node is being compared to the current one, so but they aren't controlling the loop. Like how, in a regular for-loop, the variable controlling the loop is the integer initialized and updated in the initialization and update parts of the loop's parentheses. |
key < trav->god().name()
.
|
|
In add_ordered(), after the first call to insert(): Address of *current is: 0133F5E4; The name of the god is: Zeus; The previous pointer is: 00000000; The next pointer is: 0133F71C In add_ordered(), after the first while-loop: Address of *current is: 0133F5E4; The name of the god is: Zeus; The previous pointer is: 00000000; The next pointer is: 0133F71C In add_ordered(), after the first call to insert(): Address of *current is: 0133F5E4; The name of the god is: Thor; The previous pointer is: 00000000; The next pointer is: 0133F794 In add_ordered(), after the first while-loop: Address of *current is: 0133F5E4; The name of the god is: Thor; The previous pointer is: 00000000; The next pointer is: 0133F794 In add_ordered(), after the first call to insert(): Address of *current is: 0133F5E4; The name of the god is: Frejya; The previous pointer is: 00000000; The next pointer is: 0133F80C In add_ordered(), after the first while-loop: Address of *current is: 0133F5E4; The name of the god is: Frejya; The previous pointer is: 00000000; The next pointer is: 0133F80C { Name: Frejya Myth: Norse Vehicle: N/A Weapon: Magic called seidr Name: Thor Myth: Norse Vehicle: Chariot pulled by goats Tanngrisnir and Tanngnjostr Weapon: Hammer called Mjolnir Name: Zeus Myth: Greek Vehicle: N/A Weapon: Thunderbolt Name: Odin Myth: Norse Vehicle: Eight-legged flying horse called Sleipnir Weapon: Spear called Gungnir } Number of nodes in list is: 4 |
I did look at trav and n in the debugger, and I saw that trav's next and previous pointers are both pointing at NULL. |
f I don't do something to set them to a non-NULL value before the loops, the loops will never start. |
As for the hint. Did you mean to tell me I had to use one of them in the second loop? |
Who told you that you would need use either insert() or add() in any loop? |
But am I not already doing that (insert() in the second loop)? |
Or were you trying to tell me that I'd have to call insert() or add() before the loops, too, like I just asked? |
When I say that the name of the god in the current node is being compared to the name in the current one, I'm talking about the comparison part of the condition in the second while-loop, key < trav->god().name(). |
The primary condition that should control the second loop is that the current name is less than the desired name |
But I'm having trouble moving the node from where it was to the correct position. |
|
|
|
|
In add_ordered(), after the first call to insert(): *current's address: 009EF22C; god's name: Zeus; previous pointer: 00000000; next pointer: 009EF364 In add_ordered(), after the first while-loop: *current's address: 009EF22C; god's name: Zeus; previous pointer: 00000000; next pointer: 009EF364 In add_ordered(), after the first call to insert(): *current's address: 009EF22C; god's name: Thor; previous pointer: 00000000; next pointer: 009EF3DC In add_ordered(), after the first while-loop: *current's address: 009EF22C; god's name: Thor; previous pointer: 00000000; next pointer: 009EF3DC In add_ordered(), after the first call to insert(): *current's address: 009EF22C; god's name: Frejya; previous pointer: 00000000; next pointer: 009EF454 In add_ordered(), after the first while-loop: *current's address: 009EF22C; god's name: Frejya; previous pointer: 00000000; next pointer: 009EF454 { Name: Frejya Myth: Norse Vehicle: N/A Weapon: Magic called seidr Name: Thor Myth: Norse Vehicle: Chariot pulled by goats Tanngrisnir and Tanngnjostr Weapon: Hammer called Mjolnir Name: Zeus Myth: Greek Vehicle: N/A Weapon: Thunderbolt Name: Odin Myth: Norse Vehicle: Eight-legged flying horse called Sleipnir Weapon: Spear called Gungnir } Number of nodes in list is: 4 |
this
object and then returns its argument to the calling function.
|
|
|
|
In add_ordered(), after the if-else chain: *current's address: 004FF028; god's name: Zeus; previous pointer: 004FF1F0; next pointer: 00000000 In add_ordered(), after the if-else chain: *current's address: 004FF028; god's name: Thor; previous pointer: 004FF1F0; next pointer: 004FF268 In add_ordered(), after the if-else chain: *current's address: 004FF028; god's name: Frejya; previous pointer: 00000000; next pointer: 004FF1F0 { Name: Frejya Myth: Norse Vehicle: N/A Weapon: Magic called seidr Name: Odin Myth: Norse Vehicle: Eight-legged flying horse called Sleipnir Weapon: Spear called Gungnir Name: Thor Myth: Norse Vehicle: Chariot pulled by goats Tanngrisnir and Tanngnjostr Weapon: Hammer called Mjolnir Name: Zeus Myth: Greek Vehicle: N/A Weapon: Thunderbolt } Number of nodes in list is: 4 |
|
|
std::copy(arg, arg + sz, elem);
). The arg + sz
part also won't compile without overloading an operator+, but I ended up getting a function that was recursive in all control paths when I tried to do it. I need help on this, too. Should I make a new thread for that? Using the Links with the values of type God, make a list of gods from three mythologies; then move the elements (gods) from that list to three lexicographically ordered lists — one for each mythology. |