20160225.CCPP體系詳解(0035天)

尹成發表於2016-03-25

程式片段(01):CircleList.h+CircleList.c+main.c
內容概要:環形連結串列

///CircleList.h
#pragma once
#include <stdio.h>

typedef struct node
{
    int data;
    struct node * pNext;
}Node;

void circleListTailInsertData(Node ** ppCircleList, int data);
void circleListHeadInsertData(Node ** ppCircleList, int data);

void showCircleList(Node * pCircleList);

Node * circleListSelectFirstData(Node * pCircleList, int data);

void circleListRandInsertData(Node ** ppCircleList, int data, int insertData);

void circleListDeleteFirstData(Node ** ppCircleList, int data);

int countCircleList(Node * pCircleList);

Node * circleListGetEndNode(Node ** ppCircleList, int num);
///CircleList.c
#include "CircleList.h"
#include <stdlib.h>

void circleListHeadInsertData(Node ** ppCircleList, int data)
{
    if (NULL == ppCircleList)
        abort();
    Node * pNew = (Node *)malloc(sizeof(Node));
    pNew->data = data;
    pNew->pNext = *ppCircleList;
    *ppCircleList = pNew;
}

void circleListTailInsertData(Node ** ppCircleList, int data)
{
    if (NULL == ppCircleList)
        abort();
    Node *pNew = (Node *)malloc(sizeof(Node));
    pNew->data = data;
    pNew->pNext = NULL;
    if (NULL == *ppCircleList)
    {
        *ppCircleList = pNew;
        pNew->pNext = *ppCircleList;
        return;
    }
    Node * pTemp = *ppCircleList;
    while (*ppCircleList != pTemp->pNext)
    {
        pTemp = pTemp->pNext;
    }
    pTemp->pNext = pNew;
    pNew->pNext = *ppCircleList;
}

void showCircleList(Node * pCircleList)
{
    //方式一:空節點環+單節點環+多節點環
    if (NULL == pCircleList)
        abort();
    //方式二:
    Node * pTmp = pCircleList;
    do
    {
        printf("%3d", pTmp->data);
        pTmp = pTmp->pNext;
    } while (pCircleList != pTmp);
    printf("\n");
}

void circleListRandInsertData(Node ** ppCircleList, int data, int insertData)
{
    if (NULL == ppCircleList || NULL == *ppCircleList)
        return;
    int find = 0;
    Node * pTmp1 = NULL;
    Node * pTmp2 = *ppCircleList;
    do
    {
        if (data == pTmp2->data)
        {
            find = 1;
            break;
        }
        pTmp1 = pTmp2;
        pTmp2 = pTmp2->pNext;
    } while (*ppCircleList != pTmp2);
    if (!find)
        return;
    Node * pNew = (Node *)malloc(sizeof(Node));
    pNew->data = insertData;
    //前插邏輯:
    //if (*ppCircleList == pTmp2)
    //{
    //  pNew->pNext = *ppCircleList;
    //  Node * pTmp = *ppCircleList;
    //  while (*ppCircleList != pTmp->pNext)
    //  {
    //      pTmp = pTmp->pNext;
    //  }
    //  *ppCircleList = pNew;
    //  pTmp->pNext = *ppCircleList;
    //  return;
    //}
    //pTmp1->pNext = pNew;
    //pNew->pNext = pTmp2;
    //後插邏輯:
    pNew->pNext = pTmp2->pNext;
    pTmp2->pNext = pNew;
}

Node * circleListSelectFirstData(Node * pCircleList, int data)
{
    if (NULL == pCircleList)
        abort();
    Node * pTmp = pCircleList;
    do
    {
        if (data == pTmp->data)
        {
            return pTmp;
        }
        pTmp = pTmp->pNext;
    } while (pCircleList != pTmp);
    return NULL;
}

