您的位置:首页 > 产品设计 > UI/UE

SeqPriorityQueue——顺序优先级队列

2012-03-29 23:48 549 查看
PS:

1、不用考虑“假溢出”的情况。

2、出队列时间复杂度为O(n)。将出队列元素后的元素均往前移1个索引。

/*
** File name: SeqPriorityQueue.h
** Author: ZhouFeng
** Date: 2012/03/29
*/
#ifndef SEQ_PRIORITY_QUEUE_H
#define SEQ_PRIORITY_QUEUE_H

#define BOOL int
#define TRUE 1
#define FALSE 0
#define ERROR 0
#define SUCCESS 1
#define MAX_SIZE 100

typedef int EleType;

typedef struct QueueEle
{
int nPriority;
EleType data;
}QueueEle;

typedef struct SeqPriorityQueue
{
QueueEle aSPQueue[MAX_SIZE];
int nRear;
int nCount;
}SeqPriorityQueue;

void InitSPQ(SeqPriorityQueue *SPQ);
int SPQAppend(SeqPriorityQueue *SPQ, QueueEle queueEle);
int SPQDelete(SeqPriorityQueue *SPQ, QueueEle *pQueueElement);
BOOL IsSPQEmpty(SeqPriorityQueue *SPQ);
int GetSPQ(SeqPriorityQueue *SPQ, QueueEle *pQueueElement);

#endif


/*
** File name: SeqPriorityQueue.h
** Author: ZhouFeng
** Date: 2012/03/29
*/
#include <stddef.h>
#include <stdlib.h>
#include "SeqPriorityQueue.h"

void InitSPQ(SeqPriorityQueue *SPQ)
{
int i = 0;

if(SPQ == NULL)
{
return;
}

for(i = 0; i < MAX_SIZE; ++i)
{
(SPQ->aSPQueue)[i].nPriority = 0;
(SPQ->aSPQueue)[i].data = 0;
}
SPQ->nRear = 0;
SPQ->nCount = 0;
}

int SPQAppend(SeqPriorityQueue *SPQ, QueueEle queueEle)
{
if(SPQ == NULL || SPQ->nCount >= MAX_SIZE)
{
return ERROR;
}

(SPQ->aSPQueue)[SPQ->nRear] = queueEle;
++(SPQ->nRear);
++(SPQ->nCount);

return SUCCESS;
}

int SPQDelete(SeqPriorityQueue *SPQ, QueueEle *pQueueElement)
{
int index = 0;
int nMaxPriority = 0;
int nMaxIndex = 0;

if(SPQ == NULL || SPQ->nCount <= 0 || pQueueElement == NULL)
{
return ERROR;
}

/* Get the Max priority index. */
nMaxPriority = (SPQ->aSPQueue)[0].nPriority;
for(index = 1; index < SPQ->nCount; ++index)
{
if(nMaxPriority > (SPQ->aSPQueue)[index].nPriority)
{
nMaxPriority = (SPQ->aSPQueue)[index].nPriority;
nMaxIndex = index;
}
else
{
continue;
}
}

(*pQueueElement).nPriority = nMaxPriority;
(*pQueueElement).data = (SPQ->aSPQueue)[nMaxIndex].data;

for(index = nMaxIndex + 1; index < SPQ->nCount; ++index)
{
(SPQ->aSPQueue)[index - 1] = (SPQ->aSPQueue)[index];
}

--(SPQ->nCount);

return SUCCESS;
}

BOOL IsSPQEmpty(SeqPriorityQueue *SPQ)
{
if(SPQ == NULL)
{
return TRUE;
}

if(SPQ->nCount == 0)
{
return TRUE;
}
else
{
return FALSE;
}
}

int GetSPQ(SeqPriorityQueue *SPQ, QueueEle *pQueueElement)
{
int index = 0;
int nMaxPriority = 0;
int nMaxIndex = 0;

if(SPQ == NULL || SPQ->nCount <= 0 || pQueueElement == NULL)
{
return ERROR;
}

/* Get the Max priority index. */
nMaxPriority = (SPQ->aSPQueue)[0].nPriority;
for(index = 1; index < SPQ->nCount; ++index)
{
if(nMaxPriority > (SPQ->aSPQueue)[index].nPriority)
{
nMaxPriority = (SPQ->aSPQueue)[index].nPriority;
nMaxIndex = index;
}
else
{
continue;
}
}

(*pQueueElement).nPriority = nMaxPriority;
(*pQueueElement).data = (SPQ->aSPQueue)[nMaxIndex].data;

return SUCCESS;
}


测试程序:

#include <stdio.h>
#include "SeqPriorityQueue.h"

int main(int argc, char *argv[])
{
SeqPriorityQueue SPQ;
QueueEle qe;
int i = 0;

InitSPQ(&SPQ);
printf("[Original]\nPriority   Data\n");
printf("--------- -------\n");
for(i = 0; i < MAX_SIZE / 2; ++i)
{
qe.nPriority = MAX_SIZE / 2 - i;
qe.data = i;
SPQAppend(&SPQ, qe);
printf("%5d%10d\n", qe.nPriority, qe.data);
}

for(i = 50; i < MAX_SIZE; ++i)
{
qe.nPriority = MAX_SIZE - i;
qe.data = i;
SPQAppend(&SPQ, qe);
printf("%5d%10d\n", qe.nPriority, qe.data);
}
printf("\n");

printf("[Get Queue]\nPriority   Data\n");
printf("--------- -------\n");
while(!IsSPQEmpty(&SPQ))
{
SPQDelete(&SPQ, &qe);
printf("%5d%10d\n", qe.nPriority, qe.data);
}

return 0;
}


GDB调试:clear清除断点、u跳出循环。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: