堆的实现--数据结构

目录

主程序(test.c)

头文件(heap.h)

调用函数(heap.c)


主程序(test.c)

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS 1

#include "heap.h"

int main()
{
	//大堆
	Heap heap;
	//初始化
	HeapInit(&heap);
	//堆的插入
	HeapPush(&heap, 5);
	HeapPush(&heap, 1);
	HeapPush(&heap, 7);
	HeapPush(&heap, 10);
	HeapPush(&heap, 6);
	HeapPush(&heap, 8);
	HeapPush(&heap, 12);
	//堆的删除
	HeapPop(&heap);
	HeapPush(&heap, 48);

	//取堆顶数据
	HPDataType n = HeapTop(&heap);
	printf("堆顶的数据为:%d\n", n);

	HeapPop(&heap);
	n = HeapTop(&heap);
	printf("堆顶数据为%d\n", n);
	HeapPop(&heap);
	n = HeapTop(&heap);
	printf("堆顶的数据为:%d\n", n);
	HeapPop(&heap);
	HeapPop(&heap);
	HeapPop(&heap);


	printf("堆的数据个数为:%d\n", HeapSize(&heap));

	//判空
	int a = HeapSize(&heap);
	if (a == 0)
	{
		printf("堆为空 \n");
	}
	else
		printf("堆不为空 \n");

	//销毁堆
	HeapDestory(&heap);
	return 0;
}

头文件(heap.h)

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>

typedef int HPDataType;
typedef struct Heap
{
	HPDataType* a;
	int size;
	int capacity;
}Heap;


//堆初始化
void HeapInit(Heap* php);
// 堆的销毁
void HeapDestory(Heap* php);
// 堆的插入
void HeapPush(Heap* php, HPDataType x);
// 堆的删除
void HeapPop(Heap* php);
// 取堆顶的数据
HPDataType HeapTop(Heap* php);
// 堆的数据个数
int HeapSize(Heap* php);
// 堆的判空
int HeapEmpty(Heap* php);
//向下调整
void Adjust_down(HPDataType* a, int father, int n);
//向上调整
void Adjust_up(HPDataType* a, int child);

调用函数(heap.c)

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include "heap.h"


//堆初始化
void HeapInit(Heap* php)
{
	assert(php);

	php->capacity = 4;

	HPDataType* ph = (HPDataType*)malloc(sizeof(HPDataType)* php->capacity );
	if (ph == NULL)
	{
		perror("malloc file:");
		return;
	}
	php->a = ph;
	php->size = 0;
}

// 堆的销毁
void HeapDestory(Heap* php)
{
	assert(php);

	free(php->a);
	php->capacity = php->size = 0;
}

void swap(int* a, int* b)
{
	int tmp = *a;
	*a = *b;
	*b = tmp;
}

//向上调整
void Adjust_up(HPDataType* a, int child)
{
	assert(a);

	int father = (child - 1) / 2;

	while (child > 0)
	{
		if (a[child] > a[father])
		{
			swap(&a[child], &a[father]);
			child = father;
			father = (child - 1) / 2;//当child = 0 时,father = 0;father是整形
		}
		else
		{
			break;
		}
	}
}

//向下调整
void Adjust_down(HPDataType* a, int father,int n)
{
	assert(a);

	int child = father * 2 + 1;

	while (child < n )
	{
		if (child + 1 < n && a[child + 1] > a[child])
		{
			child = child + 1;
		}
		if (a[child] > a[father])
		{
			swap(&a[child], &a[father]);
			father = child;
			child = father * 2 + 1;
		}
		else
		{
			break;
		}

	}
}

// 堆的插入
void HeapPush(Heap* php, HPDataType x)
{
	assert(php);
	//向上调整
	if (php->size == php->capacity)
	{
		php->capacity *= 2;
		HPDataType* new = (HPDataType*)realloc(php->a, sizeof(HPDataType) * php->capacity);
		if (new == NULL)
		{
			perror("realloc file:");
			return;
		}
		php->a = new;
	}
	php->a[php->size] = x;
	php->size++;
	//向上调整
	Adjust_up(php->a, php->size-1);
}

// 堆的删除
void HeapPop(Heap* php)
{
	assert(php);
	if (php->size == 0)
	{
		return;
	}
	//删除数据的逻辑是,先把头和尾交换位置,再向下调整
	swap(&php->a[0], &php->a[php->size - 1]);
	php->size--;

	Adjust_down(php->a, 0, php->size);
}
// 取堆顶的数据
HPDataType HeapTop(Heap* php)
{
	assert(php);
	if (php->size == 0)
	{
		return NULL;
	}
	return php->a[0];
}
// 堆的数据个数
int HeapSize(Heap* php)
{
	assert(php);

	return php->size;
}
// 堆的判空
int HeapEmpty(Heap* php)
{
	assert(php);

	if (php->size > 0)
	{
		return 1;
	}
	else
		return 0;
}
相关推荐
快手技术41 分钟前
快手提出端到端生成式搜索框架 OneSearch,让搜索“一步到位”!
算法
CoovallyAIHub20 小时前
中科大DSAI Lab团队多篇论文入选ICCV 2025,推动三维视觉与泛化感知技术突破
深度学习·算法·计算机视觉
NAGNIP21 小时前
Serverless 架构下的大模型框架落地实践
算法·架构
moonlifesudo21 小时前
半开区间和开区间的两个二分模版
算法
moonlifesudo21 小时前
300:最长递增子序列
算法
CoovallyAIHub1 天前
港大&字节重磅发布DanceGRPO:突破视觉生成RLHF瓶颈,多项任务性能提升超180%!
深度学习·算法·计算机视觉
CoovallyAIHub1 天前
英伟达ViPE重磅发布!解决3D感知难题,SLAM+深度学习完美融合(附带数据集下载地址)
深度学习·算法·计算机视觉
聚客AI2 天前
🙋‍♀️Transformer训练与推理全流程:从输入处理到输出生成
人工智能·算法·llm
大怪v2 天前
前端:人工智能?我也会啊!来个花活,😎😎😎“自动驾驶”整起!
前端·javascript·算法
惯导马工2 天前
【论文导读】ORB-SLAM3:An Accurate Open-Source Library for Visual, Visual-Inertial and
深度学习·算法