void circleListDeleteFirstData(Node** ppCircleList, int data)
{
    if (NULL == ppCircleList || NULL == *ppCircleList)
        return;
    int find = 0;
    Node * pTmp1 = NULL;
    Node * pTmp2 = *ppCircleList;
    do
    {
        if (data == pTmp2->data)
        {
            find = 1;
            break;
        }
        pTmp1 = pTmp2;
        pTmp2 = pTmp2->pNext;
    } while (*ppCircleList != pTmp2);
    if (!find)
        return;
    if (*ppCircleList == pTmp2 && NULL == pTmp2->pNext)
    {
        *ppCircleList = NULL;
        free(pTmp2);
        return;
    }
    if (*ppCircleList == pTmp2 && NULL != pTmp2->pNext)
    {
        Node * pTmp = *ppCircleList;
        while (*ppCircleList != pTmp->pNext)
        {
            pTmp = pTmp->pNext;
        }
        *ppCircleList = (*ppCircleList)->pNext;
        pTmp->pNext = *ppCircleList;
        free(pTmp2);
        return;
    }
    pTmp1->pNext = pTmp2->pNext;
    free(pTmp2);
}

int countCircleList(Node * pCircleList)
{
    if (NULL == pCircleList)
        abort();
    if (pCircleList == pCircleList->pNext)
    {
        return 1;
    }
    int nodeNum = 0;
    Node * pTmp = pCircleList;
    do
    {
        ++nodeNum;
        pTmp = pTmp->pNext;
    } while (pCircleList != pTmp);
    return nodeNum;
}

Node * circleListGetEndNode(Node ** ppCircleList, int num)
{
    Node * pTmp = *ppCircleList;
    Node * pFree = NULL;
    while (1 != countCircleList(*ppCircleList))
    {
        for (int i = 0; i < num; ++i)
        {
            pFree = pTmp = pTmp->pNext;
        }
        pTmp = pTmp->pNext;
        circleListDeleteFirstData(ppCircleList, pFree->data);
    }
    return *ppCircleList;
}
///main.c
#include "CircleList.h"
#include <stdlib.h>
#include <time.h>

int main01(void)
{
    Node * pCircleList = NULL;
    //srand((unsigned int)time(NULL));
    //for (int i = 0; i < 10; ++i)
    //{
    //  circleListTailInsertData(&pCircleList, rand() % 99 + 1);
    //}
    circleListTailInsertData(&pCircleList, 1);
    circleListTailInsertData(&pCircleList, 2);
    circleListTailInsertData(&pCircleList, 3);
    circleListTailInsertData(&pCircleList, 6);
    circleListTailInsertData(&pCircleList, 5);
    circleListTailInsertData(&pCircleList, 4);
    //circleListDeleteFirstData(&pCircleList, 4);
    //printf("nodeNum = %d \n", countCircleList(pCircleList));
    //circleListRandInsertData(&pCircleList, 3, 7);
    showCircleList(pCircleList);
    //printf("%3d \n", circleListSelectFirstData(pCircleList, 1)->data);

    system("pause");
}

int main02(void)
{
    Node * pCircleList = NULL;
    srand((unsigned int)time(NULL));
    for (int i = 0; i < 10; ++i)
    {
        circleListTailInsertData(&pCircleList, rand() % 99 + 1);
    }
    showCircleList(pCircleList);
    printf("endData = %d \n", circleListGetEndNode(&pCircleList, 3)->data);

    system("pause");
}

程式片段(02):Mem.h+Mem.c+mallocfree.c
內容概要:記憶體鏈式管理

///Mem.h
#pragma once

#include <stdlib.h>

void * myMalloc(size_t size);
void myFree(void * pStart);
void * myRealloc(void * pStart, size_t size);

typedef struct
{
    int size;//記憶體尺寸
    void * pStart;//記憶體起始
}Mem;

typedef struct node
{
    Mem * pMem;//資料域
    struct node * pNext;//指標域
}Node;

Node * pLinkList;

void linkListTailInsertData(Node ** ppLinkList, Mem * pMem);

void showLinkList(Node * pLinkList);

Node * linkListSelectFirstData(Node * pLinkLIst, void * pStart);

void linkListDeleteFirstData(Node ** ppLinkList, void * pMem);

void linkListClear(Node ** ppLinkList);

void linkListUpdateFirstData(Node * pLinkList, void * pOldStart, Mem * pNewMem);

void showLinkListInfo(Node * pLinkList);
///Mem.c
#include "Mem.h"
#include <stdlib.h>
#include <stdio.h>

void * myMalloc(size_t size)
{
    void * pStart = malloc(size);
    printf("分配的記憶體地址為:%p,記憶體尺寸為:%d \n",pStart, size);
    Mem * pMem = (Mem *)malloc(sizeof(Mem));
    pMem->pStart = pStart;
    pMem->size = size;
    linkListTailInsertData(&pLinkList, pMem);
    return pStart;
}

