用C语言删除链表中的重复项

用C语言删除链表中的重复项,c,linked-list,C,Linked List,我试图创建一个RemovedUpplicates函数,该函数在链表中查找重复值,调用并将找到这些重复值的索引传递到removeNode函数中(该函数将删除这些重复值)。RemovedUpplicates函数还返回删除的重复值的计数 但是我的代码中有一些错误,在我选择选项3后,程序必须强制停止,这是调用RemovedUpplicates函数的选项 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib

我试图创建一个RemovedUpplicates函数,该函数在链表中查找重复值,调用并将找到这些重复值的索引传递到removeNode函数中(该函数将删除这些重复值)。RemovedUpplicates函数还返回删除的重复值的计数

但是我的代码中有一些错误,在我选择选项3后,程序必须强制停止,这是调用RemovedUpplicates函数的选项

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

typedef struct _listnode
{
    int item;
    struct _listnode *next;
} ListNode;

typedef struct _linkedlist 
{
    int size;
    ListNode *head;
    ListNode *tail;
} LinkedList;

// FUNCTION PROTOTYPES
void createLinkedList(LinkedList *ll);
void printList(LinkedList *ll);
ListNode * findNode(LinkedList *ll, int index);
int insertNode(LinkedList *ll, int index, int value);
int insertSorted (LinkedList *ll, int value);
int removeDuplicates(LinkedList *ll);
int removeNode(LinkedList *ll, int index);

int main()
{
    int choice, value = 0;
    LinkedList l;
    l.head = 0;
    l.size = 0;

    printf("\n1: createLinkedList\n");
    printf("2: insertSorted\n");
    printf("3: removeDuplicates\n");
    printf("4: quit\n");

    do
    {
        printf("\nChoose an option: ");
        scanf("%d", &choice);
        fflush(stdin);

        switch (choice)
        {

            case 1:
                      createLinkedList(&l);
                      printList(&l);
                      break;
            case 2:
                      insertSorted(&l, value);
                      break;

            case 3:
                      printf("%d numbers were removed from the list", removeDuplicates(&l));
                      break;
        }
    } while (choice < 4);
    return 0;
}

.
.
.
(Other functions)
.
.
.

int removeDuplicates(LinkedList *ll)
{
    int index = 0;
    int count = 0;
    ListNode *pre, *cur;
    ListNode *temp = ll->head;

    if (temp == NULL)
        return;

    pre = ll->head;
    cur = pre->next;

    while (temp != NULL)
    {
        if ((pre->item == cur->item) && index < ll->size)
        {
            count++;
        }

        pre = cur->next;
        index++;

        if ((pre->item == cur->item) && index < ll->size)
        {
            count++;
        }

        cur = pre->next;
        index++;

        removeNode(&ll, index);
    } 
    printList(ll);
    return count;
}

