双链表的基本操作

头文件

pragma once
#include <malloc.h>
#include <assert.h>
#include <stdio.h>


typedef int DataType;

typedef struct DCLNode
{
    struct DCLNode* _pNext;
    struct DCLNode* _pPre;
    DataType _data;
}Node, *PNode;

void DCLInit(PNode* ppHead);
void DCLPushBack(PNode pHead, DataType data);
void DCLPopBack(PNode pHead);
void DCLPushFront(PNode pHead, DataType data);
void DCLPopFront(PNode pHead);
void DCLInsert(PNode pos, DataType data);
void DCLErase(PNode pos);
void DCLDestroy(PNode* ppHead);
PNode DCLFind(PNode pHead, DataType data);

PNode DCLBuyNode(DataType data);
void DCLPrintList(PNode pHead);

函数实现

#include"shuang.h"

typedef struct DCLNode
{
    struct DCLNode* _pNext;//后一个结点
    struct DCLNode* _pPre;//前一个结点
    DataType _data;//当前结点的数据
}Node, *PNode;

PNode DCLBuyNode(DataType data)//创建一个新的结点
{
    PNode pnewnode = (DataType *)malloc(sizeof(DataType));
    if (pnewnode==NULL)
    {
        return;
    }
    pnewnode->_pNext = NULL;
    pnewnode->_pPre = NULL;
    pnewnode->_data = data;
}



void DCLInit(PNode* ppHead)//双链表的初始化
{
    assert(ppHead);
    *ppHead = DCLBuyNode(0);
    (*ppHead)->_pPre == NULL;
    (*ppHead)->_pNext = NULL;
}
void DCLPushBack(PNode pHead, DataType data)//尾插
{
    assert(pHead);
    PNode pcur = NULL;//最后一个结点的pnext
    PNode newnode = DCLBuyNode(data);

    pcur = pHead->_pPre;
    pcur->_pNext = newnode->_pPre;
    newnode->_pPre = pcur;
    newnode->_pNext = pHead;
    pHead->_pPre = newnode;
}

void DCLPopBack(PNode pHead)//尾删
{
    assert(pHead);
    if (pHead->_pNext == pHead)//只有一个节点
    {
        return;
    }
    PNode pdel = pHead->_pPre->_pPre;//要删除的结点
    free(pdel);//释放了之后最后一个节点就是之前的倒数第二个
    pdel->_pNext = pHead;
    pHead->_pPre = pdel;
}

void DCLPushFront(PNode pHead, DataType data)//头插
{
    assert(pHead);
    PNode newnode = DCLBuyNode(data);

    newnode->_pNext = pHead->_pPre;
    pHead->_pPre = newnode->_pNext;

    pHead->_pPre->_pNext = newnode;
    newnode->_pPre = pHead->_pPre;
}
void DCLPopFront(PNode pHead)//头删
{

    assert(pHead);
    if (pHead->_pNext == pHead)//为空
    {
        return;
    }
    else
    {
        PNode pDel = pHead->_pNext;
        pHead->_pNext = pDel->_pNext;
        pDel->_pNext->_pPre = pHead;
        free(pDel);

    }

}
void DCLInsert(PNode pos, DataType data)//在pos前插入一个节点
{
    PNode newnode = DCLBuyNode(data);
    newnode->_pNext = pos;
    pos->_pPre->_pNext = newnode;
    pos->_pPre = newnode;
    newnode->_pPre = pos->_pPre;
}
void DCLErase(PNode pos)//POS位置的删除
{
    pos->_pPre->_pNext = pos->_pNext;
    pos->_pNext->_pPre = pos->_pPre;
    free(pos);


}
void DCLDestroy(PNode* ppHead)//销毁,要有一个指针来标记pdel
{
    assert(ppHead);
    PNode pdel = NULL;
    PNode pcur = *ppHead;
    while (pcur)
    {
        pdel = pcur;
        pcur = pcur->_pNext;
        free(pdel);
    }
    free(pcur);
    *ppHead = NULL;
}
PNode DCLFind(PNode pHead, DataType data)//判断一个节点在哪个位置
{
    assert(pHead);
    PNode pcur = pHead;
    while (pcur)
    {
        if (pcur->_data == data)
        {
            return pcur;
        }
        pcur = pcur->_pNext;
    }
    return 0;

}


void DCLPrintList(PNode pHead)
{
    assert(pHead);
    PNode pcur = pHead;
    while (pcur)
    {
        printf("%d ", pcur->_data);
        pcur = pcur->_pNext;
    }
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值