#include "DList.h"
#include "malloc.h"
#include "assert.h"
#include <stdio.h>
PDLNode BuyDList(DLDataType data)
{
assert(pHead);
*pHead = (PDLNode)malloc(sizeof(DLNode));
if (NULL == *pHead)
{
assert(0);
return;
}
*pNewNode->_pNext = NULL;//刚开始给节点并不知道位置所以给NULL
*pNewNode->_pPre = NULL;
}
}
void DListInit(PDLNode* pHead)//二级指针,为了通过形参去改变实参的值必须得用指针,
// 如果本身就是指针则用指针的指针
{
assert(pHead);
*pHead = (PDLNode)malloc(sizeof(DLNode));
if (NULL == *pHead)//此处和下面的*都是解引用,该函数涉及到二级指针,单纯的phead指的是地址
{
assert(0);
return;
}
*(pHead)->_pNext = *pHead;
*(pHead)->_pPre = *pHead;
}
void TestDlist(PDLNode* pHead)//对init测试一下
{
PDLNode pHead = NULL;
DListInit(&pHead);//要改变实参得传地址,所以前面都是二级指针
//“&”可以是取地址符,也可以是引用。此处是引用,形参是是实参的别名,
//通过引用改变这个值参数的值,相当于改变了主函数里实参的值
//没有“&”则只是实参的一个拷贝
}
void DListPushBack(DSList* s, SDataType data)//尾插--------有图片E理解
{
PDLNode pNewNode = BuyDList(data);
pNewNode->_pPre = pHead->_pPre;
pNewNode->_pNext = pHead;
pHead->_pPre->_pNext = pNewNode;
pHead->_pPre = pNewNode;
}
void DListPopBack(DSList* s)//尾删------------图片E理解
{
assert(pHead);
if ((pHead == pHead->_pNext))//没有节点
return;
PDLNode pDeLNode = pHead->_pPre;//用PDLNode标记要删除的数据即phead->_pre
pDelNode->pPre->_pNext = pHead;
pHead->_pPre = pDelNode->_pPre;
free(pDelNode)
}
void DlistPushFront(DSList* s, SDataType data)//头插-----图片F理解
{
PDLNode pNewNode = BuyDList(data);
pNewNode->_pNext = pHead->_pNext;
pNewNode->_pPre = pHead;
pHead->_pNext->_pPre = pNewNode;
pHead->_pNext = pNewNode;
}
void DlistPopFront(DSList* s)//头删
{
assert(pHead);
if ((pHead->_pNext == pHead)//没有节点
return;
PDLNode pDeLNode = pHead->_pNext;
pHead->_pNext = pDelNode->_pNext;
pDelNode->_pNext->_pPre = pHead;
free(pDelNode)
}
void DListInsert(PDLNode pos, DLDataType data)//任意位置的插入插在“当前位置的前面”
{
if (NULL == pos)
return;
PDLNode pNewNode = BuyDlist(data);
pNewNode->pNext = pos;
pNewNode->_pPre = pos->_pre;
pos->_pPre = pNewNode;
pNewNode->_pPre->_pNext = pNewNode;
}
void DListErase(PDLNode data)//任意位置的删除
{
if (NULL == pos)
return;
pos->next->_pPre = pos->_pPre;
pos->_pPre->_pNext = pos->_pNext;
free(pos);
}
void DListClear(PDLNode pHead)//清空链表
{
PDLNode pCur = pHead->_pNext;
while (pCur != pHead)
{
pHead->_pNext = pCur->_pNext;
free(pCur);
pCur = pHead->_pNext;
}
pHead->_pNext = pHead;
pHead->_pPre = pHead;
}
void DListDestroy(PDLNode* pHead)//销毁链表,改变指针的指向,所以得用二级指针
{
DListClear(*pHead);
free(*pHead);
*pHead = NULL;
}
双向循环带头结点链表的常见操作
最新推荐文章于 2023-05-31 11:03:20 发布