@iwktd981220
2017-09-22T07:16:10.000000Z
字数 470
阅读 446
code
如何优雅地从单链表中删除一个entry. 没有标准答案, 自由发挥, 用C实现, 给定function prototype
list_t delete(list_t list, list_t* entry);
这个函数告诉我们, 每次删除都会返回新的链表头, 参数是一个链表头, 一个是要删除的entry.
(主程序略)
typedef struct _list_t {
int num;
struct _list_t *next;
}list_t,*list_f;
list_f *delete(list_t *list,list_t *entry) {
list_t *l;
l = list;
for (size_t i = 1; i < entry->num -1; i++) {
l = l->next;
}
list_t *o,*p,*q;
o = (list_t*)malloc(sizeof(list_t));
*o = *l;
p = (list_t*)malloc(sizeof(list_t));
l = l->next;
l = l ->next;
*p = *l ;
o ->next = p;
return *l;
}