Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Glib 函数从未排序的链表中删除重复项_Glib - Fatal编程技术网

Glib 函数从未排序的链表中删除重复项

Glib 函数从未排序的链表中删除重复项,glib,Glib,我尝试编写一个函数,从GList*列表未排序的链接列表中删除重复项,并返回不带重复项的GList*列表: GList *remove_dup (GList *list) { GList *a, *b, *dup; a = list; /* Pick elements one by one */ while (a != NULL && a->next != NULL) { b = a; /* Compare

我尝试编写一个函数,从
GList*列表
未排序的链接列表中删除重复项,并返回不带重复项的
GList*列表

GList *remove_dup (GList *list)
{
    GList *a, *b, *dup;
    a = list;

    /* Pick elements one by one */
    while (a != NULL && a->next != NULL) {
        b = a;

        /* Compare the picked element with rest of the elements */
        while (b->next != NULL) {
            /* If duplicate then delete it */
            if (a->data == b->next->data) {
                /* sequence of steps is important here */
                dup = b->next;
                b->next = b->next->next;
                g_list_free_1 (dup);
            } else  /* This is tricky */ {
                b = b->next;
            }
        }
        a = a->next;
    }

    /* return list without duplicates */
    return list;
}
A
A
B
C
B
A
A
B
C
示例
name.list
包含重复项:

GList *remove_dup (GList *list)
{
    GList *a, *b, *dup;
    a = list;

    /* Pick elements one by one */
    while (a != NULL && a->next != NULL) {
        b = a;

        /* Compare the picked element with rest of the elements */
        while (b->next != NULL) {
            /* If duplicate then delete it */
            if (a->data == b->next->data) {
                /* sequence of steps is important here */
                dup = b->next;
                b->next = b->next->next;
                g_list_free_1 (dup);
            } else  /* This is tricky */ {
                b = b->next;
            }
        }
        a = a->next;
    }

    /* return list without duplicates */
    return list;
}
A
A
B
C
B
A
A
B
C
使用后
remove_dup
功能:

name.list = remove_dup (name.list);
name.列表
无重复项:

GList *remove_dup (GList *list)
{
    GList *a, *b, *dup;
    a = list;

    /* Pick elements one by one */
    while (a != NULL && a->next != NULL) {
        b = a;

        /* Compare the picked element with rest of the elements */
        while (b->next != NULL) {
            /* If duplicate then delete it */
            if (a->data == b->next->data) {
                /* sequence of steps is important here */
                dup = b->next;
                b->next = b->next->next;
                g_list_free_1 (dup);
            } else  /* This is tricky */ {
                b = b->next;
            }
        }
        a = a->next;
    }

    /* return list without duplicates */
    return list;
}
A
A
B
C
B
A
A
B
C
->但似乎
remove\u dup
返回相同的
name.list
和重复项

这就是我在这段代码中的错误吗?

这是怎么回事(没有测试过,但应该可以正常工作):

虽然这个O(n2)函数对于短列表很好,但是对于长列表则效率低下。也许您希望使用不同的算法,如:

GList *remove_dup (GList *list)
{
    GList *a, *dup;
    GHashTable *set;

    set = g_hash_table_new(NULL, NULL);

    /* Pick elements one by one */
    for (a = list; a;) {
        dub = a;
        a = a->next;
        if (g_hash_table_contains(set, dub->data)) {
            list = g_list_delete_link (list, dub);
        } else {
            g_hash_table_add (set, dub->data);
        }
    }

    g_hash_table_unref (set);

    /* return list without duplicates */
    return list;
}

请尝试添加比“无效”更准确的问题描述。查找重复数据指针或删除列表节点时是否存在问题?添加了更准确的问题描述