欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

数据结构——带头结点的双向链表

程序员文章站 2024-03-22 11:20:34
...
#pragma once
#include<assert.h>
#include<malloc.h>
typedef int DataType;
typedef struct DListNode
{
    struct DListNode* _pNext;
    struct DListNode* _pPre;
    DataType _data;
}DLNode,*PDLNode;
//带头结点的双向链表
#include<stdio.h>
void DListInit(PDLNode*pHead)//初始化链表,你想给它咋初始化就给它咋初始化。
{
    assert(pHead);
    *pHead = BuyDListNode(0);;


}
PDLNode BuyDListNode(DataType data)//买一个结点,你想给它初始化,就给它咋初始化。
{
    PDLNode pNewNode = (PDLNode)malloc(sizeof(DLNode));
    if (NULL == pNewNode)
    {
        assert(0);
        return NULL;
    }
    pNewNode->_data = data;
    pNewNode->_pNext = NULL;
    pNewNode->_pPre = NULL;
    return pNewNode;

}
void DListPushBack(PDLNode pHead, DataType data)//尾插结点
{
    PDLNode pCur = pHead;
    PDLNode pNewNode = NULL;
    assert(pHead);
    while (pCur->_pNext)//找单前链表中最后一个结点
        pCur=pCur->_pNext;
    pNewNode = BuyDListNode(data);
    pCur->_pNext = pNewNode;
    pNewNode->_pPre = pCur;
}
void DListPopBack(PDLNode pHead)//尾删
{
    PDLNode pTailNode = pHead;
    assert(pHead);
    while (pTailNode->_pNext)
        pTailNode = pTailNode->_pNext;
    if (pTailNode != pHead)
    {
        pTailNode->_pPre->_pNext = NULL;
        free(pTailNode);
    }

}
void DListPushFront(PDLNode pHead, DataType data)//头插
{
    PDLNode pNewNode = NULL;
    assert(pHead);
    pNewNode = BuyDListNode(data);
    pNewNode->_pNext = pHead->_pNext;
    pHead->_pNext = pNewNode;
    pNewNode->_pPre = pHead;
    //头插时,链表中已经有 结点
    if (pNewNode->_pNext)
        pNewNode->_pNext->_pPre = pNewNode;
}
void DListPopFront(PDLNode pHead)//头删结点
{
    PDLNode pDelNode = NULL;
    assert(pHead);
    pDelNode = pHead->_pNext;
    if (NULL == pDelNode)
        return;
    pHead->_pNext = pDelNode->_pNext;
    if (pDelNode->_pNext)
    {
        pDelNode->_pNext->_pPre = pHead;
    }
    free(pDelNode);
}
void DListErase(PDLNode pos)//删除任意位置的结点
{
    //pos在头结点的位置
    if (NULL == pos || NULL == pos->_pPre)
        return;
    pos->_pPre->_pNext = pos->_pNext;
    //pos不是最后一个结点
    if (pos->_pNext)
        pos->_pNext->_pPre = pos->_pPre;

}
PDLNode DListFind(PDLNode pHead, DataType data)//查找任意位置的元素
{
    PDLNode pCur = NULL;
    assert(pHead);
    pCur = pHead->_pNext;
    while (pCur)
    {
        if (pCur->_data == data)
            return pCur;
        pCur = pCur->_pNext;
    }
    return NULL;

}
int DListEmpty(PDLNode pHead)//链表中有效结点的个数,不包含头结点
{
    assert(pHead);
    return NULL == pHead->_pNext;
}
int DListSize(PDLNode pHead)
{
    PDLNode pCur = NULL;
    int count = 0;
    assert(pHead);
    pCur = pHead->_pNext;
    while (pCur)
    {
        count++;
        pCur = pCur->_pNext;
    }
    return count;
}
void DListClear(PDLNode pHead)//只清空有效结点,不删除头结点。
{
    PDLNode pCur = NULL;
    assert(pHead);
    pCur = pHead->_pNext;
    while (pCur)
    {
        pHead->_pNext = pCur->_pNext;
        free(pCur);
        pCur = pHead->_pNext;
    }
}
void DListDestroy(PDLNode* pHead)//销毁链表中的有效结点,销毁头结点
{

    assert(pHead);
    DListClear(*pHead);
    free(*pHead);
    *pHead = NULL;
}
void PrintDList(PDLNode pHead)
{
    PDLNode pCur = NULL;
    PDLNode pTailNode = NULL;
    assert(pHead);
    pCur = pHead->_pNext;
    //正向打印
    while (pCur)
    {
        printf("%d", pCur->_data);
        pTailNode = pCur;
        pCur = pCur->_pNext;
    }
    printf("\n");
    while (pTailNode != pHead)
    {
        printf("%d", pTailNode->_data);
        pTailNode = pTailNode->_pPre;

    }
    printf("\n");


}