| fun2code (1227) | |||
|
You're welcome. I wish I was a better teacher. I keep making mistakes. The push_back() above has a critical flaw. It can't be used to add the first node to a list.
Why? Hint: Line #9 in the push_back(). I will think more on this and get back later. | |||
|
|
|||
| devonrevenge (897) | |||
this is what im struggling to grasp...
this works but i dont see whats pointing to NULL anymore. newnode->link = conductor is identical to push_front. so theres the new node at the front of the list. so then we cycle down to the end of the list. and link the would be NULL pointing linker to the new node sitting at the front. thats a big loop in my head. so when i push_back a new node how come it finds a null again?? wait is it beacuse it doesnt replace null it ops inbetween null and the end because of conductor->link?? so its still connected into a loop? just one node points to null somehow??? | |||
|
|
|||
| fun2code (1227) | |
Line 6 is supposed to be: newnode->link = NULL;There's the NULL at the end of the list. | |
|
|
|
| devonrevenge (897) | |
WHAAAT!? that makes logical sense to me, i put it straight in there, i was like cant be initialized to anyting esle, however i only get the last line of code as an output!!! if i put conductor in however i get all the code in the right place!!!i wasnt understanding this at all, im glad its wrong again, cos it doesnt make sense (even though in this case it works) | |
|
|
|
| fun2code (1227) | |||||
|
What are you doing with the return value from push_back() ? Don't use it. The function returns conductor, but only after it was iterated to point to the end of the list. Don't assign that to to your front node in main(). I've been playing around with the node class a bit and I came up with this nifty recursive displayList function. It takes a character argument to tell which direction to display the list in:'f' = forward and 'r' = reverse: Just call it with a node in main()
Or, how about this dandy recursive push_back() function for the node class?
| |||||
|
Last edited on
|
|||||
| devonrevenge (897) | |
| wooow i will study these, thanks fun to code, im a beginner so everynow and again my basics are unstable, takes a lot longer to remember how simple things work if my brain is allready taxed | |
|
|
|
| devonrevenge (897) | |
| the code dosnt work with your (correct im sure) version of push back, do you think it could be the rest of the code and not your function | |
|
|
|
| fun2code (1227) | ||||
|
I hope it's the rest of the code. I've gone a bit rogue here in my effort to avoid creating a list class. Thought I'd see how much I can do using the node class alone. It's working for me, so here's what I have:
Output:
As expected. | ||||
|
|
||||
| devonrevenge (897) | |||
for push back why wasnt creating a new node and getting the pointer that would point to NULL to point to the new node not working?#
| |||
|
Last edited on
|
|||
| fun2code (1227) | |
|
The function is returning a pointer to the last node in the list (the one just created with new right?). Are you doing this in the main()? front = push_back(front, 7);If so, don't do that! Call push_back() by itself. Throw away the return value (unless you WANT to save a pointer to the tail node that is). ie: push_back(front, 7);// don't assign return value to front Is that what's going on? | |
|
|
|
| devonrevenge (897) | |
|
yup -_- i was calling it wrong, i just forgot about that end of the code. it doesnt work still, your canny link thing does though but i would like to have this in me=y head a lttle clearer | |
|
|
|
| fun2code (1227) | |
|
Of course. Please post the entirety of your code so I can figure out what's wrong and we can get back on track here. | |
|
|
|
| devonrevenge (897) | |||
here, again thanks for your help, more like tutoring here
| |||
|
|
|||
| naraku9333 (1038) | |||
| |||
|
|
|||
| fun2code (1227) | |
|
naraku9333 nailed it on the head. Change line 12 per narakus note line 12 and you will find that your peogram works perfectly. OMG I am so pissed right now! I had a great post prepared, and it was long. I hit the "preview" button and the browser replies with "permission denied". I found I was suddenly not logged in and my post was lost. I don't have the energy to redo it. Sorry. Some advice: ALWAYS copy your post to notepad or something before hitting that preview button! (I'm pissed because this has happened to me 4 times in the last 2 days. The other way I've found in which a post ca be instantly and irretrievably lost is to hit the backspace key (thinking that you are going to delete the last character you entered) but somehow the focus left the editing window, so the backspace is interpreted as the shortcut for "go back one page" in the browser!! And then your post is gone! | |
|
|
|
| LowestOne (895) | ||||
A wrong assumption. push_back accepts a pointer to node, but it creates it's own pointer (it isn't by reference). The only way to maintain a list is to return the function's pointer. I would change the output function a little:
A little more educational that way, to see the addresses these things point to. Especially useful if you want to start deleting nodes in the middle of the list. | ||||
|
Last edited on
|
||||
| devonrevenge (897) | |
|
-BOOM-Success, and the double bonus is i understand too, we return conductor->link because thats what newnode is now XD thanks guys perhaps when im really good i can return the favour by compiling loads of simple stuff for you :) okay fun2code, i got one of those over sensitive touch pads so thats not such a bad idea. in uk pissed means drunk too LOL | |
|
|
|
| naraku9333 (1038) | |||
@devonrevenge I'd say your list is more of a c-style list (not that it's a bad thing) but you will run into problems if you try to push front after push_back as you aren't keeping track of the front pointer. Since you are obviosly coding c++ class list you should encapsulate the push_front/back functions as class member functions and a front pointer member variable. Then there is no need to return anything from them.
| |||
|
Last edited on
|
|||
| fun2code (1227) | ||||||
|
nraku9333 is right about the need to "encapsulate" the front* within the class. As for the return types of push_front() and push_back(), there is (as naraku pointed out) no need to return anything since we will manage the front pointer within these functions. Also: http://www.cplusplus.com/reference/stl/list/push_front/ shows the STL versions of these functions return void. Shall we go with that? I have prepared a list class for starters. (copying post to clipboard now) Please check this out. Anyone else, please feel free to comment on this. Taking the step suggested by cire and others some 50 posts ago this thread, I suggest for starters:
This compiles and runs without any warnings or errors. (copies post to clipbboard again) This class is quite glaringly lacking a destructor! I'll post back with that! EDIT: prototype: ~list();definition:
I just tested this. No problems here. EDIT2:
That's funny! I am aware of that uk usage, but because that is not the usual usage here in the states, I didn't even think of it! | ||||||
|
Last edited on
|
||||||
| fun2code (1227) | ||||||
|
Bonus function for the new list class! I'm guessing that the number accompanying the word in each node is to indicate the order in which the sentences should appear, when sorted in order by that number. If this is so, then you will like this. An in-place sort for this link list could get messy. But, if we insert them in order as they are added to the list it is easy: So we have (hot off the press and barely tested!):
So, if you replace the push_back, push_front calls in main() with this:
You should get this:
Now, that's a lot to digest so I'll hang back for awhile. | ||||||
|
Last edited on
|
||||||