堆的实现--数据结构

目录

主程序(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;
}
相关推荐
superman超哥1 小时前
仓颉语言中元组的使用:深度剖析与工程实践
c语言·开发语言·c++·python·仓颉
LYFlied1 小时前
【每日算法】LeetCode 153. 寻找旋转排序数组中的最小值
数据结构·算法·leetcode·面试·职场和发展
唐装鼠1 小时前
rust自动调用Deref(deepseek)
开发语言·算法·rust
ytttr8732 小时前
MATLAB基于LDA的人脸识别算法实现(ORL数据库)
数据库·算法·matlab
charlie1145141913 小时前
现代嵌入式C++教程:C++98——从C向C++的演化(2)
c语言·开发语言·c++·学习·嵌入式·教程·现代c++
雨季余静3 小时前
c语言 gb2312转utf-8,带码表,直接使用。
c语言·c语言utf8·c语言gb2312·c语言gbk·c语言gb18030·gb2312转utf8·gbk转utf8
2401_890443023 小时前
Linux 基础IO
linux·c语言
jianfeng_zhu4 小时前
整数数组匹配
数据结构·c++·算法
yueqingll4 小时前
032、数据结构之代码时间复杂度和空间复杂度的判断:从入门到实战
数据结构
smj2302_796826524 小时前
解决leetcode第3782题交替删除操作后最后剩下的整数
python·算法·leetcode