第1个回答 2012-08-27
2种都是可以的.
只是二级指针会修改到头节点.
Destroy(head);// 使用此法,此时head的地址没有被设置成NULL
// Destroy(&head);// 使用此法,此时head的地址被设置成NULL
以下是测试代码.
#include<stdio.h>
#include<stdlib.h>
struct StuNode{
int a;
struct StuNode *next;
};
void Destroy(struct StuNode **ph) /*直接用*h的话*/
{
struct StuNode*p;
p=*ph;/*如果直接用p=h不行吗?*/
while(p!=NULL)
{
*ph=p->next;
free(p);
p=*ph;
}
printf("已删除\n");
*ph=NULL;
}
void Destroy(struct StuNode *h)
{
struct StuNode*p;
p=h;
while(p!=NULL)
{
h=p->next;
free(p);
p=h;
}
printf("已删除\n");
h=NULL;
}
void main()
{
int i;
struct StuNode *head;
struct StuNode *p = (struct StuNode*)malloc(sizeof(struct StuNode));
p->a =0;
p->next = NULL;
head = p;
for(i=1;i<4;i++)
{
p->next = (struct StuNode*)malloc(sizeof(struct StuNode));
p->next->next=NULL;
p=p->next;
p->a = i;
}
Destroy(head);// 使用此法,此时head的地址没有被设置成NULL
// Destroy(&head);// 使用此法,此时head的地址被设置成NULL
}本回答被提问者采纳