链式优先级队列

链式优先级队列

标签(空格分隔): 数据结构、优先级队列


一、优先级队列的应用场合

线程池 - 由一个任务队列和一组处理队列的线程组成。一旦工作进程需要处理某个可能“阻塞”的 操作,不用自己操作,将其作为一个任务放到线程池的队列,接着会被某个空闲线程提取处理,这里的任务紧急情况各不相同,可以用优先级队列来存储这些任务。

二、链式优先级队列

#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;
}


三、测试代码:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值