actube/src/cw/mlist_delete.c
7u83@mail.ru 3ae4f89bea join works
FossilOrigin-Name: 85bcc56e6aca1fbb8c437e74bc6bdc16d378a510e8e48a7fb58b996e919e138f
2018-04-07 17:28:00 +00:00

61 lines
871 B
C

#include <string.h>
#include <stdio.h>
#include "mlist.h"
struct mlistelem * mlist_delete(mlist_t list, void *data)
{
struct mlistelem *e;
mlist_foreach(e,list){
void *tdata = mlistelem_dataptr(e);
if (list->cmp(tdata,data)==0){
break;
}
}
if (e == NULL)
return NULL;
if (e->prev==NULL && e->next==NULL){
if (list->del)
list->del(e);
free(e);
list->count=0;
list->first=NULL;
list->last=NULL;
return NULL;
}
if (e==list->first){
list->first=e->next;
e->next->prev=NULL;
if (list->del)
list->del(e);
list->count--;
free(e);
return NULL;
}
if (e==list->last){
list->last=e->prev;
e->prev->next=NULL;
if (list->del)
list->del(e);
list->count--;
free(e);
return NULL;
}
e->next->prev=e->prev;
e->prev->next=e->next;
if (list->del)
list->del(e);
list->count--;
free(e);
return NULL;
}