void myFree(void * pStart)
{
    printf("記憶體地址:%p處開始釋放! \n", pStart);
    Node * pMem = linkListSelectFirstData(pLinkList, pStart);
    if (NULL == pMem)
        return;
    linkListDeleteFirstData(&pLinkList, pMem);
    free(pMem);
}

void * myRealloc(void * pStart, size_t size)
{
    void * pTmp = realloc(pStart, size);
    Mem mem;
    mem.pStart = pTmp;
    mem.size = size;
    linkListUpdateFirstData(pLinkList, pStart, &mem);
    printf("記憶體地址為:%p的記憶體重新分配到記憶體地址為:%p,尺寸為%d! \n", pStart, pTmp, size);
    return pTmp;
}

void linkListTailInsertData(Node ** ppLinkList, Mem * pMem)
{
    if (NULL == ppLinkList)
        abort();
    Node * pNew = (Node *)malloc(sizeof(Node));
    pNew->pMem = pMem;
    pNew->pNext = NULL;
    if (NULL == *ppLinkList)
    {
        *ppLinkList = pNew;
    }
    else
    {
        Node * pTmp = *ppLinkList;
        while (NULL != pTmp->pNext)
        {
            pTmp = pTmp->pNext;
        }
        pTmp->pNext = pNew;
    }
}

void showLinkList(Node * pLinkList)
{
    if (NULL == pLinkList)
        abort();
    printf("MemAddr:%p, MemSize:%d \n", pLinkList->pMem->pStart, pLinkList->pMem->size);
    showLinkList(pLinkList->pNext);
}

Node * linkListSelectFirstData(Node * pLinkList, void * pStart)
{
    if (NULL == pLinkList)
        abort();
    for (Node * pTmp = pLinkList; NULL != pTmp; pTmp = pTmp->pNext)
    {
        if (pStart == pTmp->pMem->pStart)
        {
            return pTmp;
        }
    }
    return NULL;
}

void linkListDeleteFirstData(Node ** ppLinkList, void * pStart)
{
    if (NULL == ppLinkList || NULL == *ppLinkList)
        abort();
    int find = 0;
    Node * p1 = NULL;
    Node * p2 = *ppLinkList;
    while (NULL != p2)
    {
        if (pStart == p2->pMem->pStart)
        {
            find = 1;
            break;
        }
        p1 = p2;
        p2 = p2->pNext;
    }
    if (!find)
        return;
    if (*ppLinkList == p2)
    {
        *ppLinkList = p2->pNext;
        free(p2);
        return;
    }
    p1->pNext = p2->pNext;
    free(p2);
}

void linkListClear(Node ** ppLinkList)
{
    if (NULL == ppLinkList)
        abort();
    if (NULL == *ppLinkList)
        abort();
    Node * p1 = NULL;
    Node * p2 = *ppLinkList;
    while (NULL != p2->pNext)
    {
        p1 = p2;
        p2 = p2->pNext;
        free(p1->pMem->pStart);
        free(p1);
    }
    free((*ppLinkList)->pMem->pStart);
    free(*ppLinkList);
}

void linkListUpdateFirstData(Node * pLinkList, void * pOldStart, Mem * pMem)
{
    if (NULL == pLinkList)
        abort();
    for (Node * pTmp = pLinkList; NULL != pTmp; pTmp = pTmp->pNext)
    {
        if (pOldStart == pTmp->pMem->pStart)
        {
            pTmp->pMem->pStart = pMem->pStart;
            pTmp->pMem->size = pMem->size;
            return;
        }
    }
}

void showLinkListInfo(Node * pLinkList)
{
    int addrNum = 0;
    int memSize = 0;
    for (Node * pTmp = pLinkList; NULL != pTmp; pTmp = pTmp->pNext)
    {
        ++addrNum;
        memSize += pTmp->pMem->size;
        printf("第%2d塊兒記憶體,記憶體地址:%p,記憶體尺寸:%d! \n", addrNum, pTmp->pMem->pStart, pTmp->pMem->size);
    }
    printf("本程式手動開闢了%d塊兒記憶體,總計佔用%d位元組的堆記憶體! \n", addrNum, memSize);
}
///mallocfree.c
#include "mem.h"

