直接看代码吧。嘿嘿~
/*
** File name: LinkPriorityQueue.h
** Author: ZhouFeng
** Date: 2012/03/30
** LPQ -- LinkPriorityQueue
*/
#ifndef LINK_PRIORITY_QUEUE_H
#define LINK_PRIORITY_QUEUE_H
#define ERROR 0
#define SUCCESS 1
#define BOOL int
#define TRUE 1
#define FALSE 0
typedef int EleDataType;
typedef struct EleType
{
EleDataType data;
int nPriority;
}EleType;
typedef struct LPQNode
{
EleType eleData;
struct LPQNode *pNext;
}LPQNode;
typedef struct LinkPriorityQueue
{
int nCount;
struct LPQNode *pRear;
struct LPQNode *pFront;
}LinkPriorityQueue, *PLPQ;
void InitLPQ(PLPQ *pLPQAddr);
BOOL IsLPQEmpty(PLPQ pLPQ);
/* et -- EleType varible. */
int LPQAppend(PLPQ pLPQ, EleType et);
int LPQDelete(PLPQ pLPQ, EleType *et);
int GetLPQ(PLPQ pLPQ, EleType *et);
void FreeLPQ(PLPQ *pLPQ);
#endif
/*
** File name: LinkPriorityQueue.c
** Author: ZhouFeng
** Date: 2012/03/30
** LPQ operation definition.
*/
#include <stddef.h>
#include <stdlib.h>
#include "LinkPriorityQueue.h"
void InitLPQ(PLPQ *pLPQAddr)
{
*pLPQAddr = (PLPQ)malloc(sizeof(LinkPriorityQueue));
(*pLPQAddr)->pRear = NULL;
(*pLPQAddr)->pFront = NULL;
(*pLPQAddr)->nCount = 0;
}
BOOL IsLPQEmpty(PLPQ pLPQ)
{
if(pLPQ == NULL)
{
return TRUE;
}
if(pLPQ->nCount == 0)
{
return TRUE;
}
else
{
return FALSE;
}
}
int LPQAppend(PLPQ pLPQ, EleType et)
{
LPQNode *pNewLPQNode;
if(pLPQ == NULL)
{
return ERROR;
}
pNewLPQNode = (LPQNode*)malloc(sizeof(LPQNode));
pNewLPQNode->eleData.data =et.data;
pNewLPQNode->eleData.nPriority = et.nPriority;
pNewLPQNode->pNext = NULL;
if(pLPQ->pRear != NULL)
{
pLPQ->pRear->pNext = pNewLPQNode;
}
pLPQ->pRear = pNewLPQNode;
if(pLPQ->pFront == NULL)
{
pLPQ->pFront = pNewLPQNode;
}
++(pLPQ->nCount);
return SUCCESS;
}
int LPQDelete(PLPQ pLPQ, EleType *et)
{
LPQNode *pIterator;
int nMaxPriority;
LPQNode *pMaxPriority, *pPrious, *pMaxPrious;
if(pLPQ == NULL || IsLPQEmpty(pLPQ) || et == NULL)
{
return ERROR;
}
/*
** Find the Max Priority Node.
*/
/* Set the max-priority node pointer. */
pMaxPriority = pLPQ->pFront;
/* Set the front of max-priority node pointer and prious pointer. */
pPrious = pMaxPrious = pLPQ->pFront;
pIterator = pMaxPriority->pNext;
/* Init the Max priority number. */
nMaxPriority = pMaxPriority->eleData.nPriority;
while(pIterator != NULL)
{
if(nMaxPriority > pIterator->eleData.nPriority)
{
nMaxPriority = pIterator->eleData.nPriority;
/* Save the Max-Priority node pointer. */
pMaxPriority = pIterator;
/* Save the Prious pointer. */
pMaxPrious = pPrious;
}
pPrious = pIterator;
pIterator = pIterator->pNext;
}
(*et).data = pMaxPriority->eleData.data;
(*et).nPriority = pMaxPriority->eleData.nPriority;
/* Check the pMaxPriority has changed. */
if(pMaxPriority != pLPQ->pFront)
{
/* pFront needn't changed. */
pMaxPrious->pNext = pMaxPriority->pNext;
}
else
{
/* pFront changed. */
pLPQ->pFront = pLPQ->pFront->pNext;
}
free(pMaxPriority);
--(pLPQ->nCount);
return SUCCESS;
}
int GetLPQ(PLPQ pLPQ, EleType *et)
{
LPQNode *pIterator;
int nMaxPriority;
LPQNode *pMaxPriority, *pPrious, *pMaxPrious;
if(pLPQ == NULL || IsLPQEmpty(pLPQ) || et == NULL)
{
return ERROR;
}
/* Find the Max Priority Node. */
pMaxPriority = pLPQ->pFront;
pPrious = pMaxPrious = pLPQ->pFront;
pIterator = pMaxPriority->pNext;
nMaxPriority = pLPQ->pFront->eleData.nPriority;
while(pIterator != NULL)
{
if(nMaxPriority > pIterator->eleData.nPriority)
{
nMaxPriority = pIterator->eleData.nPriority;
pMaxPriority = pIterator;
pMaxPrious = pPrious;
}
pPrious = pIterator;
pIterator = pIterator->pNext;
}
(*et).data = pMaxPriority->eleData.data;
(*et).nPriority = pMaxPriority->eleData.nPriority;
return SUCCESS;
}
void FreeLPQ(PLPQ *pLPQ)
{
LPQNode *pIterator, *pTemp;
pIterator = (*pLPQ)->pFront;
while(pIterator != NULL)
{
pTemp = pIterator;
pIterator = pIterator->pNext;
free(pTemp);
}
*pLPQ = NULL;
}
测试程序:
#include <stdio.h>
#include "LinkPriorityQueue.h"
#define TEST_SIZE 10
int main(int argc, char *argv[])
{
EleType et;
LinkPriorityQueue *pLPQ;
int i = 0;
InitLPQ(&pLPQ);
printf("[Original]\nPriority Data\n");
printf("-------- ----\n");
for(i = 0; i < TEST_SIZE / 2; ++i)
{
et.data = i;
et.nPriority = i;
LPQAppend(pLPQ, et);
printf("%5d%10d\n", et.nPriority, et.data);
}
for(i = TEST_SIZE / 2; i < TEST_SIZE; ++i)
{
et.data = i;
et.nPriority = i % (TEST_SIZE / 2);
printf("%5d%10d\n", et.nPriority, et.data);
LPQAppend(pLPQ, et);
}
/* Get the Max Priority Data. */
GetLPQ(pLPQ, &et);
printf("The max-priority is %d, data=%d.\n", et.nPriority, et.data);
printf("Priority Data\n");
printf("-------- ----\n");
while(LPQDelete(pLPQ, &et) == SUCCESS)
{
printf("%5d%10d\n", et.nPriority, et.data);
}
FreeLPQ(&pLPQ);
return 0;
}
输出截图:

本文介绍了一种链式优先队列的数据结构实现方法,并提供了完整的C语言代码示例。该实现支持基本操作如初始化、判断空、插入、删除最高优先级元素等。
1345

被折叠的 条评论
为什么被折叠?



