删除单链表中某个元素

    xiaoxiao2021-03-25  147

    解决方案: 1.创建链表 2.遍历链表,发现有可能为

    空链表一个节点两个节点多个节点删除重复元素的时候有可能从第一个便重复 代码如下: void Remove(pList* pplist,DataType d)//删除 { pNode cur = *pplist; pNode Del = NULL; if(cur == NULL) //处理只有一个节点的链表 { printf("链表为空\n"); return; } if(cur->next == NULL) //处理只有两个节点的链表 { if(cur->data == d) { free(cur); *pplist = NULL; } } else { while((cur->data == d) && (cur != NULL))//起始相同 { Del = cur; *pplist = cur->next; cur = *pplist; free(Del); Del = NULL; } if(cur != NULL) //处理正常重复的data { while(cur->next != NULL) { if(cur->next->data == d) { Del = cur->next; cur->next = cur->next->next; free(Del); Del = NULL; } else cur = cur->next; } } } }
    转载请注明原文地址: https://ju.6miu.com/read-7528.html

    最新回复(0)