刚下班没事干,实现了一个简单的优先级队列

#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优先级队列实现的更多相关文章

  1. 实现一个优先级队列,每次pop 返回优先级最高的元素

    demo1 实现一个按优先级排序的队列, 并且在这个队列上面每次 pop 操作总是返回优先级最高的那个元素 import heapq class PriorityQueue: def __init__ ...

  2. d-ary heap实现一个快速的优先级队列(C#)

    d-ary heap简介: d-ary heap 是泛化版本的binary heap(d=2),d-ary heap每个非叶子节点最多有d个孩子结点. d-ary heap拥有如下属性: 类似comp ...

  3. [PY3]——实现一个优先级队列

    import heapq class PriorityQueue: def __init__(self): self._queue=[] self._index=0 def push(self,ite ...

  4. Python之实现一个优先级队列

    问题 怎样实现一个按优先级排序的队列? 并且在这个队列上面每次 pop 操作总是返回优先级最高的那个元素 解决方案 下面的类利用 heapq 模块实现了一个简单的优先级队列: import heapq ...

  5. 体验Rabbitmq强大的【优先级队列】之轻松面对现实业务场景

    说到队列的话,大家一定不会陌生,但是扯到优先级队列的话,还是有一部分同学是不清楚的,可能是不知道怎么去实现吧,其实呢,,,这东西已 经烂大街了...很简单,用“堆”去实现的,在我们系统中有一个订单催付 ...

  6. 如何基于RabbitMQ实现优先级队列

    概述 由于种种原因,RabbitMQ到目前为止,官方还没有实现优先级队列,只实现了Consumer的优先级处理. 但是,迫于种种原因,应用层面上又需要优先级队列,因此需求来了:如何为RabbitMQ加 ...

  7. ACM/ICPC 之 优先级队列+设置IO缓存区(TSH OJ-Schedule(任务调度))

    一个裸的优先级队列(最大堆)题,但也有其他普通队列的做法.这道题我做了两天,结果发现是输入输出太过频繁,一直只能A掉55%的数据,其他都是TLE,如果将输入输出的数据放入缓存区,然后满区输出,可以将I ...

  8. 【python cookbook】【数据结构与算法】5.实现优先级队列

    问题:要实现一个队列,它能够以给定的优先级对元素排序,且每次pop操作时都会返回优先级最高的那个元素: 解决方案:采用heapq模块实现一个简单的优先级队列 # example.py # # Exam ...

  9. POJ 2227 The Wedding Juicer (优先级队列+bfs+dfs)

    思路描述来自:http://hi.baidu.com/perfectcai_/item/701f2efa460cedcb0dd1c820也可以参考黑书P89的积水. 题意:Farmer John有一个 ...

随机推荐

  1. 17-比赛1 F - 较小元素 Weak in the Middle (set)

    Seg-El has last chance to make the final changes in order to prevent the destruction of Krypton. He ...

  2. python requests第三方库详解

    异常处理:try ... except ...

  3. hadoop集群之Datenode无法启动解决办法

    hadoop集群之Datenode无法启动解决办法 我们在启动hadoop集群的时候,通过jps查看进程,发现namenode RM和Secondary NameNode都有,但datanode没有启 ...

  4. laravel5.5容器

    目录 1. 比较典型的例子就是 cache 缓存 2. 容器顾名思义,其实就是完成存取过程 2.1 绑定过程 简单绑定 绑定单例 绑定实例 绑定初始数据 2.2 解析过程 容器主要是为了实现控制反转, ...

  5. javascript md5 二次加密 和 java md5 二次加密结果不同

    最近研究httpclient post 时遇到了一个问题,很费解. js  md5(str) 和 java md5(str),第一次md5 加密结果一样,(当时忽略了大小写问题,java 大写,js小 ...

  6. Jenkins拾遗--第五篇-git插件填坑

    Jenkins使用过程中,大部分Job的第一项就行从源码库里签出代码.由于git越来越流行,所以,稍微新一些的项目的源码管理都是基于git的.对应的,jenkins的git plugin几乎是大部分j ...

  7. 如何将现有的项目添加到远程的git库里面!

    我们经常都会遇到这样的场景,就是将本地的一个项目同步到网上远程的git库里面.最近也遇到这样的问题,发现网上很少人讲到这个问题,但是这个问题是很多程序员遇到的版本库管理的最早的拦路虎. 我的远程是ht ...

  8. Python 字符串格式化输出方式

    字符串格式化有两种方式:百分号方式.format方式. 其中,百分号方式比较老,而format方式是比较先进的,企图替代古老的方式,目前两者共存. 1.百分号方式 格式:%[(name)][flags ...

  9. Oz 创建CentOS6镜像

    参考 http://linuxblind.blog.51cto.com/7616603/1655550/ http://www.chenshake.com/oz-making-centos-mirro ...

  10. Android记事本06

    昨天: activity的生命周期 今天: activity横竖屏切换的生命周期 遇到的问题: 为了匹配ADK,下载了更旧的版本SDK,布局文件仍然无法显示.