C语言优先队列作用,优先队列--C语言实现

优先队列--C语言实现

向乔布斯致敬

世界的今天因他而改变!

世界的今天因他而多彩!

JOBS

优先队列--C语言实现

/* binomial.h */

#ifndef _BINOMIAL_H_

#define _BINOMIAL_H_

typedef long element_type;

#define INFINITY (30000L)

#define MAX_TREES (14)

#define CAPACITY (16383)

struct bin_node;

typedef struct bin_node *bin_tree;

struct collection;

typedef struct collection *bin_queue;

#endif

/* end */

/**********************************************************************************************************/

/* binomial.c

* 实现了优先队列插入、合并、删除最小单元等操作

* nizqsut@http://doc.xuehai.net

*/

#include

#include

#include "binomial.h"

#include "fatal.h"

typedef struct bin_node *pos;

struct bin_node

{

element_type element;

pos left_child;

pos next_sibling;

};

struct collection

{

int cur_size;

bin_tree trees[ MAX_TREES ];

};

/*

合并两个优先队列,通过后h1返回新的优先队列指针

*/

static bin_queue

bq_merge( bin_queue h1, bin_queue h2 );

static int

is_empty( bin_queue h )

{

return h->cur_size == 0;

}

static int

is_full( bin_queue h )

{

return h->cur_size == CAPACITY;

}

/*

初始化一个空优先队列

返回空优先队列的指针

*/

static bin_queue

initialize( void )

{

bin_queue h;

int i;

h = malloc( sizeof( struct collection ) );

if ( h == NULL )

Error("Out of space!");

h->cur_size = 0;

for ( i = 0; i < MAX_TREES; i++ ){

h->trees[ i ] = NULL;

}

return h;

}

/* 删除一颗二项树 */

static void

destroy_tree( bin_tree t )

{

if ( t != NULL ){

destroy_tree( t->left_child );

destroy_tree( t->next_sibling );

free( t );

}

}

/*

static void

destroy_bq( bin_queue h )

{

int i;

for ( i = 0; i < MAX_TREES; i++ ){

destroy_tree( h->trees[ i ] );

h->trees[ i ] = NULL;

}

}

*/

/*

清除优先队列中所有的节点

*/

static bin_queue

make_empty( bin_queue h )

{

int i;

/*destroy_bq( h );*/

for ( i = 0; i < MAX_TREES; i++ ){

destroy_tree( h->trees[ i ] );

h->trees[ i ] = NULL;

}

h->cur_size = 0;

return h;

}

/*

插入一个节点

*/

static bin_queue

insert( element_type item, bin_queue h )

{

bin_tree new_node;

bin_queue bq_one_item;

new_node = malloc( sizeof( struct bin_node ) );

if ( new_node == NULL )

Error("Out of space!");

new_node->left_child = new_node->next_sibling = NULL;

new_node->element = item;

/* 先构造一个新优先队列放置新节点,然后与原来的优先队列合并 */

bq_one_item = initialize( );

bq_one_item->cur_size = 1;

bq_one_item->trees[ 0 ] = new_no

de;

return bq_merge( h, bq_one_item );

}

/*

删除优先队列中最小值的节点

*/

static element_type

delete_min( bin_queue h )

{

int i, j;

int min_tre

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言中的优先队列是一种特殊的队列数据结构,其中每个元素都有一个与之关联的优先级。优先级高的元素先被处理,而优先级相同的元素按照它们被插入的顺序进行处理。 在C语言中,可以使用以下几种方式来实现优先队列: 1. 数组实现:使用数组来存储元素,并根据元素的优先级进行排序。插入元素时,需要按照优先级找到合适的位置进行插入;删除元素时,直接删除数组中的第一个元素即可。这种实现方式简单直观,但插入和删除操作的时间复杂度较高。 2. 堆实现:使用堆这种数据结构来实现优先队列。堆是一种完全二叉树,满足堆序性质:对于每个节点i,其父节点的值小于等于节点i的值。在C语言中,可以使用数组来表示堆。插入元素时,将元素插入到堆的末尾,并通过上浮操作将其调整到合适的位置;删除元素时,将堆顶元素与最后一个元素交换,并通过下沉操作将其调整到合适的位置。这种实现方式的插入和删除操作的时间复杂度为O(log n),效率较高。 3. 链表实现:使用链表来存储元素,并根据元素的优先级进行排序。插入元素时,需要按照优先级找到合适的位置进行插入;删除元素时,直接删除链表中的第一个元素即可。这种实现方式相对于数组实现来说,插入和删除操作的时间复杂度较低,但查找操作的时间复杂度较高。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值