int removeNode(LinkedList *ll, int index)
{

    ListNode *pre, *cur;

    // Highest index we can remove is size-1
    if (ll == NULL || index < 0 || index >= ll->size)
        return -1;

    // If removing first node, need to update head pointer
    if (index == 0)
    {
        cur = ll->head->next;  
        free(ll->head);        
        ll->head = cur;        
        ll->size--;            

        if (ll->size == 0)     
            ll->tail = 0;

        return 0;
    }

    // Find the nodes before and after the target position
    // Free the target node and reconnect the links
    if ((pre = findNode(ll, index - 1)) != NULL)
    {   

        // Removing the last node, update the tail pointer
        if (index == ll->size - 1)
        {
            ll->tail = pre;     
            free(pre->next);    
            pre->next = NULL;   

        else      
        {
            cur = pre->next->next;   
            free(pre->next);         
            pre->next = cur;         
        }
        ll->size--;      
        return 0;
    }

    return -1;
}
\define\u CRT\u SECURE\u NO\u警告
#包括
#包括
类型定义结构\u列表节点
{
国际项目;
结构_listnode*next;
}列表节点;
类型定义结构链接列表
{
整数大小;
ListNode*头;
ListNode*尾部;
}链接列表;
//功能原型
void createLinkedList(LinkedList*ll);
作废打印列表(LinkedList*ll);
ListNode*findNode(LinkedList*ll,int索引);
int insertNode(LinkedList*ll,int索引,int值);
int insertSorted(LinkedList*ll,int值);
int-removeDuplicates(LinkedList*ll);
int-removeNode(LinkedList*ll,int-index);
int main()
{
int选项,值=0;
链接列表l;
l、 水头=0;
l、 尺寸=0;
printf(“\n1:createLinkedList\n”);
printf(“2:insertSorted\n”);
printf(“3:removeDuplicates\n”);
printf(“4:quit\n”);
做
{
printf(“\n选择一个选项:”);
scanf(“%d”,选择(&C);
fflush(stdin);
开关(选择)
{
案例1:
创建链接列表(&l);
打印列表(&l);
打破
案例2:
insertSorted(&l,value);
打破
案例3:
printf(“从列表中删除了%d个数字”,删除了副本(&l));
打破
}
}而(选择<4);
返回0;
}
.
.
.
(其他职能)
.
.
.
int-removeDuplicates(LinkedList*ll)
{
int指数=0;
整数计数=0;
ListNode*pre,*cur;
ListNode*temp=ll->head;
if(temp==NULL)
返回;
pre=ll->head;
cur=pre->next;
while(temp!=NULL)
{
如果((前->项目==当前->项目)&&indexsize)
{
计数++;
}
pre=cur->next;
索引++;
如果((前->项目==当前->项目)&&indexsize)
{
计数++;
}
cur=pre->next;
索引++;
removeNode(&ll,索引);
} 
打印列表(ll);
返回计数;
}
int removeNode(LinkedList*ll,int索引)
{
ListNode*pre,*cur;
//我们可以删除的最高索引是size-1
如果(ll==NULL | | index<0 | | index>=ll->size)
返回-1;
//如果删除第一个节点,则需要更新头指针
如果(索引==0)
{
cur=ll->head->next;
免费(ll->head);
ll->head=cur;
ll->尺寸--;
如果(ll->size==0)
ll->tail=0;
返回0;
}
//查找目标位置前后的节点
//释放目标节点并重新连接链接
if((pre=findNode(ll,索引-1))!=NULL)
{   
//删除最后一个节点,更新尾部指针
如果(索引==ll->size-1)
{
ll->tail=pre;
免费(前->下一步);
pre->next=NULL;
其他的
{
cur=pre->next->next;
免费(前->下一步);
pre->next=cur;
}
ll->尺寸--;
返回0;
}
返回-1;
}

请参见RemovedUpplicates函数中的while循环

 while (temp != NULL)
    {
        if ((pre->item == cur->item) && index < ll->size)
        {
            count++;
        }

        pre = cur->next;
        index++;

        if ((pre->item == cur->item) && index < ll->size)
        {
            count++;
        }

        cur = pre->next;
        index++;

        removeNode(&ll, index);
    } 
while(温度!=NULL)
{
如果((前->项目==当前->项目)&&indexsize)
{
计数++;
}
pre=cur->next;
索引++;
如果((前->项目==当前->项目)&&indexsize)
{
计数++;
}
cur=pre->next;
索引++;
removeNode(&ll,索引);
} 
此循环永远不会结束,因为您没有修改
temp
的值。因此请编写代码以正确结束循环。

请考虑

将元素复制到数组中

对数组进行排序

迭代数组,找出不重复的数组,并使用它们构建新列表

删除旧列表中的元素


将旧列表的标题指向新列表。

按照以下简单步骤操作:

  • 将所有值复制到数组中
  • 对数组进行排序(
    qsort
  • 将所有唯一值写回列表(很简单,如果上一个值相同,只需跳过即可)
  • 删除所有未覆盖的节点

  • 你能提供更多的上下文吗?这个问题已经有将近四年的历史了,中间有
    在这里输入code
    ,我觉得你的答案很奇怪
        void removeDuplicates ( )
    {
        Node *current, *prev, *next, *ptp;
        ptp = current = prev = next = root;
        while ( current != NULL)
        {
            ptp = prev = next = current;
            next = next -> link;
            while ( next != NULL)
            {
                if ( next -> data == current -> data )
                {
                    prev = next;
                    next = next -> link;
                    free (prev);`enter code here`
                    prev = ptp;
                    ptp -> link = next;
                } else
                {
                    prev = next;
                    ptp = next;
                    next = next -> link;
                }
            }
            current = current -> link;
        }
    }