#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建议的删除单链表指定节点的方法
最新推荐文章于 2024-03-03 09:43:00 发布
本文介绍了一段使用C语言实现的链表操作示例,包括生成链表和删除奇数节点的功能。通过`rm_if`函数判断节点值是否为奇数,并使用递归方式构建链表,然后通过`del_odd`函数遍历并删除所有奇数节点。
1778

被折叠的 条评论
为什么被折叠?



