데이터 구조: 선두 노드 의 양 방향 순환 링크 작업

5258 단어 #데이터 구조
세 부분 으로 나 뉘 는데 그것 이 바로 test. c, DList. c, DList. h 이다.
  • test.c
  • #define _CRT_SECURE_NO_WARNINGS 1
    #include "DList.h"
    #include 
    #include 
    void menu()
    {
    	printf("**1.               2.              **
    "); printf("**3. 4. **
    "); printf("**5. 6. **
    "); printf("**7. 8. **
    "); } void test() { int input = 0; DLDataType data = 0; DLNode *PH = NULL; DLDataType val = 0; DListInit(&PH); do { menu(); printf(" ;>"); scanf("%d", &input); switch (input) { case 1: printf(" :"); scanf("%d", &data); DListPushBack(PH, data); break; case 2: DListPopBack(PH); break; case 3: printf(" :"); scanf("%d", &data); DListPushFront(PH, data); break; case 4: DListPopFront(PH); break; case 5: printf(" :"); scanf("%d", &val); printf(" :"); scanf("%d", &data); DListInsertFront(DListFind(PH, val), data); break; case 6: printf(" :"); scanf("%d", &data); DListErase(DListFind(PH, data)); break; case 7: DListPrint(PH); break; case 8: DListDestroy(&PH); break; } } while (input); } int main() { test(); system("pause"); return 0; }
  • DList.h
  • #pragma once
    typedef int DLDataType;
    typedef struct DListNode//          
    {
    	DLDataType _data;
    	struct DListNode* _pPre;
    	struct DListNode* _pNext;
    }DLNode;
    void DListInit(DLNode** pHead);//   
    void DListPushBack(DLNode *pHead,DLDataType data);//  
    void DListPopBack(DLNode *pHead);//  
    void DListPushFront(DLNode *pHead, DLDataType data);//  
    void DListPopFront(DLNode *pHead);//  
    DLNode* DListFind(DLNode *pHead,DLDataType data);//  
    void DListInsertFront(DLNode *pos, DLDataType data);//      
    void DListErase(DLNode *pos);//      
    void DListPrint(DLNode *pHead);//  
    void DListDestroy(DLNode **pHead);//  
    
  • DList.c
  • #include "DList.h"
    #include 
    #include 
    #include 
    DLNode* BuyDlistNode(DLDataType data)
    {
    	DLNode *pNewNode = NULL;
    	pNewNode = (DLNode*)malloc(sizeof(DLNode));
    	if (pNewNode == NULL)
    	{
    		assert(0);
    		return NULL;
    	}
    	pNewNode->_data = data;
    	pNewNode->_pPre = NULL;
    	pNewNode->_pNext = NULL;
    	return pNewNode;
    }
    void DListInit(DLNode** pHead)//   
    {
    	assert(pHead);
    	*pHead = BuyDlistNode(0);
    	//          
    	(*pHead)->_pPre = *pHead;
    	(*pHead)->_pNext = *pHead;
    }
    void DListPushBack(DLNode *pHead, DLDataType data)//  
    {
    	DLNode *pNewNode = NULL;
    	assert(pHead);
    	pNewNode = BuyDlistNode(data);
    	//         
    	pNewNode->_pPre = pHead->_pPre;
    	pNewNode->_pNext = pHead;
    	//      
    	pHead->_pPre->_pNext = pNewNode;
    	pHead->_pPre = pNewNode;
    }
    void DListPopBack(DLNode *pHead)//  
    {
    	assert(pHead);
    	if (pHead == pHead->_pNext)//    
    	{
    		return;
    	}
    	else
    	{
    		DLNode *pDelNode = pHead->_pPre;
    		pHead->_pPre = pDelNode->_pPre;
    		pDelNode->_pPre->_pNext = pHead;
    		free(pDelNode);
    	}
    }
    void DListPushFront(DLNode *pHead, DLDataType data)//  
    {
    	DLNode *pNewNode = NULL;
    	assert(pHead);
    	pNewNode = BuyDlistNode(data);
    	pNewNode->_pNext = pHead->_pNext;
    	pNewNode->_pPre = pHead;
    	pHead->_pNext = pNewNode;
    	pNewNode->_pNext->_pPre = pNewNode;
    }
    void DListPopFront(DLNode *pHead)//  
    {
    	DLNode *pDelNode = NULL;
    	assert(pHead);
    	if (pHead == pHead->_pNext)
    	{
    		return;
    	}
    	pDelNode = pHead->_pNext;
    	pHead->_pNext = pDelNode->_pNext;
    	pDelNode->_pNext->_pPre = pHead;
    	free(pDelNode);
    }
    DLNode* DListFind(DLNode *pHead,DLDataType data)//  
    {
    	DLNode *pCur = pHead->_pNext;
    	while (pCur != pHead)
    	{
    		if (pCur->_data == data)
    		{
    			return pCur;
    		}
    		pCur = pCur->_pNext;
    	}
    	return NULL;
    }
    void DListInsertFront(DLNode *pos, DLDataType data)//        
    {
    	DLNode *pNewNode = BuyDlistNode(data);
    	if (pos == NULL)
    		return;
    	pNewNode->_pNext = pos;
    	pNewNode->_pPre = pos->_pPre;
    	pos->_pPre->_pNext = pNewNode;
    	pos->_pPre = pNewNode;
    }
    void DListErase(DLNode *pos)//      
    {
    	if (pos == NULL)
    		return;
    	pos->_pPre->_pNext = pos->_pNext;
    	pos->_pNext->_pPre = pos->_pPre;
    	free(pos);
    }
    void DListPrint(DLNode *pHead)//  
    {
    	DLNode *pCur = NULL;
    	assert(pHead);
    	pCur = pHead->_pNext;
    	while (pCur != pHead)
    	{
    		printf("%d ", pCur->_data);
    		pCur = pCur->_pNext;
    	}
    	printf("
    "); } void DListClear(DLNode *pHead)// { DLNode *pCur = NULL; assert(pHead); pCur = pHead->_pNext; while (pCur != pHead) { pHead->_pNext = pCur->_pNext; free(pCur); pCur = pHead->_pNext; } pHead->_pNext = pHead; pHead->_pPre = pHead; } void DListDestroy(DLNode **pHead)// , , { assert(pHead); DListClear(*pHead); free(*pHead); *pHead = NULL; }

    C + + 를 추가 하여 앞장 서 는 양 방향 순환 링크 의 조작 소스 코드 (C + 와 Linux 학습 후) (github) 를 실현 합 니 다.https://github.com/wangbiy/review/tree/master/DList

    좋은 웹페이지 즐겨찾기