双向循环链表的相关操作

给出双向循环链表的定义:

typedef int DataType;

typedef struct DCLNode
{
	struct DCLNode* _pNext;
	struct DCLNode* _pPre;
	DataType _data;
}Node, *PNode;
将函数的声明放在head.h的头文件里面:

#include "DCList.h"

PNode DCLBuyNode(DataType data)
{
	PNode pNewNode = (PNode)malloc(sizeof(Node));
	if (NULL == pNewNode)
	{
		assert(0);
	}

	pNewNode->_data = data;
	pNewNode->_pNext = NULL;
	pNewNode->_pPre = NULL;

	return pNewNode;
}

void DCLInit(PNode* ppHead)
{
	assert(ppHead);
	*ppHead = DCLBuyNode(0);
	if (NULL == *ppHead)
	{
		assert(0);
	}
	(*ppHead)->_pNext = *ppHead;
	(*ppHead)->_pPre = *ppHead;
}


void DCLPushBack(PNode pHead, DataType data)
{
	PNode pNewNode = DCLBuyNode(data);
	PNode pTailNode = NULL;
	assert(pHead);

	pTailNode = pHead->_pPre; //找到最后一个结点

	pTailNode->_pNext = pNewNode;
	pNewNode->_pPre = pTailNode;
	pNewNode->_pNext = pHead;
	pHead->_pPre = pNewNode;
}

void DCLPopBack(PNode pHead)
{
	if (pHead->_pNext == pHead)
	{
		return;
	}
	else
	{
		PNode pPreTail = pHead->_pPre->_pPre;
		free(pPreTail->_pNext);
		pPreTail->_pNext = pHead;
		pHead->_pPre = pPreTail;
	}
}

void DCLPushFront(PNode pHead, DataType data)
{
	PNode pNewNode = DCLBuyNode(data);
	assert(pHead);

	pNewNode->_pNext = pHead->_pNext;
	pHead->_pNext->_pPre = pNewNode;
	pNewNode->_pPre = pHead;
	pHead->_pNext = pNewNode;
}
void DCLPopFront(PNode pHead)
{
	if (pHead->_pNext == pHead)
	{
		return;
	}
	else
	{
		PNode pDel = pHead->_pNext;
		pHead->_pNext = pDel->_pNext;
		pDel->_pNext->_pPre = pHead;
		free(pDel);
	}
}

void DCLPrintList(PNode pHead)
{
	PNode pCur = NULL;
	assert(pHead);
	pCur = pHead->_pNext;

	while (pCur != pHead)
	{
		printf("%d--->", pCur->_data);
		pCur = pCur->_pNext;
	}
	printf("\n");
}

void DCLDestroy(PNode* ppHead)
{
	PNode pCur = NULL;
	assert(ppHead);
	pCur = (*ppHead)->_pNext;

	while (pCur != *ppHead)
	{
		pCur = pCur->_pNext;
		free(pCur->_pPre);
	}
	free(pCur->_pPre);
	free(pCur);
	*ppHead = NULL;
}

void DCLDestroy_P(PNode* ppHead)
{
	PNode pCur = NULL;
	PNode pPre = NULL;
	assert(ppHead);
	pCur = (*ppHead)->_pNext;

	while (pCur != *ppHead)
	{
		pPre = pCur;
		pCur = pCur->_pNext;
		free(pPre);
	}
	free(pCur);
	*ppHead = NULL;
}