//01.在當前檔案當中生效的巨集定義
//  1.巨集名替換使用(原始習慣保留)
//  2.注意該巨集定義的劫持範圍為本檔案
#define malloc myMalloc
#define free myFree
#define realloc myRealloc

//02.劫持特點:
//  1.劫持函式
//  2.劫持型別
//注:可以採用函式包裝的方式實現劫持效果!
int main01(void)
{
    int a;
    int * p;
    void * p1 = malloc(1024 * 1024 * 100);
    void * p2 = malloc(1024 * 1024 * 100);
    void * p3 = malloc(1024 * 1024 * 100);
    void * p4 = malloc(1024 * 1024 * 100);
    showLinkListInfo(pLinkList);
    realloc(p1, 1024 * 1024 * 200);
    showLinkListInfo(pLinkList);
    free(p2);
    free(p2);
    free(10003);
    showLinkListInfo(pLinkList);
    linkListClear(pLinkList);
    showLinkListInfo(pLinkList);

    system("pause");
}

程式片段(03):Stack.h+Stack.c+linkstack.c
內容概要:正向鏈式棧

///Stack.h
#pragma once 

//01.與棧結構相關的概念:
//  1.按實現資料結構的不同:
//      (1).陣列棧+連結串列棧
//      (2).兩種結構的異同點:
//          陣列棧:定長
//          連結串列棧:變長
//  2.按照實現原理的不同:
//      (1).正向棧+反向棧
//      (2).兩種結構的異同點:
//          正向棧:一直都是尾插法+獲取棧頂資料+資料量可控
//          反向棧:一直都是頭插法+獲取棧底資料+資料量不可控
#define DT int

//02.連結串列當中的每個節點
//  所儲存的資料內容自定義
typedef struct node
{
    int id;//節點編號
    DT data;//資料域
    struct node *pNext;//指標域
}Node;

void initStack(Node ** ppStack);
void showStack(Node * pStack);
void pushStack(Node ** ppStack, int id, DT data);
void popStack(Node ** ppStack, Node * pData);
void clearStack(Node ** ppStack);
///Stack.c
#include "Stack.h"
#include <stdlib.h>
#include <stdio.h>

void initStack(Node ** ppStack)
{
    if (NULL == ppStack)
        abort();
    //初始化棧結構
    *ppStack = NULL;
    //初始化首節點
    //(*ppStack)->id = 0;
    //(*ppStack)->data = 0;
    //(*ppStack)->pNext = NULL;
}

void showStack(Node * pStack)
{
    //if (NULL == pStack)
    //  abort();
    //for (Node * pTmp = pStack; NULL != pTmp; pTmp = pTmp->pNext)
    //{//迴圈方式
    //  printf("id:%3d---data:%3d", pTmp->id, pTmp->data);
    //}
    if (NULL == pStack)
        return;
    printf("id:%3d---data:%3d", pStack->id, pStack->data);
    showStack(pStack->pNext);
}

void pushStack(Node ** ppStack, int id, DT data)
{
    if (NULL == ppStack)
        abort();
    Node * pNew = (Node *)malloc(sizeof(Node));
    pNew->id = id;
    pNew->data = data;
    pNew->pNext = NULL;
    if (NULL == *ppStack)
    {
        *ppStack = pNew;
        return;
    }
    Node * pTmp = *ppStack;
    while (NULL != pTmp->pNext)
    {
        pTmp = pTmp->pNext;
    }
    pTmp->pNext = pNew;
}

void popStack(Node ** ppStack, Node * pData)
{
    if (NULL == ppStack)
        abort();
    if (NULL == (*ppStack)->pNext)
    {
        pData->id = (*ppStack)->id;
        pData->data = (*ppStack)->data;
        free(*ppStack);
        *ppStack = NULL;
        return;
    }
    Node * pTmp = *ppStack;
    while (NULL != pTmp->pNext->pNext)
    {
        pTmp = pTmp->pNext;
    }
    pData->id = pTmp->pNext->id;
    pData->data = pTmp->pNext->data;
    free(pTmp->pNext);
    pTmp->pNext = NULL;
}

void clearStack(Node ** ppStack)
{
    if (NULL == ppStack)
        abort();
    if (NULL == *ppStack)
        return;
    Node * p1 = NULL;
    Node * p2 = *ppStack;
    while (NULL != p2->pNext)
    {
        p1 = p2;
        p2 = p2->pNext;
        free(p1);
    }
    *ppStack = NULL;
}
///linkstack.c
#include "Stack.h"
#include <stdlib.h>
#include <stdio.h>

