struct person
{
int age;
char name[100];
struct person *next;
};
void delfirst(struct person **p)// For deleting the beginning
{
struct person *tmp,*m;
m = (*p);
tmp = (*p)->next;
free(m);
return;
}
void delend(struct person **p)// For deleting the end
{
struct person *tmp,*m;
tmp=*p;
while(tmp->next!=NULL)
{
tmp=tmp->next;
}
m->next=tmp;
free(tmp);
m->next = NULL;
return;
}
我正在寻找两个单独的函数来删除链表的第一个和最后一个元素。这是我试过的。你有什么建议?特别是首先删除对我来说是个问题。
答案 0 :(得分:2)
if (!p || !(*p))
return;
struct person *tmp;
tmp = (*p);
(*p) = (*p)->next;
free(tmp);
return;
void delend(struct person **p)// For deleting the end
{
if (!p || !(*p))
return;
if (!(*p)->next)
{
*p = NULL;
}
struct person *tmp,*m;
tmp=*p;
while(tmp->next->next!=NULL)
{
tmp=tmp->next;
}
free(tmp->next);
tmp->next = NULL;
return;
}