/* Required * Project: Heap. cpp * Name: zwp * Date: 2014/3 * bytes */# ifndef HEAP_H _ # define TRUE 1 # define FALSE 0 struct HeapStruct; typedef struct HeapStruct * PriorityQueue; typedef int ElementType;/*** Initialize priority queue */PriorityQueue Initialize (int MaxElements);/*** Destroy queue */void Destroy (PriorityQueue H ); /*** create heap */void MakeEmpty (PriorityQueue H);/***** Insert element */void Insert (ElementType x, PriorityQueue H ); /*** Delete the smallest element */ElementType DeleteMin (PriorityQueue H);/*** find the smallest element */ElementType FindMin (PriorityQueue H ); /*** determine whether the queue is empty */int IsEmpty (PriorityQueue H);/*** determine whether the queue is full */int IsFull (PriorityQueue H ); /*** traverse heap */void Tranver (PriorityQueue H); # endif
/* Role * Project: Heap. cpp * Name: zwp * Date: 2014.3 * role */# include
# Include
# Include "Heap. h "/*** Place in implementaton */struct HeapStruct {int Capacity; int size; ElementType * Elements ;};# define MinData 0 # define MinPQSize 10int MinElement; /*** Initialize the queue */PriorityQueue Initialize (int maxElements) {PriorityQueue H; if (maxElements <MinPQSize) printf ("Priority queue size is too small... \ n "); H = (HeapStruct *) malloc (sizeof (struct HeapStruct); if (H = NULL) printf (" Out of space .... \ n "); H-> Elements = (ElementType *) malloc (maxElements + 1) * sizeof (ElementType); if (H-> Elements = NULL) printf ("Out of space... \ n "); H-> Capacity = maxElements; H-> size = 0; H-> Elements [0] = MinData; return H ;} /*** clear queue */void MakeEmpty (PriorityQueue H) {H-> size = 0;}/*** Destroy queue */void Destroy (PriorityQueue H) {free (H-> Elements); free (H);}/*** Insert element */void Insert (ElementType X, PriorityQueue H) {int I; if (IsFull (H )){ Printf ("Priority queue is full .... \ n "); return;}/** left = I/2 * right = I/2 + 1 */for (I = ++ H-> size; h-> Elements [I/2]> X; I/= 2) H-> Elements [I] = H-> Elements [I/2]; h-> Elements [I] = X;}/*** Delete the smallest element */ElementType DeleteMin (PriorityQueue H) {int I, Child; ElementType DeleteMin, LastElement; if (IsEmpty (H) {printf ("Priority queue is empty .... \ n "); return H-> Elements [0];}/*** MinElement = root ** LastELement = Last */MinElement = H-> Elements [I]; LastElement = H-> Elements [H-> size-1]; for (I = 1; I * 2 <= H-> size; I = 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]; elsebreak ;} h-> Elements [I] = LastElement; return MinElement;}/*** find the minimum element */ElementType FindMin (PriorityQueue H) {return H-> Elements [0];} /*** determine whether it is null */int IsEmpty (PriorityQueue H) {if (H-> size = 0) return TRUE; elsereturn FALSE ;} /*** determine whether the queue is full */int IsF Ull (PriorityQueue H) {return (H-> size = H-> Capacity )? TRUE: FALSE;}/*** traverse heap */void Tranver (PriorityQueue H) {for (int index = 0; index <H-> size; ++ index) printf ("% d", H-> Elements [index]);}
/*----------------------------------------------------------------------- * Project: Main.cpp * Name: zwp * Date: 2014/3 *---------------------------------------------------------------------------*/#include "Heap.h"#include
#include
int main(int argc, char* argv[]){PriorityQueue H = Initialize(100);for(int index = 1; index < 98; ++ index)Insert(index+1, H);Insert(999, H);Insert(998, H);printf("The Min = %d\n\n", FindMin(H));Tranver(H);system("pause");return 0;}