当我在其中输入数据时,curr->data
我首先通过curr = (NODE*) malloc (sizeof(NODE));
. 然后我建立列表并最终在最后打印它。在程序的某个地方,我必须释放我用过的内存,但我不知道什么时候该做。在建立列表之后,还是在我打印列表之后?或者我可以这样做吗?
printf("How many elements do you want to make? ");
scanf("%d", &NumElem);
head = NULL;
for (i = 0; i < NumElem; i++)
{
//memory allocate
curr = (NODE*)malloc(sizeof(NODE));
printf("Number %d: ", i+1);
scanf("%d", &curr->num);
FLUSH;
if (head == NULL)/*beginning of the list*/
{
curr->next = head;
head = curr;
*tail = curr;
}
else /*adding on the list*/
{
curr->next = head;
head = curr;
}
free (curr);
}//for
每次将数据放入当前节点后,我可以释放吗?