#include<stdio.h>
#include<stdlib.h>
struct node {
int i;
struct node* next;
};
int rm_if(int i) {
return i % 2;
}
void gen_list(struct node** head, int i) {
if (i < 0)
return;
*head = (struct node*)malloc(sizeof(struct node));
(*head)->i = i;
(*head)->next = NULL;
gen_list(&((*head)->next), i - 1);
}
void del_odd(struct node** head, int(*fn)(int)) {
struct node** tmp;
for (tmp = head; *tmp; ) {
struct node* entry = *tmp;
if (fn(entry->i)) {
*tmp = entry->next;
free(entry);
} else
tmp = &entry->next;
}
}
int main() {
struct node** head, **tt;
gen_list(head, 100);
del_odd(head, rm_if);
for (tt = head ; *tt;) {
printf("%d ", (*tt)->i);
tt = &((*tt)->next);
}
return 0;
}
linus建议的删除单链表指定节点的方法
最新推荐文章于 2022-02-24 23:37:36 发布