Jump to content
  • Advertisement
Sign in to follow this  
BaldPython

Recurisve LinkList Problem

This topic is 4880 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Quote:
Original post by that1guy
In your count(), traverse() and traverse_reverse() functions change

if (L == 0)
.
.
.

to

if (L == NULL)
.
.
.

I could be wrong.. it's been a LONG time since I've had to mess with pointers but I know it was drummed into me to check NULL not 0.


You are wrong ... and the correct way to code in modern C++ is to compare against 0 ... NULL is not even defined by the compiler unless you include certain header files. The correct value to set a pointer that doesn't point anywhere to is ALWAYS 0 ... period ... because this is the only value for which the delete operator is defined not to blow up on.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by LiyonDR
After further testing the rFind function, my proposed resolution doesn't resolve anything properly and unfortunately I don't have more time to step through it. So the && doesn't work right either, but it does cause it to check the next node.

The || will cause it to return only 0 unless you pass it the value at the start of the list, as an && seems to work if you pass it the value at the end of the list.

Probably not too helpful but if you look into that more you should be able to fix it.


Well, I am not that much concerned about the rFind() function actually, I'll fix it later, i am alot worried about that zero that comes always in the end or at start. I am just about to start banging head on wall in anger.


Share this post


Link to post
Share on other sites
Quote:
Original post by BaldPython
Quote:
Original post by that1guy
traverse_reverse() has a bug where it SHOULD only print 0 1 2 3 4 5 6 7 8 9.


No! sir, the last element of list is 10, So it should print it too ...

Quote:

It should skip 10 because 10's node->next is null so it shouldn't get to the std::cout... line.


Quote:

There IS a bug in your insert function if you pass in a NULL List value it'll create a new one and then link yet another one to it.


its a circular list, hence the new list is only allocated if List L is NULL( if no place is allocated to it. )


But... but... it's not a circular list.. =) the initialize function set's the first node's next pointer to null, then on each insert, the new node's next is set to the head node's next (so on the first iteration, it's set to null), and then head node's next is set to the new node. Hence the tail will always be null If it's MEANT to be circular, change the initialze function to have the node's next pointer point to itself rather than null.

As for this being a HW question.. while we are giving some answers, he is TRULY thinking it through and trying to understand them, so IMHO it should be ok.

Share this post


Link to post
Share on other sites
Quote:
Original post by Xai
Quote:
Original post by that1guy
In your count(), traverse() and traverse_reverse() functions change

if (L == 0)
.
.
.

to

if (L == NULL)
.
.
.

I could be wrong.. it's been a LONG time since I've had to mess with pointers but I know it was drummed into me to check NULL not 0.


You are wrong ... and the correct way to code in modern C++ is to compare against 0 ... NULL is not even defined by the compiler unless you include certain header files. The correct value to set a pointer that doesn't point anywhere to is ALWAYS 0 ... period ... because this is the only value for which the delete operator is defined not to blow up on.



Hey, ya learn something new everyday.. but then again.. every C++ compiler I've used (which admittedly only consists of VS6, and 7, and Borland C++ 8) finds null (note not NULL) a perfectly acceptable value. Then again we could get into the whole "those compilers aren't REALLY compatable with the FULL C++ standard" argument, but, it's all really boring.. null works, 0 works, I guess. 'nuff said.

Share this post


Link to post
Share on other sites
Your list is not a circular list, at least not from what I can tell. A circular list is one where the last node points back at the root node. Ie:

A --> B --> C --> D --> A --> B .. etc.

Aside from the other errors of printing out and whatnot, the actual problem you have (the extra '0' in the list) is due to what's happening during initialize and insert.

Answer me the following questions:

(1) When initialize(x), what does your list look like?
(2) When the first insert(x, i) [i==1] has finished execution, what does your list look like?

Answer those two questions and you should have the bug glaring back at ya :)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I understand the educational aspect of creating the list data structure and tying in the recursion in the same project... but... CAN YOU SAY STACK OVERFLOW.

I am not sure how many items you could add to that list before you get one by calling those recursive methods to deal with the list. It will just lead you down a bad road that you really do not want to take in more robust programs that could have millions of items in that list.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!