void decToBin(int decValue)
{
    if (0 == decValue)
        return;
    decToBin(decValue / 2);
    printf("%d", decValue % 2);
}

int main01(void)
{
    //decToBin(1000);

    Node * pStack = NULL;
    initStack(&pStack);
    int decValue = 1000;
    while (decValue)
    {
        pushStack(&pStack, 0, decValue % 2);
        decValue /= 2;
    }
    while (NULL != pStack)
    {
        Node  tmp;
        popStack(&pStack, &tmp);
        printf("%d", tmp.data);
    }
    printf("\n");
    clearStack(&pStack);

    decValue = 1000;
    while (decValue)
    {
        pushStack(&pStack, 0, decValue % 2);
        Node tmp;
        popStack(&pStack, &tmp);
        printf("%d", tmp.data);
        decValue /= 2;
    }
    printf("\n");

    system("pause");
}

int main02(void)
{
    Node * pStack = NULL;
    initStack(&pStack);
    int decValue = 10;
    while (decValue)
    {
        pushStack(&pStack, 0, decValue % 2);
        decValue /= 2;
    }
    while (pStack)
    {
        Node tmp;
        popStack(&pStack, &tmp);
        printf("%d", tmp.data);
    }

    system("pause");
}

程式片段(04):Stack.h+Stack.c+main.c
內容概要:反向鏈式棧

///Stack.h
#pragma once

#define DT int 

typedef struct node
{
    int id;
    int data;
    struct node * pNext;
}Node;

void initStack(Node ** ppStack);

void showStack(Node * pStack);

void pushStack(Node ** ppSack, int id, DT data);

void popStack(Node ** ppStack, Node * pNode);

void clearStack(Node ** ppStack);
///Stack.c
#include "Stack.h"
#include <stdlib.h>
#include <stdio.h>

void initStack(Node ** ppStack)
{
    if (NULL == ppStack)
        abort();
    *ppStack = NULL;
}

void showStack(Node * pStack)
{
    if (NULL == pStack)
        abort();
    for (Node * pTmp = pStack; NULL != pTmp; pTmp = pTmp->pNext)
    {
        printf("%d", pTmp->data);
    }
    printf("\n");
}

void pushStack(Node ** ppStack, int id, DT data)
{
    if (NULL == ppStack)
        abort();
    Node * pNew = (Node *)malloc(sizeof(Node));
    pNew->id = id;
    pNew->data = data;
    pNew->pNext = NULL;
    if (NULL == *ppStack)
    {
        *ppStack = pNew;
        return;
    }
    pNew->pNext = *ppStack;
    *ppStack = pNew;
}

void popStack(Node ** ppStack, Node * pData)
{
    if (NULL == ppStack)
        abort();
    if (NULL == *ppStack)
        return;
    pData->id = (*ppStack)->id;
    pData->data = (*ppStack)->data;
    if (NULL == (*ppStack)->pNext)
    {
        *ppStack = NULL;
        free(*ppStack);
        return;
    }
    Node * pTmp = *ppStack;
    *ppStack = (*ppStack)->pNext;
    free(pTmp);
}

void clearStack(Node ** ppStack)
{
    if (NULL == ppStack)
        abort();
    if (NULL == *ppStack)
        return;
    Node * p1 = NULL;
    Node * p2 = *ppStack;
    for (Node * pTmp = *ppStack; NULL != pTmp; pTmp = pTmp->pNext)
    {
        p1 = p2;
        p2 = p2->pNext;
        free(p1);
    }
    *ppStack = NULL;
}
///main.c
#include "Stack.h"
#include <stdlib.h>
#include <stdio.h>

void decToBin(int decValue)
{
    if (0 == decValue)
        return;
    decToBin(decValue /= 2);
    if (0 != (decValue /= 2))
    {
        printf("%d", decValue %= 2);
    }
}

int main(void)
{
    //decToBin(1000);

    Node * pStack = NULL;
    initStack(&pStack);
    int decValue = 1000;
    while (decValue)
    {
        pushStack(&pStack, 0, decValue % 2);
        decValue /= 2;
    }
    while (NULL != pStack)
    {
        Node tmp;
        popStack(&pStack, &tmp);
        printf("%d", tmp.data);
    }

    system("pause");
}

