20240205单链表双链表插入和删除

单链表

//头插
Linklist insert_head(datatype element,Linklist head)
{
    //创建新节点
Linklist s=create_node();
    if(NULL==s)
    return head;
s->data=element;

    //1,判断链表为空
    if(NULL==head)
    {
    head=s;
    }
    else //链表不为空
    {
    s->next=head;
    head=s;
    }

    return head;
}
//头删
Linklist delete_head(Linklist head)
{
    //1,判断链表为空
    if(NULL==head)
    {
        return head;
    }
    else //链表存在1个或多个节点
    {
        Linklist del=head;
        head=head->next;
        free(del);
        del=NULL;
    }

    return head;
}

 双链表:

//头插
Doublelink double_insert_head(datatype element,Doublelink head)
{
    //创建新节点s
    Doublelink s=create_node();
    if(s==NULL)
        return head;
    strcpy(s->data,element);

    //1.判断链表为空
    if(NULL ==head)
        head=s;
    //2.存在多个节点>=1
    else
    {
        s->next=head;
        head->priv=s;
        head=s;
    }
    return head;

}
//头删
Doublelink delete_head(Doublelink head)
{
    //1,判断链表为空
    if(NULL==head)
        return head;
    //2,存在多个节点
    Doublelink q=head;
    head=head->next;
    if(head!=NULL)
        head->priv=NULL;
    free(q);
    q=NULL;
    return head;
}

你可能感兴趣的:(java,前端,javascript)