473,979 Members | 47,193 Online

# Define a C++ functions that create 50 nodes

5 New Member
I am solving some old papers to improve my programming skills. I am a beginer to linked list.
the question is:

Define a C++ functions that create 50 nodes and return the pointer to the first node.
Each node contains its porition number.
the nodes have the following structure:
Expand|Select|Wrap|Line Numbers
1. struct Node
2. {int Data;
4. };
5. Here is what I am thinking:
7. {
8.
9. int count =0;   //a counter for counting the nodes
10. //create a node
11. Node* insert_ptr;
12. insert_ptr = new Node;
13. insert_ptr->Data = d;
15. count = 1;//increment the counter by 1
16.
have a while loop to create 49 more nodes.
this is where i am stuck.am I on the right track?
can anyone give me a pointer as to how i should proceed?
quiz123
Feb 13 '07 #1
3 1648
sicarie
4,677 Recognized Expert Moderator Specialist
I am solving some old papers to improve my programming skills. I am a beginer to linked list.
the question is:

Define a C++ functions that create 50 nodes and return the pointer to the first node.
Each node contains its porition number.
the nodes have the following structure:
Expand|Select|Wrap|Line Numbers
1. struct Node
2. {int Data;
4. };
5. Here is what I am thinking:
7. {
8.
9. int count =0;   //a counter for counting the nodes
10. //create a node
11. Node* insert_ptr;
12. insert_ptr = new Node;
13. insert_ptr->Data = d;
15. count = 1;//increment the counter by 1
16.
have a while loop to create 49 more nodes.
this is where i am stuck.am I on the right track?
can anyone give me a pointer as to how i should proceed?
quiz123
Well, you're on the right track, but there are a few things slipping through the cracks. In the function, you initialize counter to 0. If you call this every time, it won't matter if you increment it, it's always going to be initialized to 0 every time you call it. This should probably be declared in your while loop in main, and not passed - just be how many times the while loop needs to call InsertAtFront.

My recommendation to you would be to write pseudocode of what your main would look like. That way you have something to look at - you can really get an idea of how the entire program is going to behave.
Feb 13 '07 #2
quiz123
5 New Member
Thanks for the piece of advice. I will write my pseudocode and then start coding. I overlooked the part of the counter being set to 0. Thanks once again.
Feb 13 '07 #3
sicarie
4,677 Recognized Expert Moderator Specialist
Thanks for the piece of advice. I will write my pseudocode and then start coding. I overlooked the part of the counter being set to 0. Thanks once again.
No problem, and definitely post again if you get stuck.
Feb 13 '07 #4