一个C优先级队列实现
刚下班没事干,实现了一个简单的优先级队列
#include <stdlib.h>
#include <stdio.h>
typedef void (*pqueue_setindex) (void *obj, int pq_index);
typedef int (*pqueue_cmp)	(void* obj1, void* obj2);
typedef struct pqueue_struct
{
	void 	**heap;
	int 	numelem;
	pqueue_setindex	setindex;
	pqueue_cmp	cmp;
}pqueue;
pqueue* pqueue_new(int pq_size, pqueue_setindex setindexf, pqueue_cmp cmpf)
{
	pqueue * pq	=	(pqueue*)calloc(1, sizeof(pqueue));
	if( pq == NULL) goto error;
	pq->heap	= (void**)calloc(1, pq_size*sizeof(void*));
	if( pq->heap == NULL) {
		free(pq); 
		goto error; 
	}
	pq->numelem	 = 0;
	pq->setindex	=	setindexf;
	pq->cmp	=	cmpf;
	return pq;
error:
	return NULL;
}
void pqueue_delete(pqueue * pq)
{
	if(pq == NULL || pq->heap == NULL) return;
	free(pq->heap);
	free(pq);
}
int pqueue_upheap(pqueue* pq, int pq_index)
{
	void *obj = (void*)pq->heap[pq_index];
	int i = pq_index, parent;
	while(i > 0)
	{
		parent	=	i >> 1;
		if( pq->cmp( pq->heap[parent], obj ) < 0 )
		{
			pq->heap[i]	=	pq->heap[parent];
			pq->setindex(pq->heap[parent], i);	
			i	=	parent;
		}
		else	break;
}
	pq->heap[i]	=	obj;
	pq->setindex(obj, i);
	return i;
}
void pqueue_downheap(pqueue* pq, int pq_index)
{
	void *obj	=	pq->heap[pq_index];	
	int i	=	pq_index, child;
	while(i < pq->numelem)
	{
		child	=	i << 1;
		if(	pq->cmp( pq->heap[child], pq->heap[child+1]) < 0 )  child++;
		if( pq->cmp(	pq->heap[child], obj) >  0)
		{
			pq->heap[i]	=	pq->heap[child];
			pq->setindex(pq->heap[child], i);
			i	=	child;
		}
		else break;
}
	pq->heap[i]	=	obj;
	pq->setindex(obj, i);
}
void*  pqueue_get(pqueue * pq, int pq_index)
{
	return pq->heap[pq_index];
}
int pqueue_size(pqueue* pq)
{
	return pq->numelem;
}
int pqueue_insert( pqueue * pq, void *obj )
{
	pq->heap[pq->numelem] = obj;
	pqueue_upheap(pq, pq->numelem);	
	pq->numelem ++;
	return 0;
}
void pqueue_remove( pqueue * pq, int	pq_index)
{
	void *obj	=	pq->heap[pq_index];
	pq->setindex(obj,0);
	pq->heap[pq_index]	=	pq->heap[--pq->numelem];
	pq->setindex(pq->heap[pq_index],pq_index);
	int i	=	pqueue_upheap(pq, pq_index);
	pqueue_downheap(pq, i);
}
//////////////
typedef struct pelem_struct
{
	int  priority;
	int	 pq_index;
	void	 *obj;
}pelem;
void setindex(void *elem,	int pq_index)
{	
	((pelem*)elem)->pq_index = pq_index;
}
int cmp(void * pelem1, void * pelem2)
{
	return ((pelem*)pelem1)->priority - ((pelem*)pelem2)->priority;	
}
void print(pqueue* pq)
{
	int num	=	pqueue_size(pq);
	if(!num) {
		printf("pqueue is none\n");
		return;
	}
	int i;
	for(i = 0; i < num; i++)
		printf("index:%d, priority:%d\n",i,((pelem*)(pqueue_get(pq,i)))->priority);
}
void main()
{
	pqueue * pq = pqueue_new(16, setindex, cmp);
	if(pq == NULL) printf("cannot create pqueue\n");
	pelem	p1,p2,p3;
	p1.priority = 10;
	p2.priority	= 5;
	p3.priority	= 6;
	pqueue_insert(pq, &p1);
	print(pq);
	pqueue_insert(pq, &p2);
	pqueue_insert(pq, &p3);
	print(pq);
	pqueue_remove(pq, p2.pq_index);
	print(pq);
}
一个C优先级队列实现的更多相关文章
- 实现一个优先级队列,每次pop 返回优先级最高的元素
		demo1 实现一个按优先级排序的队列, 并且在这个队列上面每次 pop 操作总是返回优先级最高的那个元素 import heapq class PriorityQueue: def __init__ ... 
