Well, yes, I didn't understand what you meant when you said that you'd noticed several problems in my code before. The reason I didn't wasn't because I didn't want to know, but more because I wasn't sure if I should ask that. I felt it was something I needed to figure out on my own. |
As for the code of mine that you posted: is that the place you're saying I'm wrong at? Or at least one of the places I'm wrong? If so, I'm guessing the main problem is in Line 6 in that snippet. But I don't know what. |
As for why I didn't make that change to main() that you suggested on Sept. 28 at 10:33 am. When I asked about whether or not I should use "new" in add_ordered(), you said there was no need because main() already does that. So I was just wondering if I'd need to use "new" in add_ordered() if I took it out of main(). But I don't think I should use regular variables (i.e. non-pointer variables) for the Link objects since all of the functions that return a Link object do so by returning a pointer to a Link. I'd have to change those functions' return values to plain Links if I were to make that change to main(). |
|
|
void print(const Link& current);
, notice the parameter is not a pointer, but a reference to a single instance of the Link class. It will be able to be called from anywhere in your program to print out the variables of interest (the god's name, the previous pointer, and the next pointer). This will be used to help you debug your program without needing to rely on your debugger.Thomas1965 wrote: |
---|
Somehow I have the suspect that you overthink this problem. When adding the node there are two options. If the name of the insert god is greater than the current god insert it after. If the name of the insert god is smaller than the current god insert it before. I assume ascending order. |
If the name of the insert god is greater than the current god insert it after. |
Can i disagree with the underlined part? If we were talking numbers, and already had {1,2,5} and wanted to insert 8 we would have {1,8,2,5} and would reiterate that this is why the OP's code has never worked. |
std::string key = n->m_god.name();
. That's the name of the god in the node to be inserted.
|
|
|
|
At the risk of being pedantic, consider if the first node is 3, the value to be inserted is 2, it has to go before ...... |
Okay, wait. If I only have to insert before the next node, what about a case where the node I want to insert is greater than the next node? |
And why did you read my code as saying I'm assigning the node itself to the std::string key? I was assigning the name of the god to it like I'm supposed to. It clearly says std::string key = n->m_god.name();. That's the name of the god in the node to be inserted. |
As for the while-loop condition. Should be like this? |
How do I find the head of the list, though? |
Would that be whatever the "this" pointer is pointing at when add_ordered() starts? |
And am I going to tell it to keep going until it finds the right place to insert the node when there's no if-condition? |
I just used the debugger and, apparently, "this" was NULL. |
Good so far? Or is there still a mistake in the while loop and/or condition? |
while ((trav = trav->m_succ) || key < trav->m_god.name())
void print(Link *gods)
because I don't know how to get the address of the object that a reference is referring to. Anyway, I made the suggested changes to main() and tried to fix add_ordered() (no change in the output of the latter, though).
|
|
|
|
Address of *gods is: 003FF624 The name of the god is: Odin The previous pointer is: 00000000 The next pointer is: 00000000 Please enter a character to exit k Press any key to continue . . . |
Anyway, for the head: Isn't that the first node in the list? |
The node isn't being inserted at all. |
I made this the signature of the new print function: void print(Link *gods) because I don't know how to get the address of the object that a reference is referring to |
The first node in the list is the one whose previous pointer is pointing at NULL. |
But I still need to check for that, right? |
|
|
|
|
|
|
So I don't need to worry about a case where the key is greater than the name in the current node? |
So then why was I told I'd have to use both insert() and add() in the same loop? |
|
|
Still not actually adding anything, though. |
|
|
|
|
And I know you said I shouldn't return nullptr, but there are some situations where I have to, right? So could you suggest something to better to return in places where I'm returning nullptr but shouldn't be? |
|
|
|
|
|
|
Write a print_all() function that lists gods with their attributes one per line. |
The function insert() is for inserting before "this" object and the function add() is for inserting after "this" object. |
So I also don't need two loops, with the second testing for greater than? I just need the one checking for less than, and it has to somehow see where to insert the node if it has to go after the current or next node? If so, what am I still missing? |
From print(): Address of *current is: 008FF608 The name of the god is: Odin The previous pointer is: 00000000 The next pointer is: 00000000 Address of *current is: 008FF608 The name of the god is: Zeus The previous pointer is: 00000000 The next pointer is: 00000000 Address of *current is: 008FF608 The name of the god is: Thor The previous pointer is: 00000000 The next pointer is: 00000000 From print_all(): { Name: Odin Myth: Vehicle: N/A Weapon: } Number of nodes in list is: 1 Please enter a character to exit l Press any key to continue . . . |
|
|
|
|
|
|
But about print_all(), am I not already printing out each god's attribute one per line? I thought it was asking for each god's attribute one per line with each god's information also separated by a new line, like I have now. |
|
|
|
|
{Name: Odin Myth: Vehicle: N/A Weapon:}
As for the functions that return nullptr. find(), advance, erase() and insert() all return nullptr originally; that's how I got them from the book. I think I know why you're saying it's bad, but I don't want to change code I got from the book that much. |
|
|
The getters in the God class shouldn't be returning const references? So I shouldn't make them return a const string, either? But I thought getters in classes had to return a const reference if they return a big object like a string or a vector? |
std::string name() const { return m_name; }
The remaining const is telling the compiler that this function doesn't modify the class in any way.So anyway, if I don't need another loop to check for where to put the new node, then what am I still missing? |