链式优先级队列
标签(空格分隔): 数据结构、优先级队列
一、优先级队列的应用场合
线程池 - 由一个任务队列和一组处理队列的线程组成。一旦工作进程需要处理某个可能“阻塞”的 操作,不用自己操作,将其作为一个任务放到线程池的队列,接着会被某个空闲线程提取处理,这里的任务紧急情况各不相同,可以用优先级队列来存储这些任务。
二、链式优先级队列
#include <iostream>
#include <Windows.h>
#include <time.h>
using namespace std;
/************************************
* 优先级队列
* 1、入队无序,出队根据优先级的顺序出队
* 2、这里采用链式队列
*
**************************************/
#define MAX_SIZE 12
#define isLess(a,b) a<b
typedef int DATATYPE;
typedef struct _Node {
DATATYPE data;
int priority;
_Node *next;
}Node;
typedef _Node* queuePtr;
typedef struct _PriorityQueue {
int length;
queuePtr front;
queuePtr rear;
}PriorityQueue;
//初始化优先级队列
bool initPriorityQueue(PriorityQueue* Q) {
if (!Q) return false;
Q->length = 0;
Q->front = Q->rear = NULL;
return true;
}
bool isFull(PriorityQueue* Q) {
if (!Q) return false;
if (Q->length == MAX_SIZE) {
return true;
}
else {
return false;
}
}
bool isEmpty(PriorityQueue* Q) {
if (!Q) return false;
if (Q->length == 0) return true;
else {
return false;
}
}
//入队无序,这里采用尾插法
bool enterPriorityQueue(PriorityQueue* Q, DATATYPE data, int priority) {
if (!Q) return false;
queuePtr node = new Node;
node->data = data;
node->priority = priority;
node->next = NULL;
if (isFull(Q)) {
cout << "优先队列已满" << endl;
return false;
}
else {
if (isEmpty(Q)) {
Q->front = node;
Q->rear = node;
}
else {
Q->rear->next = node;
Q->rear = node;
}
Q->length++;
return true;
}
}
//根据优先级顺序出队列
bool popPriorityQueue(PriorityQueue* Q,DATATYPE& data,int &priority) {
if(!Q) return false;
if (isEmpty(Q)) {
cout << "队列已为空" << endl;
return false;
}
//找到优先级最大的元素出队,prioroty由大到小9、8、7...1
queuePtr last = Q->front;
queuePtr temp = last->next;
queuePtr* prev_next = &Q->front;
queuePtr prev_node = NULL;
while (temp) {
//找到优先级更高的元素
if (temp && isLess((*prev_next)->priority, temp->priority)) {
prev_next = &(last->next);
prev_node = last;
}
last = temp;
temp = temp->next;
}
//保存出队元素的数据
data = (*prev_next)->data;
priority = (*prev_next)->priority;
temp = *prev_next;//保存被删除节点
//将出队节点的上一个节点的next值修改为出队元素的下一个节点的地址
*prev_next = temp->next;
Q->length--;
delete temp;
//特殊情况:
//出队之后队列为空
if (isEmpty(Q)) {
Q->rear = NULL;
}
//出队的是最后一个节点
if (prev_node && prev_node->next == NULL) {
Q->rear = prev_node;
}
return true;
}
//打印输出
bool printAll(PriorityQueue* Q) {
if (!Q) return false;
if (isEmpty(Q)) {
cout << "队列为空" << endl;
}
queuePtr temp = Q->front;
while (temp) {
cout << "temp->data:" << temp->data << "\ttemp->priority:" << temp->priority << endl;
temp = temp->next;
}
return true;
}
bool clearQueue(PriorityQueue* Q) {
if (!Q) return false;
queuePtr temp = Q->front;
while (temp) {
Q->front = Q->front->next;
delete temp;
temp = Q->front;
}
return true;
}
//测试代码
void test() {
PriorityQueue* q = new PriorityQueue;
initPriorityQueue(q);
int num;
cout << "enter nums of inputs:";
cin >> num;
while (num) {
enterPriorityQueue(q, num,rand()%10);
num--;
}
cout << "print all:" << endl;
printAll(q);
int data;
int priority;
cout << "出队:" << endl;
while (!isEmpty(q)) {
popPriorityQueue(q, data, priority);
cout << "data:" << data << "\tpriority:" << priority << endl;
}
if (clearQueue(q)) {
cout << "销毁成功" << endl;
}
else {
cout << "销毁失败" << endl;
}
delete q;
}
int main(void) {
srand(time(NULL));
test();
system("pause");
return 0;
}