SeqPriorityQueue——顺序优先级队列
来源:互联网 发布:人工智能电影女主角 编辑:程序博客网 时间:2024/06/05 09:40
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 100typedef 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跳出循环。
- SeqPriorityQueue——顺序优先级队列
- 顺序优先级队列
- 顺序表实现优先级队列
- 优先级队列——PriorityQueue
- 队列——顺序队列
- 安卓 消息队列 优先级 顺序
- LinkPriorityQueue——链式优先级队列
- 堆排序2——优先级队列
- STL——priority_queue 优先级队列
- 优先级队列(priority——queue)
- 堆的应用——优先级队列
- 堆的应用——优先级队列
- 数据结构——队列之顺序队列
- 数据结构应用标准模版库STL——优先级队列(优先级队列排序)
- 数据结构——顺序队列
- 数据结构Java实现——②队列-->队列的“奇葩”二 优先级队列
- java集合——队列和双端队列+优先级队列
- 优先级队列——用C++模板实现
- android2.3源码下载编译全过程(附图及文档)修改版
- android listview 使用--转载
- scp
- 【redhat5.5】dns 基本配置
- android 中系统自带的主题与样式(theme and style)
- SeqPriorityQueue——顺序优先级队列
- Android重量级开发之--提高android启动速度研究
- 在terminal 下开启网卡
- Git命令
- Android基础 : Android Content Provider[转]
- java setXORMode()理解
- android 之 TouchEvent 示例
- 记录
- 双生花