void DCLInsert(PNode pos, DataType data)
{
	PNode pNewNode = DCLBuyNode(data);
	assert(pNewNode);

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

void DCLErase(PNode pos)
{
	pos->_pPre->_pNext = pos->_pNext;
	pos->_pNext->_pPre = pos->_pPre;
	free(pos);
}

PNode DCLFind(PNode pHead, DataType data)
{
	PNode pCur = pHead->_pNext;
	while (pCur != pHead)
	{
		if (pCur->_data == data)
		{
			return pCur;
		}
		pCur = pCur->_pNext;
	}
	return NULL;
}


函数的定义(具体实现):

#include "DCList.h"

PNode DCLBuyNode(DataType data)
{
	PNode pNewNode = (PNode)malloc(sizeof(Node));
	if (NULL == pNewNode)
	{
		assert(0);
	}

	pNewNode->_data = data;
	pNewNode->_pNext = NULL;
	pNewNode->_pPre = NULL;

	return pNewNode;
}

void DCLInit(PNode* ppHead)
{
	assert(ppHead);
	*ppHead = DCLBuyNode(0);
	if (NULL == *ppHead)
	{
		assert(0);
	}
	(*ppHead)->_pNext = *ppHead;
	(*ppHead)->_pPre = *ppHead;
}


void DCLPushBack(PNode pHead, DataType data)
{
	PNode pNewNode = DCLBuyNode(data);
	PNode pTailNode = NULL;
	assert(pHead);

	pTailNode = pHead->_pPre; //找到最后一个结点

	pTailNode->_pNext = pNewNode;
	pNewNode->_pPre = pTailNode;
	pNewNode->_pNext = pHead;
	pHead->_pPre = pNewNode;
}

void DCLPopBack(PNode pHead)
{
	if (pHead->_pNext == pHead)
	{
		return;
	}
	else
	{
		PNode pPreTail = pHead->_pPre->_pPre;
		free(pPreTail->_pNext);
		pPreTail->_pNext = pHead;
		pHead->_pPre = pPreTail;
	}
}

void DCLPushFront(PNode pHead, DataType data)
{
	PNode pNewNode = DCLBuyNode(data);
	assert(pHead);

	pNewNode->_pNext = pHead->_pNext;
	pHead->_pNext->_pPre = pNewNode;
	pNewNode->_pPre = pHead;
	pHead->_pNext = pNewNode;
}
void DCLPopFront(PNode pHead)
{
	if (pHead->_pNext == pHead)
	{
		return;
	}
	else
	{
		PNode pDel = pHead->_pNext;
		pHead->_pNext = pDel->_pNext;
		pDel->_pNext->_pPre = pHead;
		free(pDel);
	}
}

void DCLPrintList(PNode pHead)
{
	PNode pCur = NULL;
	assert(pHead);
	pCur = pHead->_pNext;

	while (pCur != pHead)
	{
		printf("%d--->", pCur->_data);
		pCur = pCur->_pNext;
	}
	printf("\n");
}

void DCLDestroy(PNode* ppHead)
{
	PNode pCur = NULL;
	assert(ppHead);
	pCur = (*ppHead)->_pNext;

	while (pCur != *ppHead)
	{
		pCur = pCur->_pNext;
		free(pCur->_pPre);
	}
	free(pCur->_pPre);
	free(pCur);
	*ppHead = NULL;
}

void DCLDestroy_P(PNode* ppHead)
{
	PNode pCur = NULL;
	PNode pPre = NULL;
	assert(ppHead);
	pCur = (*ppHead)->_pNext;

	while (pCur != *ppHead)
	{
		pPre = pCur;
		pCur = pCur->_pNext;
		free(pPre);
	}
	free(pCur);
	*ppHead = NULL;
}

void DCLInsert(PNode pos, DataType data)
{
	PNode pNewNode = DCLBuyNode(data);
	assert(pNewNode);

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

void DCLErase(PNode pos)
{
	pos->_pPre->_pNext = pos->_pNext;
	pos->_pNext->_pPre = pos->_pPre;
	free(pos);
}

PNode DCLFind(PNode pHead, DataType data)
{
	PNode pCur = pHead->_pNext;
	while (pCur != pHead)
	{
		if (pCur->_data == data)
		{
			return pCur;
		}
		pCur = pCur->_pNext;
	}
	return NULL;
}

测试代码:

#include "DCList.h"

void TestDCList()
{
	PNode pHead, pos;
	DCLInit(&pHead);
	DCLPushBack(pHead, 1);
	DCLPushBack(pHead, 2);
	DCLPushBack(pHead, 3);
	DCLPushBack(pHead, 4);
	DCLPrintList(pHead);

	DCLPopBack(pHead);
	DCLPrintList(pHead);

	DCLPushFront(pHead, 0);
	DCLPrintList(pHead);

	DCLPopFront(pHead);
	DCLPrintList(pHead);

	pos = DCLFind(pHead, 2);
	DCLInsert(pos, 5);
	DCLPrintList(pHead);

	pos = DCLFind(pHead, 5);
	DCLErase(pos);
	DCLPrintList(pHead);

	DCLDestroy(&pHead);
}

int main()
{
	TestDCList();
	system("pause");
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值