SeqPriorityQueue——顺序优先级队列
2012-03-29 23:48
549 查看
PS:
1、不用考虑“假溢出”的情况。
2、出队列时间复杂度为O(n)。将出队列元素后的元素均往前移1个索引。
测试程序:
GDB调试:clear清除断点、u跳出循环。
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跳出循环。
相关文章推荐
- C++ - 库函数优先级队列(priority_queue)输出最小值 代码
- STL之优先级队列priority_queue
- STL系列之五 priority_queue 优先级队列
- 并发队列-无界阻塞优先级队列PriorityBlockingQueue原理探究
- 优先级队列Priority_queue
- c++ 优先级队列 priority_queue
- 优先级队列priority_queue
- C++最大堆实现priority_queue优先级队列(算法导论)
- ural 1306. Sequence Median(优先级队列 priority_queue用法)
- priority_queue优先级队列
- leetcode 218. The Skyline Problem 优先级队列PriorityQueue + mulitset模拟Heap
- C++ 标准模板库STL 优先级队列 priority_queue 使用方法与应用介绍(一)
- priority_queue<int,vector<int>,greater<int>>优先队列 按照由小到大顺序
- 解析Java中PriorityQueue优先级队列结构的源码及用法
- 优先级队列(java.util.PriorityQueue<E>)
- 31STL之优先级队列priority_queue
- priority_queue<int,vector<int>,greater<int>>优先队列 按照由小到大顺序
- STL系列之五 priority_queue 优先级队列
- priority_queue优先级队列
- C++ 优先队列 priority_queue 的基本使用方法【定义优先级】