C 更改tld\u列表\u添加功能

C 更改tld\u列表\u添加功能,c,binary-tree,C,Binary Tree,有没有办法缩短这部分代码?对于这个add函数。有一些困难。基本上,我想做一个列表,生成域的TLD,并根据用户输入日期的方式显示它。但我觉得我的代码相当长,我希望缩短它。这是我希望缩短它的函数之一 int tldlist_add(TLDList *tld, char *hostname, Date *d) { // check if it's within the tld dates if (date_compare(tld->begin, d) > 0 ||

有没有办法缩短这部分代码?对于这个add函数。有一些困难。基本上,我想做一个列表,生成域的TLD,并根据用户输入日期的方式显示它。但我觉得我的代码相当长,我希望缩短它。这是我希望缩短它的函数之一

int tldlist_add(TLDList *tld, char *hostname, Date *d) {
    // check if it's within the tld dates
    if (date_compare(tld->begin, d) > 0 ||
            date_compare(tld->end, d) < 0)
        return 0;

    // capture the actual tld
    char *tld_start = strrchr(hostname, '.') + 1;
    int tld_len = strlen(tld_start);
    char *tld_s = (char *)malloc(sizeof(char) * (tld_len + 1));
    tld_s[tld_len] = '\0'; // make sure there is a null end
    strcpy(tld_s, tld_start);

    if (tld->root == NULL) {
        // list is empty, just add to root
        TLDNode *node = tldnode_create(tld_s);
        node->count++;
        tld->root = node;
        tld->size++; // new node
    } else {
        // add to list. do we need to free the string?
        if (!tldlist_add_r(tld, tld_s, tld->root))
            free(tld_s);
        else {
            tld->size++; // new node, so increase size

            // we must now rebalance the tree
            tldlist_rebalance(tld);
        }
    }

    tld->count++;
    return 1; // maximum success
}
int-tldlist\u-add(tldlist*tld,char*hostname,Date*d){
//检查是否在tld日期内
如果(日期比较(tld->begin,d)>0||
日期比较(tld->结束,d)<0)
返回0;
//捕获实际tld
char*tld_start=strrchr(主机名,'.')+1;
int tld_len=strlen(tld_开始);
char*tld_s=(char*)malloc(sizeof(char)*(tld_len+1));
tld_s[tld_len]='\0';//确保有空端
strcpy(tld\U s、tld\U start);
如果(tld->root==NULL){
//列表为空,只需添加到根
TLDNode*node=TLDNode_create(tld_s);
节点->计数++;
tld->root=节点;
tld->size++;//新节点
}否则{
//添加到列表。是否需要释放字符串?
如果(!tldlist\u add\u r(tld,tld\u s,tld->root))
免费(tld_s);
否则{
tld->size++;//新建节点,因此增加大小
//我们现在必须重新平衡这棵树
tld列表再平衡(tld);
}
}
tld->count++;
返回1;//最大成功率
}

在我看来并不是一个特别长且复杂的函数。你为什么要缩短它?是否存在性能问题,或者只是源代码行的数量?我需要帮助理解此代码,为什么我们必须对整个malloc进行计数?您提供的代码并不完整,也没有真正的问题
malloc
为TLD的字符串表示(例如“.com”)分配空间,就我所见,与
TLDNode
中的计数器变量
count
无关。从一个基本的C教程开始,然后开始。但是我必须迭代一个二叉树,并为它做一个计数器+?你们还需要什么代码?这个代码让我有点困惑,为什么会出现语法错误:缺少“;”在此代码的“type”之前:char*tld_start=strrchr(主机名“.”)+1;