一、创建三个文件
二、代码实现
1.Heap.c部分
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <ctype.h>
#include <assert.h>
#include <stdbool.h>
#include"Heap.h"
void TestHeap() //测试用例
{
HP hp;
HeapInit(&hp);
HeapInsert(&hp, 1);
HeapInsert(&hp, 5);
HeapInsert(&hp, 3);
HeapInsert(&hp, 0);
HeapInsert(&hp, 8);
HeapInsert(&hp, 9);
HeapPrint(&hp);
HeapPop(&hp);
HeapPrint(&hp);
HeapPop(&hp);
HeapPrint(&hp);
HeapSize(&hp);
HeapTop(&hp);
HeadDestroy(&hp);
}
int main()
{
TestHeap();
return 0;
}
2.Heap.h部分
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <ctype.h>
#include <assert.h>
#include <stdbool.h>
//1.定义结构体
typedef struct Heap
{
int* data;
size_t size;
size_t capacity;
}HP;
//2.初始化堆函数声明
void HeapInit(HP* php);
//3.回收堆空间函数声明
void HeadDestroy(HP* php);
//4.交换数值函数声明
void Swap(int* pa, int* pb);
//5.向上调整函数声明
void HeapAdjustUp(int* data, size_t child);
//6.插入数值函数声明
void HeapInsert(HP* php, int x);
//7.向下调整函数声明
void HeapAdjustDown(int* data, size_t size, size_t rootpos);
//8.删除数据函数声明
void HeapPop(HP* php);
//9.打印数据函数声明
void HeapPrint(HP* php);
//10.查看堆是否为空函数声明
bool HeapEmpty(HP* php);
//11.查看堆数据量函数声明
void HeapSize(HP* php);
//12.获取顶部数据函数声明
void HeapTop(HP* php);
3.HeapFunc.c部分
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <ctype.h>
#include <assert.h>
#include <stdbool.h>
#include"Heap.h"
//2.初始化堆函数实现
void HeapInit(HP* php)
{
assert(php);
php->data = NULL;
php->size = php->capacity = 0;
}
//3.回收堆空间函数实现
void HeadDestroy(HP* php)
{
assert(php);
free(php->data);
php->data = NULL;
php->capacity = php->size = 0;
}
//4.交换数值函数实现
void Swap(int* pa, int* pb)
{
int tmp = 0;
tmp = *pa;
*pa = *pb;
*pb = tmp;
}
//5.向上调整函数实现
void HeapAdjustUp(int* data, size_t childpos)
{
if (childpos == 0)
{
return;
}
size_t parentpos = (childpos - 1) / 2;
while (childpos > 0)
{
//建大堆
if (data[childpos] > data[parentpos])
{
Swap(&data[childpos], &data[parentpos]);
childpos = parentpos;
parentpos = (childpos - 1) / 2;
}
else
{
break;
}
}
}
//6.插入数值函数实现
void HeapInsert(HP* php, int x)
{
assert(php);
if (php->capacity == php->size)
{
size_t newcapacity = php->capacity == 0 ? 4 : 2 * php->capacity;
int* tmp = realloc(php->data, newcapacity * sizeof(int));
if (tmp == NULL)
{
printf("HeapInset::()%s\n", strerror(errno));
exit(-1);
}
php->data = tmp;
php->capacity = newcapacity;
}
php->data[php->size] = x;
HeapAdjustUp(php->data, php->size);
php->size++;
}
//7.向下调整函数实现
void HeapAdjustDown(int* data, size_t size, size_t rootpos)
{
size_t parentpos = rootpos;
size_t childpos = parentpos * 2 + 1;
while (childpos < size)
{
if (childpos + 1 < size && data[childpos + 1] > data[childpos])
{
childpos++;
}
if (data[childpos] > data[parentpos])
{
Swap(&data[childpos], &data[parentpos]);
parentpos = childpos;
childpos = parentpos * 2 + 1;
}
else
{
break;
}
}
}
//8.删除函数实现
void HeapPop(HP* php)
{
assert(php);
assert(php->size > 0);
php->size--;
Swap(&php->data[0], &php->data[php->size]);
HeapAdjustDown(php->data, php->size, 0);
}
//9.打印数据函数实现
void HeapPrint(HP* php)
{
assert(php);
for (size_t i = 0; i < php->size; i++)
{
printf("%d ", php->data[i]);
}
printf("\n");
}
//10.查看堆是否为空函数实现
bool HeapEmpty(HP* php)
{
assert(php);
return php->size == 0;
}
//11.查看堆数据量函数实现
void HeapSize(HP* php)
{
assert(php);
printf("%d\n", php->size);
}
//12.获取顶部数据函数实现
void HeapTop(HP* php)
{
assert(php);
assert(php->size);
printf("%d\n", php->data[0]);
}