程式片段(05):Queue.h+Queue.c+main.c
內容概要:鏈式佇列

///Queue.h
#pragma once

#define DT int

typedef struct node
{
    DT data;
    struct node * pNext;
}Node;

void initQueue(Node ** ppQueue);
void showQueue(Node * pQueue);
void enQueue(Node ** ppQueue, DT data);
void deQueue(Node ** ppQueue, Node * pData);
void clearQueue(Node ** ppQueue);
///Queue.c
#include "Queue.h"
#include <stdlib.h>
#include <stdio.h>

void initQueue(Node ** ppQueue)
{
    if (NULL == ppQueue)
        abort();
    *ppQueue = NULL;
}

void showQueue(Node * pQueue)
{
    if (NULL == pQueue)
        return;
    for (Node * pTmp = pQueue; NULL != pTmp; pTmp = pTmp->pNext)
    {
        printf("%d", pTmp->data);
    }
    printf("\n");
}

void enQueue(Node ** ppQueue, DT data)
{
    if (NULL == ppQueue)
        abort();
    Node * pNew = (Node *)malloc(sizeof(Node));
    pNew->data = data;
    pNew->pNext = NULL;
    if (NULL == *ppQueue)
    {
        *ppQueue = pNew;
        pNew->pNext = NULL;
        return;
    }
    Node * pTmp = *ppQueue;
    while (NULL != pTmp->pNext)
    {
        pTmp = pTmp->pNext;
    }
    pTmp->pNext = pNew;
}

void deQueue(Node ** ppQueue, Node * pData)
{
    if (NULL == ppQueue)
        abort();
    if (NULL == *ppQueue)
        return;
    pData->data = (*ppQueue)->data;
    if (NULL == (*ppQueue)->pNext)
    {
        free(*ppQueue);
        *ppQueue = NULL;
        return;
    }
    Node * pTmp = *ppQueue;
    *ppQueue = (*ppQueue)->pNext;
    free(pTmp);
}

void clearQueue(Node ** ppQueue)
{
    if (NULL == ppQueue)
        abort();
    if (NULL == *ppQueue)
        return;
    Node * p1 = NULL;
    Node * p2 = *ppQueue;
    while (NULL != p2)
    {
        p1 = p2;
        p2 = p2->pNext;
        free(p1);
    }
    *ppQueue = NULL;
}
///main.c
#include "Queue.h"
#include <stdlib.h>

int main(void)
{
    Node * pQueue = NULL;
    initQueue(&pQueue);
    for (int i = 0; i < 10; ++i)
    {
        printf("enQueue:%2d \n", i + 1);
        enQueue(&pQueue, i + 1);
        showQueue(pQueue);
    }
    while (NULL != pQueue)
    {
        Node tmp;
        deQueue(&pQueue, &tmp);
        printf("deQueue:%2d \n", tmp.data);
        showQueue(pQueue);
    }

    system("pause");
}

程式片段(06):Queue.h+Queue.c+優先佇列測試.c
內容概要:優先鏈式佇列

///Queue.h
#pragma once

//01.關於佇列內容:
//  1.佇列沒有正反向的區別:
//      只有實現上面的正反向區別
//  2.佇列分為兩種:
//      普通佇列+優先佇列
#define DT int

typedef struct node
{
    int priority;//優先佇列
    DT data;
    struct node * pNext;
}Node;

void initQueue(Node ** ppQueue);

void showQueue(Node * pQueue);

void enQueue(Node ** ppQueue, int priority, DT data);

void deQueue(Node ** ppQueue, Node * pData);

void clearQueue(Node ** ppQueue);
///Queue.c
#include "Queue.h"
#include <stdlib.h>
#include <stdio.h>

void initQueue(Node ** ppQueue)
{
    if (NULL == ppQueue)
        return;
    if (NULL == *ppQueue)
        return;
    *ppQueue = NULL;
}

void showQueue(Node * pQueue)
{
    if (NULL == pQueue)
        return;
    for (Node * pTmp = pQueue; NULL != pTmp; pTmp = pTmp->pNext)
    {
        printf("優先順序:%2d, 資料:%4d \n", pTmp->priority, pTmp->data);
    }
    printf("\n");
}

