20160225.CCPP体系详解(0035天)

程序片段(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("
");
}

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 
", countCircleList(pCircleList));
    //circleListRandInsertData(&pCircleList, 3, 7);
    showCircleList(pCircleList);
    //printf("%3d 
", 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 
", 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 
",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处开始释放! 
", 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! 
", 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 
", 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! 
", addrNum, pTmp->pMem->pStart, pTmp->pMem->size);
    }
    printf("本程序手动开辟了%d块儿内存,总计占用%d字节的堆内存! 
", 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("
");
    clearStack(&pStack);

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

    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("
");
}

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("
");
}

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 
", i + 1);
        enQueue(&pQueue, i + 1);
        showQueue(pQueue);
    }
    while (NULL != pQueue)
    {
        Node tmp;
        deQueue(&pQueue, &tmp);
        printf("deQueue:%2d 
", 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 
", pTmp->priority, pTmp->data);
    }
    printf("
");
}

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("优先队列此时状态! 
");
    showQueue(pQueue);

    while (NULL != pQueue)
    {
        Node tmp;
        deQueue(&pQueue, &tmp);
        printf("优先级:%2d, 数据:%4d 
", 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("
");
}

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");
}
原文地址:https://www.cnblogs.com/new0801/p/6176802.html