|
|
Are they each being overwritten by the next one entered for some reason because I'm doing the wrong checks for nullptr or am doing the wrong thing there, or something like that? |
|
|
{ Name: Freyja Myth: Norse Vehicle: Chariot pulled by two cats Weapon: Magic called seior 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: Mjolnir } { Name: Ares Myth: Greek Vehicle: N/A Weapon: A spear stained in blood Name: Athena Myth: Greek Vehicle: N/A Weapon: Two swords and a spear; is allowed to use Zeus's thunderbolt Name: Hera Myth: Greek Vehicle: N/A Weapon: Her cleverness Name: Poseidon Myth: Greek Vehicle: N/A Weapon: Three-pronged spear called Trident Name: Zeus Myth: Greek Vehicle: N/A Weapon: Lightning } |
{ Name: Thor Myth: Norse Vehicle: Chariot pulled by goats Tanngrisnir and Tanngnjostr Weapon: Mjolnir } { Name: Hera Myth: Greek Vehicle: N/A Weapon: Her cleverness } |
The way the list is written, when add_ordered() is called, there should already be at least one element in the list. |
If trav->m_succ or trav->m_prev is NULL, there's only one member in the list. |
But I'm not sure if I should allocate memory for it |
and then move there or just use advance() or do trav = trav->m_succ or trav = trav->m_prev to move there to insert a new element, depending on whether the name string of the God in the element I want to insert is lexicographically greater than or less than the one in the current element I'm comparing it to. |
And because of the way advance() is written, since it also tests for NULL, I thought I'd have to use that if m_succ or m_prev is NULL and I want to go it. |
If it successfully moves to where it has to be to insert the new element, |
it should insert it there in the correct position. |
^If I'm not mistaken, this would be expected output if add_ordered() works. |
No, this is not quite true. Can you tell me why? Or can you prove me wrong? |
Please clarify this statement, simplify it to each specific item. You're grouping too many discrete steps in one large sentence. |
True, but how does the program know the "correct" position and how are you going to place the element into the "correct" position? |
Maybe, but you really need to rework your main() so that all it has is a single list, and only "inserts" the elements and doesn't do anything else that may confuse the issue. |
|
|
I'm not the one who wrote that advance() function, remember? |
All I did was convert it into a member function of the class. Or at least try to. |
I thought it would be that way because: when there's only one element in the list, wouldn't its previous and next pointers both be pointing to NULL? Or am I misunderstanding something? |
If trav->m_succ or trav->m_prev is NULL, there's only one member in the list. |
Basically, what I'm saying is that I'll try to traverse the list. If the name string in the new element I want to insert is lexicographically "greater" than the element already in the list that I'm looking at, I'll follow the current element's next pointer and insert the new element at that location. Otherwise, follow the previous pointer and insert the new element there. |
It should be the correct position since I'm doing a lexicographical compare. |
How about this for main(), then? |
|
|
p is pointing to: Zeus p is pointing to: Poseidon p is pointing to: Ares p is pointing to: Athena p is pointing to: Hera |
|
|
Basically, what I'm saying is that I'll try to traverse the list.(1) If the name string in the new element I want to insert is lexicographically "greater" than the element already in the list that I'm looking at, I'll follow the current element's next pointer and insert the new element at that location.(2) Otherwise, follow the previous pointer and insert the new element there.(3) |
What are the problems in those statements? |
Basically, what I'm saying is that I'll try to traverse the list.(1) |
If the name string in the new element I want to insert is lexicographically "greater" than the element already in the list that I'm looking at, I'll follow the current element's next pointer and insert the new element at that location. |
Otherwise, follow the previous pointer and insert the new element there.(3) |
And I think part of the problem I'm having is also to do with the fact that I can't tell how to not overwrite stuff when there's more than one element in the list. |
I'm traversing the list by following the next and previous pointers. To go forward, use next pointer. To backward, follow the previous pointer. |
I did test for "less than" in the code when I used std::lexicographical_compare(). I read the documentation, so I know that it returns true if the first string is "less than" the second. So when I want to test for "greater than", I either do an "else" or I use the ! operator on the "if" checking the function's return value. |
But how is what I said there incorrect? Are you saying I shouldn't insert *n into the list before the current element if it's "less than" the current element and after if it's "greater"? |
As for overwriting stuff, |
but more because what did get entered got overwritten by whatever got entered after it. |
Basically, what I'm saying is that I'll try to traverse the list.(1) If the name string in the new element I want to insert is lexicographically "greater" than the element already in the list that I'm looking at, I'll follow the current element's next pointer and insert the new element at that location.(2) Otherwise, follow the previous pointer and insert the new element there.(3) |
Okay, how do you know which pointer to traverse? Where do you make this decision? |
|
|
What is this "stuff" you keep talking about, be specific? |
TheIdeasMan wrote: |
---|
Now insert a 3rd item: A big clue here, it's slightly different to the previous step . Do you know why? Actually, follow the instructions for inserting the second item: does it produce the correct results? It might do, depending on what the values are. OTOH it might not be for this or successive insertions, depending on the values. What do you need to do to make it work? Once you figure out why, you can generalize what an insertion looks like for any amount of the values already in the container. This is where you are going wrong at the moment. |
TheIdeasMan wrote: |
---|
This methodology an be used in 2 ways: To see where the current thinking goes wrong ; and what actually happens when it is done correctly. The first part leads to the realization of how to do the second part in practise (That is, in code), not just in the sense of knowing intuitively what the answer should be. |
Is that logic correct? |
I could make a counter for the number of non-NULL m_succ and m_prev pointers. I've noticed that the number of elements in the list is that number + 1. |
Or should I keep using a while-loop? |
And can both operations be done in a single loop, or should I use two different loops? |
|
|
|
|