void enQueue(Node ** ppQueue, int priority, DT data)
{
    if (NULL == ppQueue)
        abort();
    Node * pNew = (Node *)malloc(sizeof(Node));
    pNew->priority = priority;
    pNew->data = data;
    pNew->pNext = NULL;
    if (NULL == *ppQueue)
    {
        *ppQueue = pNew;
        return;
    }
    if (priority > (*ppQueue)->priority)
    {
        pNew->pNext = *ppQueue;
        *ppQueue = pNew;
        return;
    }
    //優先佇列:前插實現
    Node * p1 = NULL;
    Node * p2 = *ppQueue;
    int find = 0;
    while (NULL != p2)
    {
        if (priority > p2->priority)
        {
            find = 1;
            break;
        }
        p1 = p2;
        p2 = p2->pNext;
    }
    if (find)
    {
        p1->pNext = pNew;
        pNew->pNext = p2;
        return;
    }
    Node * pTmp = *ppQueue;
    while (NULL != pTmp->pNext)
    {
        pTmp = pTmp->pNext;
    }
    pTmp->pNext = pNew;
    //優先佇列:後插實現
}

void deQueue(Node ** ppQueue, Node * pData)
{
    if (NULL == ppQueue)
        abort();
    if (NULL == *ppQueue)
        return;
    pData->priority = (*ppQueue)->priority;
    pData->data = (*ppQueue)->data;
    if (NULL == (*ppQueue)->pNext)
    {
        free(*ppQueue);
        *ppQueue = NULL;
        return;
    }
    Node * pTmp = *ppQueue;
    *ppQueue = (*ppQueue)->pNext;
    free(pTmp);
}
///優先佇列測試.c
#include "Queue.h"
#include <stdlib.h>
#include <stdio.h>

int main01(void)
{
    Node * pQueue;
    initQueue(&pQueue);
    enQueue(&pQueue, 4, 100);
    enQueue(&pQueue, 2, 100);
    enQueue(&pQueue, 1, 100);
    enQueue(&pQueue, 3, 100);
    enQueue(&pQueue, 5, 100);
    enQueue(&pQueue, 5, 102);
    enQueue(&pQueue, 5, 101);
    printf("優先佇列此時狀態! \n");
    showQueue(pQueue);

    while (NULL != pQueue)
    {
        Node tmp;
        deQueue(&pQueue, &tmp);
        printf("優先順序:%2d, 資料:%4d \n", tmp.priority, tmp.data);
    }

    system("pause");
}

程式片段(07):CppList.cpp+List.h+List.c+main.c
內容概要:List

///CppList.cpp
#include <stdio.h>
#include <stdlib.h>
#include <list>

using namespace std;//匯入名稱空間

int main01(void)
{
    list<int> myList;//設定元素型別
    //list<list<list<int>>> myHighList; 
    for (int i = 0; i < 10; ++i)
    {
        myList.push_back(i + 1);
    }
    for (auto tmp : myList)
    {//遍歷一個STL容器
        printf("%d", tmp);
    }

    system("pause");
    return 1;
}
///List.h
#pragma once

typedef struct node
{
    int data;//資料域
    struct node * pPre;//前驅
    struct node * pNext;//後期
}Node;

typedef struct list
{
    Node * pHead;//頭指標
    Node * pTail;//尾指標
}List;

void initList(List * pList);

void listHeadInsert(List * pList, int data);
void listTailInsert(List * pList, int data);

void showList(List * pList);
void revShowList(List * pList);

Node * listSelelctFirst(List * pList, int data);
Node * listRevSelectFirst(List * pList, int data);

void listRandInsert(List * pList, int findData, int insertData);

void listDeleteFirst(List * pList, int data);
void listRevDeleteFirst(List * pList, int data);
///List.c
#include "List.h"
#include <stdlib.h>
#include <stdio.h>

void initList(List * pList)
{
    if (NULL == pList)
        abort();
    pList->pHead = pList->pTail = NULL;
}

void listHeadInsert(List * pList, int data)
{
    if (NULL == pList)
        abort();
    Node * pNew = (Node *)malloc(sizeof(Node));
    pNew->data = data;
    pNew->pPre = pNew->pNext = NULL;
    if (NULL == pList->pHead)
    {//空雙鏈
        pList->pHead = pList->pTail = pNew;
        return;
    }
    pNew->pNext = pList->pHead;
    pList->pHead->pPre = pNew;
    pList->pHead = pNew;
}

