首页 > 学院 > 开发设计 > 正文

优先队列(缺)

2019-11-06 09:22:11
字体:
来源:转载
供稿:网友
#include <stdio.h>typedef int ElementType;struct HeapStruct;typedef struct HeapStruct *PRiorityQueue;PriorityQueue Initialize( int MaxElements );void Destroy( PriorityQueue H );void MakeEmpty( PriorityQueue H );void Insert( ElementType X, PriorityQueue H );ElementType DeleteMin( PriorityQueue H );ElementType FindMin( PriorityQueue H);int IsEmpty( PriorityQueue H );int IsFull( PriorityQueue H );struct HeapStruct{ int Capacity; int Size; ElementType *Elements;};int main(){ return 0;}PriorityQueueInitialize( int MaxElements){ PriorityQueue H; if( MaxElements < MinPQSize ) Error( "Priority queue size is too small"); H = malloc( sizeof( struct HeapStruct ) ); if( H == NULL ) FatalError( "Out of space!!!"); H->Elements = malloc( ( MaxElements + 1 ) * sizeof( ElementType) ); if( H->Elements == NULL ) FatalError( "Out of space!!!"); H->Capacity = MaxElements; H->Size = 0; H->Elements[ 0 ] = MinData; return H;}/* H->Element [0] is a sentinel 哨兵*/void Insert(ElementType X, PriorityQueue H){ int i; if( IsFull( H ) ) { Error("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;}ElementTypeDeleteMin( PriorityQueue H){ int i, child; ElementType MinElement, LastElement; if( IsEmpty( H )) { Error( "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->Capacity[ Child]) Child++; if( LastElement > H->Elements[ Child ]) H->Elements[ i ] = H->Elements[ Child ]; else break; } H->Elements[ i ] = LastElement; return MinElement;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表