please let me know if this is ok as it is my first linked list example
I do not what my code changed as I will deal with that later.
I just want to know is this fine up to this point and after new_nodes
has finished I want to send the pointer new_nodes to another function
to free all the memory(please show me with code), and why is it bad
practice to store data in the top node.
I know this is old style waiting for new compiler to be delivered.
Code:#include<iostream.h> #include<conio.h> #include<stdlib.h> #define MAX 30 void new_nodes(node *top,int amount); struct node{ int age; char name[MAX]; node *next; }; int main() { int amount; node *top; cout<<"How many people to be stored >"; cin>>amount; if(amount==0) exit(1); top=new node // memory set aside for the top node void new_nodes(node *top,int amount); // pass top node and amount to function getch(); return 0; } void new_nodes(node *top,int amount) { int loop; node *new_node=top; // pointer of type node points to the top node for(loop=0;loop<amount;loop++) { new_node->next=new node; // ptr new_node points to spare memory, the size of node(the loop node) new_node=new_node->next; // ptr new_node now points to the next element in the loop node cout<<"Enter persons name >"; cin>>new_node->name; // Enter name in second node cout<<'\n'<<"Enter persons age >"; cin>>new_node->age; // Enter age in second node } new_node->next=NULL; }
Thanks all.