试设计一算法,删除单链表h中数据域data的值为x的结点

来源:百度知道 编辑:UC知道 时间:2024/09/23 04:53:33
有更好的算法吗?

//下面这个单链表头节点为head
Node *del_node(Node *head)
{
Node *p,*q;
for(q=head,p=head->next;p!=NULL;p=p->next,q=q->next)
{
if(p->data == x)
{
q->next=p->next;
free(p);
}
}
return head;
}

pLNode deleteAllMatchVal(pLNode L, int x)

{

 if(L==NULL)  

  return NULL;

 pLNode p = L;

 pLNode q = p->next;

 while(q!=NULL)

 {

  

  if(q->data == x)

  {

   p->next = q->next;

   free(q);  

  }

  else

  {

   p = p->next;

 &nb