C和指针之高级指针话题通过函数指针实现在链表中找到特定的值

举报
chenyu 发表于 2021/07/27 00:09:38 2021/07/27
【摘要】 1、问题 通过函数指针实现在链表中找到特定的值,这里可以是int 类型或者char *类型 思路:      整形数据自己写比较函数,字符串比较用strcmp,然后把这个函数指针传递到函数作为参数。     2、代码实现 #include <stdi...

1、问题

通过函数指针实现在链表中找到特定的值,这里可以是int 类型或者char *类型
思路:
     整形数据自己写比较函数,字符串比较用strcmp,然后把这个函数指针传递到函数作为参数。
 
 


2、代码实现


   
  1. #include <stdio.h>
  2. #include <string.h>
  3. typedef struct Node
  4. {
  5. struct Node *next;
  6. char value[100];
  7. //int value;
  8. } Node;
  9. //在链表中查找找到制定的值
  10. Node *search_list(Node *node, void const *value, int (*compare)(void const *, void const *))
  11. {
  12. while (node != NULL)
  13. {
  14. if (compare(&(node->value), value) == 0)
  15. {
  16. printf("hello");
  17. return node;
  18. }
  19. node = node->next;
  20. }
  21. return NULL;
  22. }
  23. int compare_int(void const *a, void const *b)
  24. {
  25. if (*(int *)a == *(int *)b)
  26. return 0;
  27. else
  28. return 1;
  29. }
  30. int main()
  31. {
  32. Node node1, node2, node3, node4, node5;
  33. strcpy(node

文章来源: chenyu.blog.csdn.net,作者:chen.yu,版权归原作者所有,如需转载,请联系作者。

原文链接:chenyu.blog.csdn.net/article/details/78726593

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。