Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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
从链表中删除重复项并将其保存到C语言中的另一个列表中_C_List_Copy_Duplicates - Fatal编程技术网

从链表中删除重复项并将其保存到C语言中的另一个列表中

从链表中删除重复项并将其保存到C语言中的另一个列表中,c,list,copy,duplicates,C,List,Copy,Duplicates,在执行此任务期间,我遇到了一个问题。我只是在获取列表,检查当前处理的元素是否在其中。如果是跳过它,否则添加到新列表。 为此,我编写了三个函数。一个是搜索特定元素,下一个是检查新列表中是否存在元素,最后一个是将所有元素集合在一起 我试着用两种方法来做到这一点: 首先检查“旧”列表中的出现情况,它可以工作,但根本不起作用(以降序写入新列表),而且我认为在特定的示例中,它不会工作 第二次检查元素在当前创建的列表中的出现情况,但这不起作用 代码如下: struct list *search_node

在执行此任务期间,我遇到了一个问题。我只是在获取列表,检查当前处理的元素是否在其中。如果是跳过它,否则添加到新列表。 为此,我编写了三个函数。一个是搜索特定元素,下一个是检查新列表中是否存在元素,最后一个是将所有元素集合在一起

我试着用两种方法来做到这一点:

  • 首先检查“旧”列表中的出现情况,它可以工作,但根本不起作用(以降序写入新列表),而且我认为在特定的示例中,它不会工作
  • 第二次检查元素在当前创建的列表中的出现情况,但这不起作用
代码如下:

struct list *search_node(struct list *prt, char *to_search) {
    is_empty();
    short is_found = -1;
    while (prt != NULL && ((is_found = strcmp(prt->word, to_search) != 0))) {
        prt = prt->next;
    }
    if (!is_found)
        return prt;
    else
        return NULL;
}

bool is_on_list(struct list *ptr, char *str) {
    if (search_node(ptr, str) != NULL)
        return 1;
    else
        return 0;
}

struct list *without_repetiton(struct list *head) {
    struct list *new_list = NULL;
    struct list **new_el = &new_list;
    struct list *prt1 = head, *check;

    while (prt1 != NULL) {
        //printf("test = %s\n", prt1 -> word);
        check = new_list;
        if (!is_on_list(prt1->next, prt1->word)) { // or (new_list, prt1 -> word) 
            *new_el = (struct list *)malloc(sizeof(struct list));
            memcpy(*new_el, prt1, sizeof(struct list));
            new_el = &((*new_el)->next);
        }
        prt1 = prt1->next;
    }
    return new_list;
}
列表的结构如下:

struct list {
    char *word;
    struct list *next;
    struct list *prev;
};
我有两个问题,第一个问题是为什么第一种方法按降序编写列表,第二个问题是为什么我试图在not work中搜索已创建列表中出现的单词

IO样本:

当:
在列表中(prt1->next,prt1->word))

第一个列表:一,一,二
第二个列表:两个,一个
当:
在列表中(新列表,prt1->word))


第一个列表与第二个列表相同。

您的代码没有正确构造重复列表:简化代码并手动链接新元素,而不是使用
memcpy()


请注意,使用
memcpy
功能复制列表中的节点时,不会进行深度复制。也就是说,您只复制指针
word
,而不复制它所指向的数据。这意味着之后将有两个指针指向同一内存。如果稍后尝试修改一个列表的字符串,则这可能会导致问题,因为这两个列表都将有更新。或者当您(可能)尝试释放内存时,可能会释放两次。此外,还有一种更简单的方法可以对这样的结构进行浅层复制:
**new_el=*prt1
((is_found=strcmp(prt->word,to_search)!=0))
是可疑的。问题是什么?你能提供输入和输出样本吗?
struct list *search_node(struct list *ptr, const char *to_search) {
    while (ptr != NULL) {
        if (!strcmp(ptr->word, to_search))
            return ptr;
    }
    return NULL;
}

bool is_on_list(struct list *ptr, const char *str) {
    return search_node(ptr, str) != NULL;
}

struct list *without_repetiton(struct list *head) {
    struct list *new_list = NULL;
    struct list *tail = NULL;

    for (struct list *ptr = head; ptr != NULL; ptr = ptr->next) {
        if (!is_on_list(new_list, ptr->word)) {
            struct list *node = malloc(sizeof(*node));
            node->next = NULL;
            node->prev = tail;
            node->str = strdup(ptr->word);
            if (tail == NULL) {
                 new_list = tail = node;
            } else {
                 tail->next = node;
                 tail = node;
            }
        }
    }
    return new_list;       
}