node *del()
{node *p1,*p2;
int len=1,value=0;
printf("Please enter the numbar which will be deleted:");
scanf("%d",&value);
if(head!=NULL)
{
p1=head;
p2=p1;//记住当前节点位置
while(p1!=NULL)//&&p1->value!=value)
{
if(p1->value==value)
{
len=0;
if(p1==head)
{
head=p1->next;
}
else
{
p2->next=p1->next;//直接删除数据,p2位置不应改变
}
printf("%d is been deleted!!\n",value);
}
else
p2=p1;//没有发现需要删除的数据,p2重新记录位置,p1向下查询
p1=p1->next;//p1向下移动
}
}
else
printf("List is null!\n");
if(len)
printf("There is no %d\n",value);
return head;
}
这是一个简单的对于单链表数据操作的程序,对于删除链表中重复数据有效,但程序还有很多需要改进的地方。