IP trie树接口

前两天其他项目组的同学说他们项目中的IP黑白名单要用到trie树,于是我好奇也自己实现了一个IP trie树接口.

在这里保存一下,方便备份以后使用,同时欢迎纠错和交流,希望有大神能指教更高效的算法.

1.头文件如下(iptrie.h)

 1 #ifndef _IP_TRIE_H_
 2 #define _IP_TIRE_H_
 3 
 4 #define SPLIT_SIGN "."
 5 #define IP_BINARY_LEN 32
 6 
 7 typedef struct ip_trie_node
 8 {
 9     struct ip_trie_node *child[2]; //two child node
10 }ip_trie_node;
11 
12 ip_trie_node *create_iptrie_node();
13 
14 void insert_iptrie_node(ip_trie_node *root,char ip[]);
15 
16 int select_iptrie_node(ip_trie_node *root,char ip[]);
17 
18 #endif

2.c文件如下(iptrie.c)

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 
  5 #include "iptrie.h"
  6 
  7 /*
  8  *name: itobinary
  9  *
 10  *param:
 11  * num: orignal number; binary_str: dest string; index: the binary str copy index
 12  *
 13  *return:
 14  * void
 15  */
 16 void itobinary(int num,char binary_str[],int index)
 17 {
 18     if(binary_str == NULL)
 19     {
 20         return;
 21     }
 22 
 23     int i,bit = 0x01;
 24     for(i = 0; i < 8; i++)
 25     {
  //conver integer to 8 bit binary str
 26         if((num & bit) != 0)
 27         {
  //oprater & is lower than != 
 28             binary_str[index + 7 - i] = '1';
 29         }
 30  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值