堆的实现C

#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
#include<string.h>

typedef int HPDataType;
//大堆
typedef struct Heap
{
	HPDataType* a;
	int size;
	int capacity;
}HP;

void Swap(int* px, int* py);
void AdjustDown(int* a, int n, int parent);
void AdjustUp(int* a, int child);


//void HeapInit(HP* php);
void HeapInit(HP* php, HPDataType* a, int n);
void HeapDestroy(HP* php);
// 插入x,保持他继续是堆
void HeapPush(HP* php, HPDataType x);
// 删除堆顶数据,删除后保持他继续是堆
void HeapPop(HP* php);
// 获取堆顶的数据,也就是最值
HPDataType HeapTop(HP* php);
bool HeapEmpty(HP* php);
int HeapSize(HP* php);

void HeapPrint(HP* php);
#define _CRT_SECURE_NO_WARNINGS 1
#include"Heap.h"
void Swap(int* a, int* b)
{
	int tmp = *a;
	*a = *b;
	*b = tmp;
}
//建大堆
//向下调整算法
void AdjustDown(int* a, int n, int parent)
{
	//父节点的左右子树都是大堆或者小堆可以直接建堆
	int child = parent * 2 + 1;
	while (child < n)
	{
		//先选出两个孩子中较大的那个
		if (a[child] < a[child + 1] && child + 1 < n)
			child++;
		//孩子与父亲比较,孩子大就交换,孩子小就结束循环
		if (a[child] > a[parent])
		{
			Swap(&a[child], &a[parent]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
			break;
		}

	}
}
//向上调整算法
//向堆中插入一个数据时可以用向上调整算法实现
void AdjustUp(int* a, int child)
{
	int parent = (child - 1) / 2;
	while (child > 0)
	{
		//孩子比父亲大,就交换,更新孩子父亲
		if (a[child] > a[parent])
		{
			Swap(&a[child], &a[parent]);
			child = parent;
			parent = (child - 1) / 2;
		}
		else
		{
			break;
		}
	}
}
//排升序,建大堆,排降序,建小堆
void HeapSort(int* a, int n)
{
	//n为个数
	for (int i = (n - 1 - 1) / 2; i >= 0; i--)
	{
		AdjustDown(a, n, i);
	}//建堆
	//建堆后排序
	int end = n - 1;
	while (end > 0)
	{
		//堆顶和堆底不断交换,然后对除了堆底的其他部分进行重新建堆
		//由于交换只会影响一条分支,所以建堆直接用向下调整即可
		Swap(&a[0], &a[end]);
		AdjustDown(a, end, 0);
		end--;
	}

}
void HeapInit(HP* php, HPDataType* a, int n)
{
	assert(php);
	php->a = (HP*)malloc(sizeof(HPDataType) * n);
	if (php->a == NULL)
	{
		printf("malloc fail\n");
		exit(-1);
	}
	memcpy(php->a, a, sizeof(HPDataType) * n);
	for (int i = (n - 2) / 2; i >= 0; i--)
	{
		AdjustDown(php->a, n, i);
	}
	php->size = n;
	php->capacity = n;
}
void HeapDestroy(HP* php)
{
	assert(php);
	free(php->a);
	php->a = NULL;
	php->size = php->capacity = 0;
}
void HeapPush(HP* php, HPDataType x)
{
	assert(php);
	if (php->size == php->capacity)
	{
		HPDataType* tmp = (HPDataType*)realloc(php->a, sizeof(HPDataType) * php->capacity * 2);
		if (php->a == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		php->capacity *= 2;
	}
	php->size++;
	php->a[php->size - 1] = x;
	AdjustUp(php->a, php->size - 1);
}
//删除堆顶元素
void HeapPop(HP* php)
{
	assert(php);
	assert(!HeapEmpty(php));
	//先将对顶交换到最下面,然后重新向下调整
	Swap(&php->a[0], &php->a[php->size - 1]);
	php->size--;
	AdjustDown(php->a, php->size,0);
}
bool HeapEmpty(HP* php)
{
	assert(php);
	return php->size == 0;
}
HPDataType HeapTop(HP* php)
{
	assert(php);
	assert(!HeapEmpty(&php));
	return php->a[0];
}
int HeapSize(HP* php)
{
	assert(php);
	return php->size;
}

void HeapPrint(HP* php)
{
	for (int i = 0; i < php->size; i++)
		printf("%d ", php->a[i]);
	printf("\n");
}
#define _CRT_SECURE_NO_WARNINGS 1
#include"Heap.h"

int main()
{
	int a[] = { 27, 37, 28, 18, 19, 34, 65, 4, 2, 5, 7, 9, 3, 0, 25, 49, 15 };
	HP hp;
	HeapInit(&hp, a, sizeof(a) / sizeof(int));

	HeapPrint(&hp);
	HeapPush(&hp, 100);
	HeapPrint(&hp);
	HeapPop(&hp);
	HeapPrint(&hp);

	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是C语言实现串的分配的代码示例: ```c // 串结构 typedef struct HString { char *ch; // 指向字符串所在空间首地址 int length; // 记录字符串长度 } HString; // 初始化串 void InitString(HString *S) { S->ch = NULL; S->length = 0; } // 生成一个值等于串常量T的串S void StrAssign(HString *S, char *T) { int len = strlen(T); if (len == 0) { S->ch = NULL; S->length = 0; } else { S->ch = (char *)malloc(sizeof(char) * len); assert(S->ch != NULL); for (int i = 0; i < len; ++i) { S->ch[i] = T[i]; } S->length = len; } } // 销毁串 void DestroyString(HString *S) { if (S->ch != NULL) { free(S->ch); S->ch = NULL; } S->length = 0; } // 复制串 void StrCopy(HString *S, HString *T) { int len = T->length; if (S->ch != NULL) { free(S->ch); } S->ch = (char *)malloc(sizeof(char) * len); assert(S->ch != NULL); for (int i = 0; i < len; ++i) { S->ch[i] = T->ch[i]; } S->length = len; } // 串比较 int StrCompare(HString *S, HString *T) { for (int i = 0; i < S->length && i < T->length; ++i) { if (S->ch[i] != T->ch[i]) { return S->ch[i] - T->ch[i]; } } return S->length - T->length; } // 求子串 void SubString(HString *Sub, HString *S, int pos, int len) { if (pos < 1 || pos > S->length || len < 0 || pos + len - 1 > S->length) { printf("Error: invalid position or length.\n"); exit(1); } if (Sub->ch != NULL) { free(Sub->ch); } if (len == 0) { Sub->ch = NULL; Sub->length = 0; } else { Sub->ch = (char *)malloc(sizeof(char) * len); assert(Sub->ch != NULL); for (int i = 0; i < len; ++i) { Sub->ch[i] = S->ch[pos + i - 1]; } Sub->length = len; } } // 串插入 void StrInsert(HString *S, int pos, HString *T) { if (pos < 1 || pos > S->length + 1) { printf("Error: invalid position.\n"); exit(1); } int len = T->length; S->ch = (char *)realloc(S->ch, sizeof(char) * (S->length + len)); assert(S->ch != NULL); for (int i = S->length - 1; i >= pos - 1; --i) { S->ch[i + len] = S->ch[i]; } for (int i = 0; i < len; ++i) { S->ch[pos + i - 1] = T->ch[i]; } S->length += len; } // 串删除 void StrDelete(HString *S, int pos, int len) { if (pos < 1 || pos > S->length || len < 0 || pos + len - 1 > S->length) { printf("Error: invalid position or length.\n"); exit(1); } for (int i = pos - 1; i < S->length - len; ++i) { S->ch[i] = S->ch[i + len]; } S->length -= len; S->ch = (char *)realloc(S->ch, sizeof(char) * S->length); assert(S->ch != NULL); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值