Heap.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
#include<malloc.h>
#include<assert.h>
typedef int HPDataType;
typedef struct Heap
{
HPDataType* _a;
size_t _size;
size_t _capacity;
}Heap;
void HeapCreat(Heap* hp, HPDataType* a, int n); //堆的初始化
void HeapDestory(Heap* hp); //堆的清空
void HeapPush(Heap* hp, HPDataType x);//入堆
void HeapPop(Heap* hp); //出堆
HPDataType HeapTop(Heap* hp); //
Heap *swap(HPDataType *a, HPDataType* b);//交换
int HeapSize(Heap* hp); //堆的大小
int HeapEmpty(Heap* hp);//判断堆是否为空
void AdjustUp(HPDataType *a, int n, int child);//向上调整
void AdjustDown(HPDataType *a, int n, int root);//向下调整
// 堆排序 void HeapSort(int* a, int n);
void HeapPrint(Heap* hp);//打印
Heap.c
//建立大堆
void HeapCreat(Heap* hp, HPDataType* a, int n) //堆的初始化
{
hp->_capacity = hp->_size = n;
hp->_a = (HPDataType*)malloc(sizeof(HPDataType)*n);
memcpy(hp->_a,a,sizeof(HPDataType)*n);//把a数组中的数据拷贝过来
for (int i = (n - 2) / 2; i >= 0; --i)
{
//向下调整
AdjustDown(hp->_a, hp->_size,i);
}
}
void HeapDestory(Heap* hp) //堆的清空
{
free(hp->_a);
hp->_capacity = 0;
hp->_size = 0;
HeapEmpty(hp);
}
void HeapPush(Heap* hp, HPDataType x)//入堆
{
if (hp->_size == hp->_capacity)
{
hp->_capacity = hp->_capacity*2;
hp->_a = realloc(hp->_a, sizeof(HPDataType)*hp->_capacity);
}
hp->_a[hp->_size++] = x;
//向上调整
AdjustUp(hp->_a, hp->_size, hp->_size - 1);
}
Heap *swap(HPDataType *x, HPDataType *y)
{
HPDataType c = *y;
*y= *x;
*x = c;
}
void HeapPop(Heap* hp)//出堆
{
assert(hp);
swap(&hp->_a[0],&hp->_a[hp->_size - 1]);
hp->_size--;
AdjustDown(hp->_a, hp->_size, 0);//向下调整
}
HPDataType HeapTop(Heap* hp) //堆顶
{
if (hp->_size == 0)
{
return NULL;
}
else
{
printf("堆顶为%d\n", hp->_a[0]);
}
return hp->_a[0];
}
int HeapSize(Heap* hp)//堆的大小
{
assert(hp);
printf("堆的大小%d\n", hp->_size);
return hp->_size;
}
HPDataType HeapEmpty(Heap* hp)//判断堆是否为空
{
if (hp->_size == 0)
{
printf("堆为空\n");
return 1;
}
else
{
printf("堆不为空\n");
return 0;
}
}
void AdjustUp(HPDataType *a, int n, 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 AdjustDown(HPDataType *a, int n, int root)//向下调整
{
int child = root * 2 + 1;
while (child<n)
{
if (a[child] < a[child + 1]&&(child+1)<n)
{
child++;
}
if (a[child]>a[root])
{
swap(&a[child], &a[root]);
root = child;
child = 2 * root + 1;
}
else
break;
}
}
void HeapPrint(Heap* hp)//打印
{
for (int i = 0; i < hp->_size; i++)
{
printf("%d ", hp->_a[i]);
}printf("\n");
}
Test.c
#include"Heap.h"
void sort(int*brr, int n)//堆排序
{
//建立一个大堆
for (int i = (n - 2) / 2; i >= 0; --i)
{
AdjustDown(brr, n,i);
}
int end =n - 1;
while (end)//每次将堆顶的值放在最后,再继续向下调整,继续互换
{
swap(&brr[0], &brr[end]);
AdjustDown(brr, end, 0);
end--;
}
HeapPrint(&brr);
}
int main()
{
Heap hp; HPDataType a[10] = { 11,1, 2, 3, 4, 5, 6, 7, 8, 9 };
HeapCreat(&hp,&a, 10); //堆的初始化
HeapPrint(&hp);
HeapPush(&hp,30);//入堆
HeapPrint(&hp);
HeapPush(&hp, 15);//入堆
HeapPrint(&hp);
HeapPop(&hp);//出堆
HeapPrint(&hp);
//HeapSize(&hp); //堆的大小
// HeapTop(&hp); //
// HeapEmpty(&hp);//判断堆是否为空
//HeapDestory(&hp); //堆的清空*/
int brr[5] = { 5, 8, 3, 1, 4 };
sort(&brr, 5);
}
学了这么长时间的数据结构还是不太懂怎们传参