#include<stdio.h>
#include<stdlib.h>
#define?N?6
int?main()
{
typedef?struct?st
{
int?num;
struct?st?*next;
}node;
node?*p,?*q,?*h;
p?=?(node?*)malloc(sizeof(node));
h?=?p;
for?(int?i?=?0;?i?<?N;?i++)
{
p->num?=?i?+?0;
q?=?p;?//保存已完成的數(shù)據(jù)的地址
if(i==N-1)
{
p->next=NULL;break;
}
p?=?(node?*)malloc(sizeof(node));//開辟新的地址?
q->next?=?p;//讓完成數(shù)據(jù)的地址和新的地址關(guān)聯(lián)?
}
p?=?h;
while?(p)
{
printf("%d\n",?p->num);//輸出當(dāng)前節(jié)點(diǎn)的數(shù)據(jù)
q?=?p->next;???//更新到下一節(jié)點(diǎn)的地址
p?=?q; //更新下一節(jié)點(diǎn)
}
p?=?h;
while(p)
{
q=p->next;
free(p);
p?=?q;
}?
free(h);
system("pause");
return?0;
}請各位大神幫忙看下,是否讓申請的內(nèi)存充分利用,釋放的時(shí)候應(yīng)該都釋放了吧?在這個(gè)代碼里我沒有用到 h->next ,我覺得反正 h 這個(gè)領(lǐng)頭的也不存數(shù)據(jù), 用h 和h->next應(yīng)該沒區(qū)別吧?
關(guān)于c語言實(shí)現(xiàn)鏈表內(nèi)存的問題
慕粉1600176492
2017-04-17 22:21:17