解决方案:
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;
}
}
}
}