双链表

双链表

整体结构

图1.png

[图片上传中…(图3.png-fdbb47-1588932145605-0)]

初始化


void init(DLlist* pList)
{
    pList->head = pList->back = NULL;
    pList->size = 0;
}


DLlist* _createList()
{
    DLlist* pList = (DLlist*)malloc(sizeof(DLlist));
    pList->head = pList->back = NULL;
    pList->size = 0;
    return pList;
}

添加元素

添加为头结点

图示:

图2.png

int addToHead(DLlist* pList, _TYPE value)
{
    if (checkNull(pList))
    {
        return 0;
    }

    DLNode* newNode = (DLNode*)malloc(sizeof(DLNode));
    if (checkNull(newNode))
    {
        return 0;   // 创建新节点失败,则返回
    }
    newNode->pNext = newNode->pPre = NULL;
    newNode->value = value;

    if (pList->head==NULL)  // 若头节点为空,则新节点置为头节点
    {
        pList->head = newNode;
        pList->back = newNode;
        pList->size++;
    }
    else    // 添加到头部
    {
        newNode->pNext = pList->head;
        pList->head->pPre = newNode;
        pList->head = newNode;  // 将链表的头部设为新节点
        pList->size++;
    }
    return 1;
}

添加为尾结点

图3.png

int addToBack(DLlist* pList, _TYPE value)
{
    if (checkNull(pList))
    {
        return 0;
    }
    DLNode* newNode = (DLNode*)malloc(sizeof(DLNode));
    if (checkNull(newNode))
    {
        return 0;   // 创建新节点失败,则返回
    }
    newNode->pNext = newNode->pPre = NULL;
    newNode->value = value;
    if (NULL == pList->head)  // 若头节点为空,则新节点置为头节点
    {
        pList->head = newNode;
        pList->back = newNode;
        pList->head->pNext = pList->back->pNext = NULL;
        pList->size++;
    }
    else if (pList->size == 1)  // 只有一个元素,头尾都指向它,链接好头部与下一个节点
    {
        newNode->pPre = pList->head;
        pList->head->pNext = newNode;
        pList->back = newNode;
        pList->size++;
    }
    else  // 添加到尾部
    {
        newNode->pPre = pList->back;
        pList->back->pNext = newNode;
        pList->back = newNode;
        pList->size++;
    }

    return 1;
}

数组元素添加进入链表
  • 1 添加到尾部的形式
int addToBackWithArrayValues(DLlist* pList, _TYPE array[], int length)
{
    if (checkNull(pList) || checkNull(array) || length == 0)
    {
        return 0;
    }

    for (int i = 0; i < length; i++)
    {
        if (!addToBack(pList, array[i]))
        {
            return 0;
        }
    }

    return 1;
}


指定结点后面插入元素

图4.png


int insertAfter(DLlist* pList, _TYPE curValue, _TYPE newValue)
{
    if (checkNull(pList))
    {
        return 0;
    }
    if (isEmpty(pList))
    {
        printf("\nlog:插入指定节点后面失败,空链表\n");
        return 0;
    }

    DLNode* newNode = (DLNode*)malloc(sizeof(DLNode));
    if (checkNull(newNode))
    {
        return 0;   // 创建新节点失败,则返回
    }
    newNode->pNext = newNode->pPre = NULL;
    newNode->value = newValue;

    if (pList->head->value==curValue)  // 头节点满足
    {
        DLNode* next = pList->head->pNext;
        next->pPre = newNode;
        newNode->pNext = next;
        newNode->pPre = pList->head;
        pList->head->pNext = newNode;
        pList->size++;
        return 1;
    }
    else if (pList->back->value == curValue)  // 尾节点满足,防止要遍历一遍,浪费时间
    {
        pList->back->pNext = newNode;
        newNode->pPre = pList->back->pNext;
        pList->back;
        pList->size++;
        return 1;
    }
    else  // 其他
    {
        DLNode* p = pList->head->pNext;
        while (p)
        {
            if (p->value==curValue)
            {
                DLNode* next = p->pNext;
                next->pPre = newNode;
                newNode->pNext = next;
                p->pNext = newNode;
                newNode->pPre = p;
                pList->size++;
                return 1;
            }
            p = p->pNext;
        }
    }

    return 0;
}

删除元素

删除第一个指定元素的节点

图示:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6gD0W3nh-1588932165374)(https://upload-images.jianshu.io/upload_images/5653258-66262a6bc2ea8b82.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)]

//删除第一个指定元素的节点,返回1则删除成功
int deleteNode(DLlist* pList, _TYPE value)
{

    if (checkNull(pList))
    {
        printf("\nlog:删除节点失败\n");
        return 0;
    }
    if (isEmpty(pList))
    {
        printf("\nlog:删除节点失败,空链表\n");
        return 0;
    }
    DLNode* findNode = findByValue(pList, value);
    if (checkNull(findNode))
    {
        printf("\nlog:要删除的节点不存在,value=%d\n", value);
        return 0;
    }
    if (value == pList->head->value)  // 头节点满足
    {
        if (pList->head->pNext==NULL)  // 只有head时
        {
            DLNode* curNode = pList->head;
            pList->head = pList->back = NULL;  // 删除所有节点后将head与back都赋值NULL
            free(curNode);
        }
        else
        {
            DLNode* curNode = pList->head;
            pList->head = curNode->pNext;   // 头节点后移
            free(curNode);

        }
        pList->size--;
        return 1;
    }
    else if (value == pList->back->value)   // 尾节点满足
    {
        DLNode* curNode = pList->back;
        pList->back = curNode->pPre;
        pList->back->pNext = NULL;
        free(curNode);
        pList->size--;
        return 1;
    }
    else
    {
        DLNode* curNode = pList->head->pNext;
        while (curNode!=NULL)       // 下面可以调用findByValue来查找指定节点
        {
            if (curNode->value==value)  // 删除满足条件的节点
            {
                DLNode* preNode = curNode->pPre;
                DLNode* nextNode = curNode->pNext;
                preNode->pNext = nextNode;
                nextNode->pPre = preNode;
                free(curNode);  // 释放内存
                pList->size--;
                return 1;
            }
            curNode = curNode->pNext;
        }
    }
    return 0;
}

修改指定结点值

int modify(DLlist* pList, _TYPE oldValue, _TYPE newValue)
{
    if (checkNull(pList) )
    {
        return 0;
    }

    if (isEmpty(pList))
    {
        printf("\nlog:空链表\n");
        return 0;
    }
    DLNode* curNode = pList->head;
    while (curNode)
    {
        if (curNode->value==oldValue)
        {
            curNode->value = newValue;
            return 1;
        }
        curNode = curNode->pNext;
    }

    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值