- d-ary heap实现一个快速的优先级队列(C#)
		d-ary heap简介: d-ary heap 是泛化版本的binary heap(d=2),d-ary heap每个非叶子节点最多有d个孩子结点. d-ary heap拥有如下属性: 类似comp ... 
- [PY3]——实现一个优先级队列
		import heapq class PriorityQueue: def __init__(self): self._queue=[] self._index=0 def push(self,ite ... 
- Python之实现一个优先级队列
		问题 怎样实现一个按优先级排序的队列? 并且在这个队列上面每次 pop 操作总是返回优先级最高的那个元素 解决方案 下面的类利用 heapq 模块实现了一个简单的优先级队列: import heapq ... 
- 体验Rabbitmq强大的【优先级队列】之轻松面对现实业务场景
		说到队列的话,大家一定不会陌生,但是扯到优先级队列的话,还是有一部分同学是不清楚的,可能是不知道怎么去实现吧,其实呢,,,这东西已 经烂大街了...很简单,用“堆”去实现的,在我们系统中有一个订单催付 ... 
- 如何基于RabbitMQ实现优先级队列
		概述 由于种种原因,RabbitMQ到目前为止,官方还没有实现优先级队列,只实现了Consumer的优先级处理. 但是,迫于种种原因,应用层面上又需要优先级队列,因此需求来了:如何为RabbitMQ加 ... 
- ACM/ICPC 之 优先级队列+设置IO缓存区(TSH OJ-Schedule(任务调度))
		一个裸的优先级队列(最大堆)题,但也有其他普通队列的做法.这道题我做了两天,结果发现是输入输出太过频繁,一直只能A掉55%的数据,其他都是TLE,如果将输入输出的数据放入缓存区,然后满区输出,可以将I ... 
- 【python cookbook】【数据结构与算法】5.实现优先级队列
		问题:要实现一个队列,它能够以给定的优先级对元素排序,且每次pop操作时都会返回优先级最高的那个元素: 解决方案:采用heapq模块实现一个简单的优先级队列 # example.py # # Exam ... 
- POJ 2227 The Wedding Juicer (优先级队列+bfs+dfs)
		思路描述来自:http://hi.baidu.com/perfectcai_/item/701f2efa460cedcb0dd1c820也可以参考黑书P89的积水. 题意:Farmer John有一个 ... 
随机推荐
- Java实现Avl树
			Avl树即左右子树的深度[高度]相差不可超过1,所以在插入key的时候,就会出现需要旋转[更改根节点]的操作 下面是源代码: /* the define of avltree's node */ cl ... 
- 前端各种mate积累
			<!DOCTYPE html> H5标准声明,使用 HTML5 doctype,不区分大小写 <head lang=”en”> 标准的 lang 属性写法 <meta c ... 
- 11.2,nginx负载均衡实验
			Nginx负载均衡概述 Web服务器,直接面向用户,往往要承载大量并发请求,单台服务器难以负荷,我使用多台WEB服务器组成集群,前端使用Nginx负载均衡,将请求分散的打到我们的后端服务器集群中,实现 ... 
- laravel5.5事件系统
			目录 1 注册事件和监听器 2 定义事件 3 定义监听器 4 分发事件 更多使用方法 1. 可以手动注册事件 2. 事件监听器中调用队列 3.事件订阅者 1 注册事件和监听器 1.修改EventSer ... 
- wget 下载页面下所有文件
			先介绍几个参数:-c 断点续传(备注:使用断点续传要求服务器支持断点续传),-r 递归下载(目录下的所有文件,包括子目录),-np 递归下载不搜索上层目录,-k 把绝对链接转为相对链接,这样下载之后的 ... 
- Python全栈工程师(元组、字典)
			ParisGabriel 感谢 大家的支持 你们的阅读评价就是我最好的更新动力 我会坚持吧排版做的越来越好 每天坚持 一天一篇 点个订阅吧 灰常感谢 当个死粉也阔以 ... 
- web知识清单
			声名随笔中的实例链接到另一个博客是我本人的另一个博客号 模块一:HTML 1.html是什么: hyperText markup language超文本标记语言 超文本:比文本更丰富的内容 所有的浏览 ... 
- hnust 分蛋糕
			问题 B: 分蛋糕 时间限制: 1 Sec 内存限制: 128 MB提交: 2430 解决: 966[提交][状态][讨论版] 题目描述 今天是DK生日,由于DK的朋友很多,所以DK在蛋糕店定制了 ... 
- zedboard zynq 学习 sobel 边缘检测 IP核 制作   根据 文档 Xapp890
			官方文档http://www.xilinx.com/support/documentation/application_notes/xapp890-zynq-sobel-vivado-hls.pdf ... 
- 软工实践Alpha冲刺(5/10)
			队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 已经解决登录注册等基本功能的界面. 完成了主界面的基本布局 ... 
