删除在C链表的第一和最后一个元素 [英] Deleting first and last element of a linked list in C
本文介绍了删除在C链表的第一和最后一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
结构的人
{
INT年龄;
字符名称[100];
结构*人旁边;
};空隙delfirst(结构人** p)的//对于删除开始
{
结构*人TMP,*米;
M =(* P);
TMP =(* P) - >接着,
免费(米);
返回;}
无效delend(结构人** P)//删除结束
{
结构*人TMP,*米;
TMP = * P;
而(tmp->!下次= NULL)
{
TMP = tmp->接下来,
}
M->接下来= TMP;
免费(TMP);
间 - >接着= NULL;
返回;}
我要找的两个单独的函数删除链表的第一个和最后一个元素。这里是我试过了。你有什么建议?尤其是先删除对我来说是如此困难。
解决方案
如果(P!||!(* P))
返回;
结构*人TMP;
TMP =(* P);
(* P)=(* P) - >接着,
免费(TMP);
返回;无效delend(结构人** P)//删除结束
{
如果(P!||!(* P))
返回;
如果(!(* P) - >接着)
{
* p值= NULL;
}
结构*人TMP,*米;
TMP = * P;
而(tmp->下一步 - >!下次= NULL)
{
TMP = tmp->接下来,
}
免费(tmp->下面);
tmp->接着= NULL;
返回;}
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;
}
I'm looking for two separate functions to delete the first and last elements of a linked list. Here is what I tried. What do you suggest? Especially deleting first is so problematic for me.
解决方案
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;
}
这篇关于删除在C链表的第一和最后一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文