|
-
September 26th, 2010, 03:37 PM
#12
Re: Call Array Member in function Help
Let's answer your post backwards...
 Originally Posted by M2W
2. How do I even pass the "next" pointer as a parameter? I don't know the syntax.
The "next" pointer can be treated just like the "top" pointer, which already is obvious from the fact that they're both of the same type: Block_Ptr.
Yet again, I strongly suggest to forget recursion here. While recursion is the method of choice for, for instance, binary trees, a linear list is better approached by plain iteration. Considering this, function_two() might look something like this:
Code:
void function_two(Block_Ptr pNode)
{
while (pNode!=NULL) {
// Do something with the Nodes object
pNode=pNode->link;
}
}
1. How do I make the last node point back to the first node? I know I have it as NULL, so I should change it to what? top?
Yes, exactly that. By having the last node point back to the first one, you close the circle. But be aware that, in that case, you can't use the way of terminating the iteration I demonstrated above any more. Passing the pointer to any node of a circular list to the function_two() above would send your program into an endless loop.
So you need some other means of determining that you have scanned the entire list. This has to be done using some property of the node objects, and one particular property is the link pointer. But in this case you wouldn't compare it to NULL but rather to top. (I'm not 100% sure though whether comparing pointers for equality to anything else but NULL would pass the "clean design" check.) Another way of doing this would be using a sentinel node like potatoCode described above for the array, or to implement another struct field solely for that purpose, probably a boolean flag.
But here applies the same I already said about recursion: As far as I know the scenario by now, I don't see any reason why you should prefer the more complicated circular list over the plain linear list.
And finally: You are allocating the list nodes using new, which is the way to do it for a linked list. But you never release them again using delete, which is the most basic sample of a memory leak.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|