Deleting first and last element of a linked list in C
Posted
by LuckySlevin
on Stack Overflow
See other posts from Stack Overflow
or by LuckySlevin
Published on 2010-05-25T16:48:16Z
Indexed on
2010/05/25
17:01 UTC
Read the original article
Hit count: 171
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 seperate 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.
© Stack Overflow or respective owner