slight problem just can't see the answer
/* ok I now understand single linked lists just have this slight */
/* problem freeing them here */
/* I know when I input 1 for the amount there are two nodes ie top and newnode */
/* so when my program gets down to freeing the memory it outputs memory freed */
/* memory freed (which whould be correct) but when I input more than 1 for the amount */
/* and it gets down to freeing the nodes I still only see memory freed memory freed */
/* when I should be seeing memory freed equal to amount + one more memory freed */
/* for the top node */
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#define MAX 30
struct node{
int age;
char name[MAX];
node *next;
};
int main()
{
int amount;
bool initialized=false;
node *top;
node *newnode;
node *begin;
node *store;
top=(node*)malloc(sizeof(node));
if(top==NULL)
abort();
top->next=NULL;
cout<<"How many people to be stored >";
cin>>amount;
for(int start=0;start<amount;start++)
{
newnode=(node*)malloc(sizeof(node));
if(newnode==NULL)
break;
if(!initialized)
{
top->next=newnode;
initialized=true;
}
newnode->next=NULL;
cout<<'\n'<<"Input your name >";
cin>>newnode->name;
cout<<'\n'<<"Input your age >";
cin>>newnode->age;
}
begin=top;
while(begin->next)
{
store=begin;
begin=begin->next;
if(store!=NULL)
cout<<'\n'<<"Memory freed",free(store);
}
if(begin!=NULL) cout<<'\n'<<"Memory freed",free(begin);
getch();
return 0;
}
/* thanks for all the help */