堆的实现--数据结构

目录

主程序(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;
}
相关推荐
无忧.芙桃8 小时前
C++语言原理与实践(二十三):哈希表的实现
c++·链表·哈希算法·散列表·哈希表
郝学胜-神的一滴10 小时前
Effective Python 条款36:优雅拼装迭代器与生成器
开发语言·数据结构·人工智能·python·numpy·scikit-learn
AvocadoZeroOneOI11 小时前
B4500 [GESP202603 三级] 凯撒密码 详细解题报告
c++·算法
cjz389913 小时前
双指针技巧:覆盖式双指针(原地覆盖)
算法
console.log('npc')15 小时前
Python 学习教程 · 第4课:数据结构
数据结构·python·学习
橘子1316 小时前
【高级数据库】关系代数基本概念
算法
电科_银尘1 天前
【大模型算法与工程 15】手写Self-Attention:Q、K、V与缩放点积
算法
郝学胜-神的一滴1 天前
Effective STL 条款03:容器中的对象复制要廉价且正确
开发语言·数据结构·c++·算法·rpc·stl
秋天的怀念_David1 天前
算法一千零一夜 · 第三十三夜 · 跳表
java·程序人生·算法
戊本菌1 天前
进程(三)进程优先级:谁先上 CPU
linux·c语言·硬件架构