一个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有一个 ...
随机推荐
- HyperLedger Fabric 1.4 区块链技术定义(2.1)
区块链技术指使用点对点传输.共识机制.加密算法等技术,保证分布式数据库区块写入链中数据的一致性,达到去中心化和不可篡改的目的. 区块链就是一种特殊的分布式数据库,使用现有的各种成熟的技术, ...
- 零基础学css第二天
内边距与外边距: <!DOCTYPE html> <html> <head> <title></title> <style type= ...
- Hive LanguageManual DDL
hive语法规则LanguageManual DDL SQL DML 和 DDL 数据操作语言 (DML) 和 数据定义语言 (DDL) 一.数据库 增删改都在文档里说得也很明白,不重复造车轮 二.表 ...
- Java 虚拟机结构分析
本博文主要介绍了JVM(Java Virtual Machine)的组成部分以及它们内部的工作机制和原理.需要注意的是,虽然平时我们用的大多是Sun(现已被Oracle收购)JDK提供的JVM,但是J ...
- centso下如何解压RAR文件
tar -xvf rarlinux-3.9.3.tar.gz cd rar make 看见下面这些信息就是安装成功了 mkdir -p /usr/local/bin mkdir -p /usr/l ...
- 【Lowest Common Ancestor of a Binary Search Tree】cpp
题目: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in th ...
- 【Insert Interval】cpp
题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...
- 孤荷凌寒自学python第十一天初识Python的字典类
孤荷凌寒自学python第十一天初识Python的字典类 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) Python的字典其实是一张二维对照表 下面举例说明: 键名Key 姓名 性别 身高 ...
- Ext JS 的一个非常好的学习网站
起飞网 http://www.qeefee.com/zt-extjs 发现一个非常好的学习ExtJS的中文网站
- vmware中三种网络连接方式(复制)
原文来自http://note.youdao.com/share/web/file.html?id=236896997b6ffbaa8e0d92eacd13abbf&type=note 我怕链 ...