binHeap.h:
#ifndef _BinHeap_H
struct HeapStruct;
typedef struct HeapStruct *PriorityQueue;
typedef int ElementType;
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);
void DecreaseKey(int P, int IncreaseNUm, PriorityQueue H);
void IncreaseKey(int P, int DecreaseNum, PriorityQueue H);
void Delete(int P, PriorityQueue H);
#endif
binHeap.c:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include "binHeap.h"
#define MinPQSize 10
struct HeapStruct
{
int Capacity;
int size;
ElementType *Elements;
};
PriorityQueue Initialize(int MaxElements)
{
PriorityQueue H;
H = malloc(sizeof(struct HeapStruct));
if (MaxElements < MinPQSize)
{
printf("Priority queue size is too small!!!\n");
exit(0);
}
if (H == NULL)
{
printf("Out of space!!!\n");
exit(0);
}
else
{
/*Allocate the array plus one extra for sentinel(哨兵)*/
//用到了“哨兵”模式
H->Elements = malloc((MaxElements + 1) * sizeof(ElementType));
H->Capacity = MaxElements;
H->size = 0