C和指针之高级指针话题通过函数指针实现在链表中找到特定的值
【摘要】 1、问题
通过函数指针实现在链表中找到特定的值,这里可以是int 类型或者char *类型
思路:
整形数据自己写比较函数,字符串比较用strcmp,然后把这个函数指针传递到函数作为参数。
2、代码实现
#include <stdi...
1、问题
通过函数指针实现在链表中找到特定的值,这里可以是int 类型或者char *类型
思路:
整形数据自己写比较函数,字符串比较用strcmp,然后把这个函数指针传递到函数作为参数。
2、代码实现
-
#include <stdio.h>
-
#include <string.h>
-
-
typedef struct Node
-
{
-
struct Node *next;
-
char value[100];
-
//int value;
-
} Node;
-
-
//在链表中查找找到制定的值
-
Node *search_list(Node *node, void const *value, int (*compare)(void const *, void const *))
-
{
-
while (node != NULL)
-
{
-
if (compare(&(node->value), value) == 0)
-
{
-
printf("hello");
-
return node;
-
}
-
node = node->next;
-
}
-
return NULL;
-
}
-
-
int compare_int(void const *a, void const *b)
-
{
-
if (*(int *)a == *(int *)b)
-
return 0;
-
else
-
return 1;
-
}
-
-
int main()
-
{
-
Node node1, node2, node3, node4, node5;
-
strcpy(node
文章来源: chenyu.blog.csdn.net,作者:chen.yu,版权归原作者所有,如需转载,请联系作者。
原文链接:chenyu.blog.csdn.net/article/details/78726593
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)