这些删除列表项算法之间的区别

问题描述

我编写了三种算法,用于从单链接列表中删除项目/项目。 1号工作正常。同样2号也可以。 3号不起作用,我知道。但是为什么数字3不正确?数字2和双指针有什么区别?

数字1:

Set cdomsg = CreateObject("cdo.message")
With cdomsg.Configuration.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = "465"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "xxxxx@gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "xxxxxx"
.Update
End With
'==================================================================
' The procedure is to find the value in database with correspond information
'==================================================================

'(( i need the procedure to call the values ))

'===================================================================
'===================================================================
With cdomsg

strBody = strBody & "<span><br/>This Email was sent from Application </span>"

strBody = strBody & "<span>Hello : " & Name & " your pin code is: " & pincode & "</span><br/>"
   

'====================================================================
.To = user_email
.From = "xxxxx@gmail.com"
.Subject = "Pin code application"
.HTMLBody = strBody
.Send
End With
Set cdomsg = nothing

End Function```

数字2:

struct nodo * delete1(struct nodo * top,int data) {

struct nodo *current,*temp;

if(top->valore == data ) {
     current = top->next;
     free(top);
     top = current;
 }
 
 
     current = top;
     while (current->next)
     {
         if (current->next->valore == data)
         {
             temp = current->next->next;
             free(current->next);
             current->next = temp;
         }
         else
             current = current->next;
     }
 

return top;
}

数字3:

void delete2(struct nodo **p,int k) {

while(*p) {
    if ((*p)->valore == k)
    {
        struct nodo * tmp = (*p)->next;
        free(*p);
        (*p) = tmp;
    }
    else
    {
        p = &(*p)->next;
    }
    
}

}

解决方法

对于初学者来说,这三个功能具有不同的行为。

在第一个函数中,如果列表的第一个节点的值等于指定的值,则仅删除该第一个节点。否则,将删除所有具有指定值的节点。

但是在任何情况下,即使在更新函数之后,它也可以调用未定义的行为,例如,当调用空列表或删除列表的单个节点时。

在第二个功能中,将删除具有等于指定值的所有节点(取决于第一个节点是否具有等于指定值的值)。

第三个函数是完全错误的,因为它更改了局部变量curr而不是例如节点的变量top或下一个数据成员。在任何情况下,函数都将返回指针top的先前值,而与指向的节点是否已删除无关。

要使第一个功能的行为类似于第二个功能的行为,应按以下方式进行定义。

struct nodo * delete1(struct nodo * top,int data) 
{
    while ( top && top->valore == data ) 
    {
        struct nodo *temp = top->next;
        free( top );
        top = temp;
    }

    if ( top )
    {
        struct nodo *current = top;
        while (current->next)
        {
            if ( current->next->valore == data )
            {
                struct nodo *temp = current->next->next;
                free( current->next );
                current->next = temp;
            }
            else
            {
                current = current->next;
            }
        }
    }

    return top;
}