선두 결점 의 양 방향 링크 [데이터 구조]

선두 결점 의 양 방향 순환 링크:
구성:
typedef struct DCList{
	struct DCList* _pNext;
	struct DCList* _pPre;
	DataType _data;
}Node, *pNode;

새 노드 만 들 기:
pNode DCLBuyNode(DataType data)
{
	pNode NewNode = (pNode)malloc(sizeof(Node));
	if (NULL == NewNode)
	{
		return NULL;
	}
	NewNode->_data = data;
	NewNode->_pNext = NULL;
	NewNode->_pPre = NULL;
	return NewNode;
}

초기 화:
void InitDCList(pNode* ppHead)
{
	assert(ppHead);
	*ppHead = DCLBuyNode(0);	//          
	if (NULL == *ppHead)
	{
		return;
	}
	(*ppHead)->_pNext = *ppHead;
	(*ppHead)->_pPre = *ppHead;
}

끝 삽입:
void DCLPushBack(pNode pHead, DataType data)
{
	pNode pNewNode = NULL;
	pNode pTailNode = NULL;
	assert(pHead);
	pNewNode  = DCLBuyNode(data);
	if (NULL == pNewNode)
	{
		return;
	}
	pTailNode = pHead->_pPre;			//        
	pTailNode->_pNext = pNewNode;			//            
	pNewNode->_pPre = pTailNode;			//     
	pNewNode->_pNext = pHead;
	pHead->_pPre = pNewNode;			//         
}

끝 삭제:
void DCLPopBack(pNode pHead)
{
	if (NULL == pHead)
	{
		return;
	}
	pNode pPreTail = pHead->_pPre->_pPre;
	free(pPreTail->_pNext);
	pPreTail->_pNext = pHead;
	pHead->_pPre = pPreTail;
}

플러그 인:
void DCLPushFront(pNode pHead, DataType data)
{
	pNode pNewNode = NULL;
	assert(pHead);
	pNewNode = DCLBuyNode(data);
	if (NULL == pNewNode)
	{
		return;
	}
	pNewNode->_pNext = pHead->_pNext;	//               
	pHead->_pNext->_pPre = pNewNode;	
	pHead->_pNext = pNewNode;		
	pNewNode->_pPre = pHead;		
}

삭제:
void DCLPopFront(pNode pHead)
{
	pNode pDel = NULL;
	if (pHead == pHead->_pNext)		//     
	{
		return;
	}
	pDel = pHead->_pNext;
	pDel->_pNext->_pPre = pHead;
	pHead->_pNext = pDel->_pNext;
	free(pDel);
}

임의의 삽입:
void DCLInsert(pNode pos, DataType data)	//  pos 
{
	assert(pos);
	pNode pNewNode = DCLBuyNode(data);
	if (NULL == pNewNode)
	{
		return;
	}
	//      	
	pNewNode->_pNext = pos;						//         pos
	pos->_pPre->_pNext = pNewNode;				//pos              
	pNewNode->_pPre = pos->_pPre;				//         pos    
	pos->_pPre = pNewNode;						//pos      pos
}

임의로 삭제:
void DCLErase(pNode pos)
{
	assert(pos);
	pos->_pPre->_pNext = pos->_pNext;
	pos->_pNext->_pPre = pos->_pPre;
	free(pos);
}

찾기:
pNode DCLFind(pNode pHead, DataType data)
{
	pNode pCur = NULL;
	assert(pHead);
	pCur = pHead->_pNext;
	while (pHead != pCur)
	{
		if (data == pCur->_data)
		{
			return pCur;
		}
		pCur = pCur->_pNext;
	}
	return NULL;
}

소각:
void DestroyDCList(pNode *ppHead)
{
	pNode pDel = NULL;		
	pNode pPreDel = NULL;
	assert(ppHead);
	pDel = (*ppHead)->_pNext;
	while (*ppHead != pDel)
	{
		pPreDel = pDel;
		pDel = pDel->_pNext;
		free(pPreDel);
	}
	free(pDel);				//     
	(*ppHead) = NULL;
}

좋은 웹페이지 즐겨찾기