| devonrevenge (897) | |
|
oh yeah of course | |
|
|
|
| fun2code (1227) | |
|
Good. You got all of that yourself except how to judge when to stop iterating through the list. That method of iterating until NULL (or some other certain value) is found will be key in other functions for finding a desired place in the list, like inserting elements in order, etc. I have another task for you to try. You have an addNode() for adding a node at the front of the list. I guess you didn't like push_front! Doesn't matter of course, but now another function name is needed. Write a function to add a node at the end of the list. The common (boring) name is push_back, but choose what you wish. You will need to do what is done in the displayList(), but you need to stop 1 node sooner. Iterate until conductor->next = NULL. Then conductor points to the last node in the list. Just tack a new node on right there! | |
|
|
|
| devonrevenge (897) | |||
it only displays the last node, i suspect that i didn't re-connect the chain somehow but my mind got cloudier and cloudier and soon enough i couldn't remember if NULL is the front or the back, and who is behind whom.
| |||
|
Last edited on
|
|||
| cire (2362) | |||||
So, once conductor->link is NULL, you assign a value to it, which means it is no longer NULL, which means the loop continues. The pointer is advanced, so now conductor is == newnode, so you set its link to point to itself and then to NULL, finally causing the loop to stop. You return a pointer to the last element in the list, when you should be returning a pointer to the first.
| |||||
|
Last edited on
|
|||||
| devonrevenge (897) | |
|
i see what i did, thanks for explaining. i have to think the way you did it for a while though. i guess i was a long way from finding that solution myself | |
|
Last edited on
|
|
| devonrevenge (897) | |
|
why does it return conductor? im going to see what happens if its void too. i can see how it works but im not crystal just yet, i feel like i need to practice more, thanks cire. | |
|
Last edited on
|
|
| cire (2362) | ||||
Well, the code isn't quite complete. Perhaps if it were it would be more obvious. We need to handle the case where conductor is 0/NULL/nullptr. What do you think push_back should do in that case? Maybe it should just create a new list?
push_front already handles the nullptr case nicely. If I were designing a list in this fashion (as opposed to making it a class like we should) I would be more inclined to pass a reference to a pointer to the head of the list so that it wouldn't be necessary for the caller to assign the return value to the list pointer when the head was modified. Making it a class, of course, would be a much better solution. | ||||
|
|
||||
| devonrevenge (897) | |
| oh i thought you just gave me the answer, i was strangley happy when it didnt work properly cos it didnt make sense anyway. | |
|
|
|
| devonrevenge (897) | |||
this, this isnt what you meant by putting it in a class is it, though it is in a class
| |||
|
Last edited on
|
|||
| Catfish2 (666) | |||
|
Finally. Now all you need to do is store your base inside the class, and make your push_front() automatically use it. And then your code can look like:
| |||
|
|
|||
| fun2code (1227) | |||||||||||||||
|
@devonrevenge Nice try but, as cire pointed out, making the assignments from inside the while loop causes problems. In the push_back function the only thing we are using the while loop for is to locate the last node in the list. We can know we have reached the last node because it is the only node that has next = NULL (which signifies that there is no next node). Hence:
Will do the job. The while loop ends when conductor->link = NULL. conductor will be pointing to the last node following this while loop. So, finish this function here:
Then I will have another task for you. I see that you have created a list class, defined the node structure in it, added a 2nd data member, etc... While this is where we are going, at this point I think it introduces unnecessary complication. I'd prefer to keep our model of a link list simple, so we can focus on how it works. @cire. The advice you give is good but I think that you are introducing new concepts too quickly. Let's step back a moment, OK? What is required at minimum to create and use a link list?
We belong at baby step #4 away from this right now. @devonrevenge. When you get that push_back() working right then we will look at a a way to make it more efficient. OK? EDIT: When I look at the above code for baby step 1 I see that I missed a baby step! I should have gone from this:
To this:
To this:
Actually, the next thing needed here is a cleanup() function for deleting the nodes when we are done with them. Note: This global cleanup() function will become the basis for a list class destructor later. EDIT2: Notice that with the new node structure constructor, a push_front() may now be written as:
It's hard to get much simpler than that! | |||||||||||||||
|
Last edited on
|
|||||||||||||||
| devonrevenge (897) | |||
can someone tell me why this function did not work?
is it because im connecting to the wrong end wait temp = conductor was supposed to be conductor->link = temp but it dont workify anyhow. | |||
|
Last edited on
|
|||
| cire (2362) | |||
You didn't preserve a pointer to the head (first element) of the list and you leaked your memory right after you allocated it.
| |||
|
|
|||
| Catfish2 (666) | |||
|
Minor point: with that while() you don't need the if(). The code doesn't work because... you're simply throwing away the data that temp points to.
| |||
|
|
|||
| fun2code (1227) | |||
|
RESET Complete this function:
| |||
|
Last edited on
|
|||
| devonrevenge (897) | |||
|
didnt see you changed your post up there fun2code, thanks for the help, and incredible patience, i feel a bit slow on this one. EDIT; wow for the return new node thing!!
is this supposed to work? ithink i might have an error elsewhere if it is, i been at this ALL day :O | |||
|
Last edited on
|
|||
| devonrevenge (897) | |
| why does it show only two of the last?? its not hooked up to summink again is it :P | |
|
|
|
| devonrevenge (897) | |||
:D
theres something i realy cant get the feeling of, i should have tried to draw the whole thing, its working out which way theyre linking and where im putting what new node...ive been trying to put square pegs in round holes for five hours, its a good puzzle though. | |||
|
Last edited on
|
|||
| fun2code (1227) | |||||
|
The problem is that we changed conductor within the function, and then returned this changed value. I'm sorry. I'm the one who is slow here. The value of front will not be changed by a push_back() operation. push_back() does not need to return anything here! It should be:
Which would be called like so:
| |||||
|
Last edited on
|
|||||
| devonrevenge (897) | |
|
my thanks for all your help fun2code, you seem like a professional teacher, or maybe a sentient AI? either way maybe i wouldnt have understood this all by myself + google as soon as i have :D, am gonna have a play with them now and then get some undeserved sleep and start again tmrw | |
|
|
|