为什么这段代码在else里加上free(*pphead)后在打印的过程中会报错,而不加的话就没问题
void SListPopFont(SLTNode** pphead)//头删
{
assert(*pphead != NULL);
SLTNode* tail = *pphead;
if (tail->next==NULL)
{
free(*pphead);
}
else
{
SLTNode*head = *pphead;
free(*pphead);
*pphead = head->next;
}
}
void SListPrint(SLTNode* phead)//打印
{
SLTNode* cur = phead;
while (cur!=NULL)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("NULL\n");
}
这么改下,供参考:
void SListPopFont(SLTNode** pphead)//头删
{
assert(*pphead != NULL);
SLTNode* tail = *pphead;
if (tail->next==NULL)
{
free(*pphead);
}
else
{
SLTNode*head = *pphead;
*pphead = head->next; // 修改
free(head); //free(*pphead); 修改
}
}