數據結構——帶頭結點的雙向鏈表

#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");


}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章