日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

优先级队列 c语言,使用最小堆使用优先级队列(c语言版本)

發布時間:2024/2/28 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 优先级队列 c语言,使用最小堆使用优先级队列(c语言版本) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

下面的例子來自Weiss的《數據結構與算法分析:c語言描述》,自己親自敲了一遍,跑了個demo,并將結果記錄下來。

binheap.h的頭文件聲明

//description: 使最小堆實現優先級隊列

//date: 2019-03-15

#ifndef __BINHEAP_H__

#define __BINHEAP_H__

typedef int ElementType;

struct HeapStruct {

int Capacity; //最大容量

int Size; //當前大小

ElementType *Elements; //元素數組

};

typedef struct HeapStruct *PriorityQueue;

PriorityQueue Initialize(int MaxElements);

void Destroy(PriorityQueue H);

void MakeEmpty(PriorityQueue H);

int IsEmpty(PriorityQueue H);

int IsFull(PriorityQueue H);

void Insert(ElementType X, PriorityQueue H);

ElementType DeleteMin(PriorityQueue H);

ElementType FindMin(PriorityQueue H);

#endif

binheap.c源碼文件

#include "binheap.h"

#include #include #define MinData (-32767)

#define MinPQSize (10)

PriorityQueue

Initialize(int MaxElements){

PriorityQueue H;

if(MaxElements < MinPQSize){

printf("Priority queue size is too small");

exit(-1);

}

H = malloc(sizeof(struct HeapStruct));

if(H == NULL){

printf("failed to alloc memory space for HeapStruct");

exit(-1);

}

/* allocate the array plus one extra for sentinel */

H->Elements = malloc( (MaxElements+1) * sizeof(ElementType));

if(H->Elements == NULL){

printf("failed to allocate memory for Elements array");

exit(-1);

}

H->Capacity = MaxElements;

H->Size = 0;

H->Elements[0] = MinData; //此處設置哨兵

return H;

}

void

Destroy(PriorityQueue H){

free(H->Elements);

free(H);

}

void

MakeEmpty(PriorityQueue H){

H->Size = 0;

}

void

Insert(ElementType X, PriorityQueue H){

int i;

if(IsFull(H)){

printf("Priority queue is full");

return;

}

//從尾部向頭部檢查

for(i=++H->Size; H->Elements[i/2]>X; i/=2){

H->Elements[i] = H->Elements[i/2];

}

H->Elements[i] = X;

}

ElementType

DeleteMin(PriorityQueue H){

int i,Child;

ElementType MinElement, LastElement;

if(IsEmpty(H)){

printf("FATAL: Priority queue is empty");

return H->Elements[0];

}

MinElement = H->Elements[1];

LastElement = H->Elements[H->Size--];

for(i=1; i * 2 <= H->Size; i=Child){

/*Find smaller child*/

Child = i * 2;

if(Child != H->Size && H->Elements[Child+1] < H->Elements[Child])

Child++;

/*Percolate one level */

//此時最后一個元素已經在堆頂部了,頭部與最后一個元素交換過了

if(LastElement > H->Elements[Child])

H->Elements[i] = H->Elements[Child];

else

break;

}

H->Elements[i] = LastElement;

return MinElement;

}

ElementType

FindMin(PriorityQueue H){

if(!IsEmpty(H))

return H->Elements[1];

printf("FATAL: Priority queue is Empty");

return H->Elements[0];

}

int

IsEmpty(PriorityQueue H){

return H->Size == 0;

}

int

IsFull(PriorityQueue H){

return H->Size == H->Capacity;

}

//=================================

int main(){

int i, NUM=30;

PriorityQueue pq = Initialize(NUM);

for(i=0; i

下面是運行圖示:

總結

以上是生活随笔為你收集整理的优先级队列 c语言,使用最小堆使用优先级队列(c语言版本)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。