带头节点的双向链表

带头节点的双向链表


#include <stdio.h>
#include <assert.h>
#include <windows.h>
#pragma warning (disable:4996)

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


void DCLInit(PNode *ppHead)//初始化
{
    assert(ppHead);
    *ppHead = (PNode)malloc(sizeof(Node));
    assert(ppHead);
    (*ppHead)->_pNext = *ppHead;
    (*ppHead)->_pPre = *ppHead;
}


PNode DCLTouchNode(DataType data) //创建
{
    PNode pNewNode = (PNode)malloc(sizeof(Node));
    assert(pNewNode);
    pNewNode->_data = data;
    pNewNode->_pNext = NULL;
    pNewNode->_pPre = NULL;
    return pNewNode;
}

void DCLPrint(PNode pHead)//打印
{
    assert(pHead);
    PNode pCur = pHead->_pNext;
    while (pCur != pHead)
    {
        printf("%d --> ", pCur->_data);
        pCur = pCur->_pNext;
    }
    printf("Head\n");
}

void DCLPushFront(PNode pHead,DataType data)//头插
{
    assert(pHead);
    PNode pNewNode = DCLTouchNode(data);
    pNewNode->_pNext = pHead->_pNext;
    pHead->_pNext->_pPre = pNewNode;
    pNewNode->_pPre = pHead;
    pHead->_pNext = pNewNode;
}


void DCLPushBack(PNode pHead, DataType data)//尾插
{
    assert(pHead);
    PNode pNewNode = DCLTouchNode(data);
    PNode pTail = NULL;
    pTail = pHead->_pPre;
    pTail->_pNext = pNewNode;
    pNewNode->_pNext = pHead;
    pNewNode->_pPre = pTail;
    pHead->_pPre = pNewNode;
}

void DCLPopFront(PNode pHead) //头删
{
    assert(pHead && pHead->_pNext != pHead);
    PNode pCur = pHead->_pNext;
    pCur->_pNext-> _pPre = pCur->_pPre;
    pCur->_pPre->_pNext = pCur->_pNext;
    free(pCur);
}

void DCLPopBack(PNode pHead)//尾删
{
    assert(pHead && pHead->_pNext != pHead);
    PNode pTail = pHead->_pPre;
    pTail->_pPre->_pNext = pHead;
    pHead = pTail->_pPre;
    free(pTail);
}

void DCLInsert(PNode pHead, PNode pos, DataType data)//任意位置插
{
    assert(pHead && pos);
    PNode pNewNode = DCLTouchNode(data);
    assert(pNewNode);

    pNewNode->_pNext = pos;
    pos->_pPre->_pNext = pNewNode;
    pNewNode->_pPre = pos->_pPre;
    pos->_pPre = pNewNode;
}

void DCLErase(PNode pos)//任意位置删
{
    assert(pos);
    pos->_pPre->_pNext = pos->_pNext;
    pos->_pNext->_pPre = pos->_pPre;
    free(pos);
}

void DCLDestroy(PNode *ppHead)//销毁
{
    assert(ppHead);
    PNode pCur = (*ppHead)->_pNext;
    PNode pStr = NULL;
    while (pCur != (*ppHead))
    {
        pStr = pCur;
        pCur = pCur->_pNext;
        free(pStr);
    }
    free(pCur);
    (*ppHead)->_pNext = *ppHead;
}

PNode DCLFindPos(PNode pHead, DataType data)//找到第一个值为data的节点
{
    assert(pHead);
    PNode pCur = pHead->_pNext;
    if (pCur == pHead)
        return NULL;
    while (pCur != pHead)
    {
        if (pCur->_data == data)
            return pCur;
        pCur = pCur->_pNext;
    }
    return NULL;
}

void DCLTest() //测试
{
    PNode pHead = NULL;
    PNode pCur = NULL;
    DCLInit(&pHead);

    DCLPushFront(pHead, 1);
    DCLPushFront(pHead, 2);
    DCLPushFront(pHead, 3);//头插
    DCLPrint(pHead);       //打印---第1次
    DCLPushBack(pHead, 4);
    DCLPushBack(pHead, 5); //尾插
    DCLPrint(pHead);       //打印---2
    DCLPopFront(pHead);    //头删
    DCLPrint(pHead);       //打印---3
    DCLPopBack(pHead);     //尾删
    DCLPrint(pHead);       //打印---4
    DCLInsert(pHead, pHead->_pNext, 6);//在pHead->_pNext前面插
    DCLPrint(pHead);                   //
    DCLErase(pHead->_pNext->_pNext);//任意位置删
    DCLPrint(pHead);       //
    DCLDestroy(&pHead);    //销毁
    DCLPrint(pHead);       // 
    pCur = DCLFindPos(pHead, 1);//找数据为1的结点



}



int main()
{
    DCLTest();

    system("pause");
    return 0;
}

这里写图片描述

猜你喜欢

转载自blog.csdn.net/romantic_c/article/details/79991268