1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 
  5 typedef int ElementType;
  6 
  7 struct  HeapStruct;
  8 typedef struct HeapStruct *PriorityQueue;
  9 
 10 PriorityQueue Initialize( int MaxElements);
 11 
 12 void DestoryQueue(PriorityQueue H);
 13 void MakeEmpty(PriorityQueue H);
 14 void Insert(ElementType X, PriorityQueue H);
 15 
 16 ElementType DeleteMin(PriorityQueue H);
 17 ElementType FindMin(PriorityQueue H);
 18 
 19 int IsEmpty(PriorityQueue H);
 20 int ISFull(PriorityQueue H);
 21 
 22 
 23 #define MinPQSize 10
 24 #define MinData -32767
 25 
 26 struct HeapStruct
 27 {
 28         int Capacity;
 29         int Size;
 30         ElementType *Elements;
 31 };
 32 
 33 PriorityQueue 
 34 Initialize( int MaxElements)
 35 {
 36         PriorityQueue H;
 37 
 38         if(MaxElements < MinPQSize)
 39                 printf("small");
 40 
 41         H = malloc(sizeof(struct HeapStruct));
 42         
 43         if( H == NULL)
 44                 printf("ERROR");
 45                 
 46         H->Elements = malloc(sizeof(ElementType) *(MaxElements +1));
 47         
 48         if( H->Elements    == NULL)
 49                 printf("OUT");
 50         H->Capacity = MaxElements;
 51         H->Size = 0;
 52         H->Elements[0] = MinData;
 53         
 54         return H;
 55 }
 56 
 57 
 58 void
 59 Insert(ElementType X, PriorityQueue H)
 60 {
 61         int i ;
 62         if( ISFull( H ))
 63         {
 64                 printf("Full");
 65                 return;
 66         }
 67 
 68         for( i= ++ H->Size; H->Elements[ i / 2] > X; i/=2)
 69         {
 70                 H->Elements[ i ] = H->Elements[i/2];
 71         }
 72 
 73         H->Elements[ i ] = X;
 74 }
 75 
 76 
 77 ElementType
 78 DeleteMin( PriorityQueue H)
 79 {
 80         int i,Child;
 81         ElementType MinElement,LastElement;
 82 
 83         if( IsEmpty(H))
 84         {        
 85                 printf("EMPTY");
 86                 return H->Elements[0];
 87         }
 88 
 89 
 90         MinElement  = H->Elements[1];
 91         LastElement = H->Elements[ H->Size --];
 92 
 93         for( i = 1; i*2 <= H->Size; i = Child)
 94         {
 95                 Child = i*2;
 96                 if( Child != H->Size && H->Elements[Child+1] < H->Elements[Child])
 97                 {
 98                         Child ++;
 99                 }
100 
101 
102                 if( LastElement > H->Elements[Child])
103                         H->Elements[i] = H->Elements[Child];
104                 else
105                         break;
106             }
107             H->Elements[i] = LastElement;
108             return MinElement;
109 }
110 
111 
112 
113 void 
114 MakeEmpty( PriorityQueue H)
115 {
116         H->Size =0;
117 }
118 
119 
120 ElementType 
121 FindMin(PriorityQueue H)
122 {
123         if(!IsEmpty(H))
124                 return H->Elements[1];
125         printf("ERROR");
126         return H->Elements[0];
127 }
128 
129 
130 int
131 IsEmpty(PriorityQueue H)
132 {
133         return H->Size == 0;
134 }
135 
136 
137 int 
138 ISFull(PriorityQueue H)
139 {
140         return  H->Size == H->Capacity;
141 }
142 
143 
144 
145 int 
146 main()
147 {
148         PriorityQueue H = Initialize( 50 );
149         int arr[] = { 32,21,16,24,31,19,68,65,26,13};
150 
151         int i;
152         for( i =0; i< 10; i++)
153         {
154                 Insert(arr[i], H);
155         }
156 
157         for( i=0 ; i < 10; i++)
158         {
159                 printf("%d\n",DeleteMin( H ));
160         }
161         return 0;
162 }
posted @ 2012-09-16 11:38  风去无痕  阅读(163)  评论(0编辑  收藏  举报