Is this prog fine and if so how would I go about
freeing memory set up for the nodes in a seperate function
also why is it a bad idea to store data in the top node.
Code:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 30
void new_node(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_node(node *top,int amount); // pass top node and amount to function
getch();
return 0;
}
void new_node(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;
}