void listTailInsert(List * pList, int data)
{
    if (NULL == pList)
        abort();
    Node * pNew = (Node *)malloc(sizeof(Node));
    pNew->data = data;
    pNew->pPre = pNew->pNext = NULL;
    if (NULL == pList->pTail)
    {
        pList->pHead = pList->pTail = pNew;
        return;
    }
    pList->pTail->pNext = pNew;
    pNew->pPre = pList->pTail;
    pList->pTail = pNew;
}

void showList(List * pList)
{
    if (NULL == pList)
        abort();
    if (NULL == pList->pHead)
    {
        return;
    }
    for (Node * pTmp = pList->pHead; NULL != pTmp; pTmp = pTmp->pNext)
    {
        printf("%3d", pTmp->data);
    }
    printf("\n");
}

void revShowList(List * pList)
{
    if (NULL == pList)
        abort();
    if (NULL == pList->pTail)
        return;
    for (Node * pTmp = pList->pTail; NULL != pTmp; pTmp = pTmp->pPre)
    {
        printf("%3d", pTmp->data);
    }
}

Node * listSelectFirst(List * pList, int data)
{
    if (NULL == pList)
        abort();
    if (NULL == pList->pHead)
        return;
    for (Node * pTmp = pList->pHead; NULL != pTmp; pTmp = pTmp->pNext)
        if (data == pTmp->data)
            return pTmp;
    return NULL;
}

Node * listRevSelectFirst(List * pList, int data)
{
    if (NULL == pList)
        abort();
    if (NULL == pList->pTail)
        return;
    for (Node * pTmp = pList->pTail; NULL != pTmp; pTmp = pTmp->pPre)
        if (data == pTmp->data)
            return pTmp;
    return NULL;
}

void listRandInsert(List * pList, int findData, int insertData)
{
    if (NULL == pList)
        abort();
    if (NULL == pList->pTail)
        return;
    int find = 0;
    Node * p1 = NULL;
    //逆向前插<正向後插>
    //正向前插<逆向後插>
    Node * p2 = pList->pTail;
    while (NULL != p2)
    {//逆向前插
        if (findData == p2->data)
        {
            find = 1;
            break;
        }
        p1 = p2;
        p2 = p2->pPre;
    }
    if (!find)
        return;
    Node * pNew = (Node *)malloc(sizeof(Node));
    pNew->data = insertData;
    pNew->pPre = pNew->pNext = NULL;
    if (pList->pTail == p2)
    {
        pList->pTail->pNext = pNew;
        pList->pTail = pNew;
        return;
    }
    p2->pNext = pNew;
    pNew->pPre = p2;
    p2->pPre = pNew;
    pNew->pNext = p1;
}

void listRevDeleteFirst(List * pList, int data)
{
    if (NULL == pList)
        abort();
    if (NULL == pList->pTail)
        return;
    int find = 0;
    Node * p1 = NULL;
    Node * p2 = pList->pTail;
    while (NULL != p2)
    {
        if (data == p2->data)
        {
            find = 1;
            break;
        }
        p1 = p2;
        p2 = p2->pPre;
    }
    if (!find)
        return;
    if (p2 == pList->pTail)
    {
        pList->pTail->pPre->pNext = NULL;
        Node * pTmp = pList->pTail;
        pList->pTail = pList->pTail->pPre;
        free(pTmp);
        return;
    }
    if (p2 == pList->pHead)
    {
        pList->pTail->pNext->pPre = NULL;
        Node * pTmp = pList->pHead;
        pList->pHead = pList->pHead->pNext;
        free(pTmp);
        return;
    }
    p1->pPre = p2->pPre;
    p2->pPre->pNext = p1;
    free(p2);
}
///main.c
#include "List.h"
#include <stdio.h>
#include <stdlib.h>

int main01(void)
{
    List list;
    initList(&list);
    //listHeadInsert(&list, 5);
    //listHeadInsert(&list, 4);
    //listHeadInsert(&list, 3);
    //listHeadInsert(&list, 2);
    //listHeadInsert(&list, 1);
    listTailInsert(&list, 1);
    listTailInsert(&list, 2);
    listTailInsert(&list, 3);
    listTailInsert(&list, 4);
    listTailInsert(&list, 5);
    //listRevDeleteFirst(&list, 3);
    listRandInsert(&list, 3, 6);
    showList(&list);

    system("pause");
}

相關文章