|
-
September 26th, 2010, 02:31 PM
#11
Re: Call Array Member in function Help
Again, thanks for the replies. Some, I admit, are a little over my head. I mean, what I want to do sounds simple in my head, but it's totally confusing when writing it. Forgive me, I am just a mathematician trying to pick up on the programming part. I used to get along fine with a piece of paper and a pencil before. C++ is another story...
Anyway, I kind of wrote a linked-list before reading your post Eri523. I liked potatoCode's stuff too but couldn't get my head around it. Well, like I said, I tried writing a linked-list. And since I already wrote it, can you guys check it out anyway?
The idea is that I will call function_two with some pointer as a parameter, then it will do something to that node, and then call function_two on the next node via a pointer. Sounds simpler no?
In other words:
1. function_two (on first node)
2. do stuff to it
3. function_two (next node)
Two issues:
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?
2. How do I even pass the "next" pointer as a parameter? I don't know the syntax.
Please check what I have.
Code:
#include <iostream>
using namespace std;
struct Nodes
{
int number;
Nodes *link;
};
typedef Nodes* Block_Ptr;
void function_two(/*pointer to a node*/);
void insert_node(Block_Ptr &top, int num); //insert a new node, and modify its number member in Nodes
int main(){
do{
Block_Ptr top;
top = new Nodes;
top->number = 2;
top->link = NULL;
insert_node(top, 1);
insert_node(top, 0);
function_two(/*pointer to some specified node, maybe use number member?*/);
}while(/* something */);
return 0;
}
void insert_node(Block_Ptr &top, int num)
{
Block_Ptr tmp_ptr;
tmp_ptr = new Nodes;
tmp_ptr->number = num;
tmp_ptr->link = top;
top = tmp_ptr;
}
void function_two(/*Ponter to some specified node*/)
{
// do stuff and then move on to next node
function_two(/*Pointer to the next node*/)
}
This seems easier to follow except for my 2 questions.
Thanks for any help fellas.
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
|