首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

请问个链表的有关问题

2012-10-23 
请教个链表的问题....前面略声明个结构:struct film{char title[20]int nstruct film * nextnext为指向

请教个链表的问题
....前面略
声明个结构:
struct film{
  char title[20];
  int n;
  struct film * next; next为指向下一个结构的指针
  };
int main()
{
  ........
  struct film * head; 头指针
  struct film * current; 当前指针
  ......
  current=(struct film *)malloc(sizeof(struct film));
  创建一个链表,此处略


  我的问题是释放malloc分配的内存块时,书中使用了下面的代码:
  current=head;
  while(current!=NULL)
  {
  free(current);
  current=current->next; current指向的结构体不是被free刚刚释放了吗,怎么能找得到他的指针成员啊?
  }
  

  我觉得这里加入一个新的指针struct film * p才是对的:
  
  current=head;
  while(current!=NULL)
  {
  p=current->next;
  free(current);
  current=p;  
  }
请高手给说说

[解决办法]
我也觉得你的答案是正确的 已经释放了 怎么还会指向?

热点排行