C 程序 - 从 LinkedList 中删除重复项

标签 c linked-list

我正在尝试从链接列表中删除重复项。用户插入一个值,然后程序将检查用户的输入和链表中的值是否相同。如果相似,它将删除并在链表中只留下一个。例如链接列表=10 100。用户=10。结果=10 100 而不是 10 10 100。

int insertSorted(LinkedList *ll, int item)
{
    ListNode *cur = ll->head;
    int size = ll->size;
    int i;

    for (i = 0; i <= size; i++)
    {
        if ((size - i) == 0 || item < cur->item)
        {
            insertNode(ll, i, item); // function to insert the value into Linkedlist
            return i;
        }

        cur = cur->next;
    }

    ListNode *current = ll->head;
    while (current->next != NULL)
    {
        if (current->item == current->next->item)
        {
            ListNode *nextNext = current->next->next;
            free(current->next);
            current->next = nextNext;
        }

        else
        {
            current = current->next; // only advance if no deletion
        }
    }

    return -1;
}

最佳答案

当您插入新节点(示例中的值 10)时,您将 return i; ,制作其余的代码,我认为它会检查重复项而不执行。

关于C 程序 - 从 LinkedList 中删除重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46865787/

相关文章:

c++ - C++ 的网络框架(UDP 或 TCP)?

c - 错误 : expected expression before ‘int’

java - Hashmap 及其背后的工作原理

C 链表中的冒泡排序

c - C中的插入排序链表

c - 如何理解链表结构中的指针 'next'?

c - 在节点中传递 ')' token 之前预期为 '*'

c - 运算符 `<--` 在 C 中做什么?

c - 使用 shmctl() 销毁共享内存段 (Linux)

c - fgetc 无法加载文件的最后一个字符