小编Bou*_*ezg的帖子

从c中的链表中删除重复项?

我试图从链表中删除重复项,所以如果列表以[1,1,2,3,4,4,5,5]开头,则附加列表为[1,2,3,4, 5].代码如下.

struct node_h
{
    int data;
    struct node_h* next;
} node;

void remove_h(node* head)
{
    while (head != NULL)
    {
        if (head->data == head->next->data)
        {
            if (head->next->next == NULL)
            {
                head->next = NULL;
            }
            else
            {
                head->next = head->next->next;
            }
        }        
        head = head->next;
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是它的分段错误.有时.

c linked-list

0
推荐指数
1
解决办法
75
查看次数

标签 统计

c ×1

linked-list ×1