[Dev-C++] Single Linked List Problem
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <and...@su...> - 2003-06-06 01:58:21
|
Dear Users,
I need to discover a way to use the "free" command to correctly avoid 5
records dinamically allocated in a simple Single Linked List program.
Here are some pieces of it:
...
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
struct address
{
char name[40];
char phone[20];
struct address *next;
};
void slstore(struct address *, struct address **, struct address **);
...
int main()
{
struct address *info, *start, *last;
init(&start);
start=NULL;
init(&last);
last=NULL;
for(i=0; i<MAX; i++)
{
init(&info);
printf("Enter name: ");
gets(info->name);
printf("Enter phone: ");
gets(info->phone);
slstore(info, &start, &last);
}
...
system("PAUSE");
return(0);
}
void slstore(struct address *i, struct address **start_arg,
struct address **last_arg)
{
if(*last_arg==NULL) *start_arg=i;
else (*last_arg)->next=i;
i->next=NULL;
*last_arg=i;
}
void init(struct address **arg)
{
*arg= malloc(1*sizeof(struct address));
if(!(*arg))
{
printf("sem memória\n");
system("PAUSE");
exit(EXIT_FAILURE);
}
}
***
The slstore function stores the new element in the last position. It´s
"next" record is filled
with the NULL address to mean that there aren´t more elements in the list.
At the end of the program 5 memory spaces of 64 bytes each one, are
allocated for each element.
How can I use the free command to return the memory for the computer?
Thanx
Andre
|