/*-------------------------------------------------------------------------------------
* Project: Queue.h
* Name: zwp
* Date: 2014/3
*-------------------------------------------------------------------------------------*/
#ifndef QUEUE_H_
#define QUEUE_H_
struct HeapStruct;
typedef struct HeapStruct *PriorityQueue;
typedef int ElementType;
/*
** 优先队列初始化
*/
PriorityQueue Initialize(int MaxElement);
/*
** 销毁优先队列
*/
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 Travse(PriorityQueue H);
#endif
/*-------------------------------------------------------------------------------
* Project: Queue.cpp
* Name: zwp
* Date: 2014/3
*-------------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "Queue.h"
#define MinPQSize 10
#define MinData 0
struct HeapStruct
{
int Capacity;
int Size;
ElementType *Elements;
};
/*
** 清空队列
*/
void MakeEmpty(PriorityQueue H)
{
H->Size = 0;
free(H->Elements);
H->Capacity = 0;
}
/*
** 销毁优先队列
*/
void Destroy(PriorityQueue H)
{
H->Size = 0;
free(H->Elements);
H->Capacity = 0;
}
/*
** 遍历优先队列
*/
void Travse(PriorityQueue H)
{
int i ;
for(i = 1; i < H->Size; ++ i)
printf("%d \n", H->Elements[i]);
printf("\n");
}
/*
** 优先队列初始化
*/
PriorityQueue Initialize(int MaxElement)
{
PriorityQueue H;
if(MaxElement < MinPQSize)
printf("The Prioity queue size is too small.....\n");
H = (HeapStruct*)malloc(sizeof(struct HeapStruct));
if(H == NULL)
printf("Out of space...\n");
/* Allocate array plus one extra for sentinal */
H->Elements = (ElementType*)malloc((MaxElement + 1) * sizeof(ElementType));
if(H->Elements == NULL)
printf("Out of space...\n");
H->Capacity = MaxElement;
H->Size = 0;
H->Elements[0] = MinData;
return H;
}
/*
** 插入元素
*/
void Insert(ElementType X, PriorityQueue H)
{
int i;
if(IsFull(H))
{
printf("Priority queue is full....\n");
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("Priority queue is empty....\n");
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 Node probely have two */
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))
{
printf("The Priority is NULL ...\n");
}
return H->Elements[1];
}
/*
** 优先队列是否为空
*/
int IsEmpty(PriorityQueue H)
{
return H->Size == 0 ? 1 : 0;
}
/*
** 优先队列是否满
*/
int IsFull(PriorityQueue H)
{
return (H->Capacity == H->Size) ? 1 : 0;
}
/*----------------------------------------------------------------------------------
* Project: Main.cpp
* Name: zwp
* Date: 2014/3
*---------------------------------------------------------------------------------*/
#include "Queue.h"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
PriorityQueue H = Initialize(20);
int i = 0;
for(i = 0; i < 20; ++ i)
Insert(i+1, H);
DeleteMin(H);
Travse(H);
system("pause");
return 0;
}