新手上路
- 积分
- 29
- 金钱
- 29
- 注册时间
- 2016-9-10
- 在线时间
- 104 小时
|
本帖最后由 lccpcc 于 2022-6-16 22:12 编辑
- #include <stdlib.h>
- #include <stdbool.h>
- #include <assert.h>
- #include <string.h>
- typedef int HPDateType;
- typedef struct Heap
- {
- HPDateType *a;
- int size;
- int capacity;
- } HP;
- void HeapInit(HP *php, HPDateType *a, int n);
- void HeapDestroy(HP *php);
- void HeapPush(HP *php, HPDateType x);
- void HeapPop(HP *php);
- HPDateType HeapTop(HP *php);
- int HeapSize(HP *php);
- bool HeapEmpty(HP *php);
- void HeapPrint(HP *php);
- void AdjustUp(HP *st, int child);
- void AdjustDown(HP *st, int praent);
- void Swap(int *p1, int *p2);
- void HeapSort(HP *php);
- </div><div yne-bulb-block="code" data-theme="default" style="white-space:pre-wrap;" data-language="c">int main(int argc, char **argv)
- {
- HP st;
- int a[] = {15, 18, 28, 34, 65, 19, 49, 25, 37, 27};
- int n = sizeof(a) / sizeof(a[0]);
- printf("\ntest HeapInit(), HeapPrint():\n");
- HeapInit(&st, a, n);
- HeapPrint(&st);
- printf("\ntest HeapPush(&st, 8):\n");
- HeapPush(&st, 8);
- HeapPrint(&st);
- printf("\ntest HeapPush(&st, 88):\n");
- HeapPush(&st, 88);
- HeapPrint(&st);
- printf("\ntest HeapPop(&st):\n");
- HeapPop(&st);
- HeapPrint(&st);
- printf("\ntest HeapPop(&st):\n");
- HeapPop(&st);
- HeapPrint(&st);
- printf("\n对堆进行排序:\n");
- HeapSort(&st);
- HeapPrint(&st);
- /* 销毁堆 */
- HeapDestroy(&st);
- if(st.a == NULL)
- printf("\n销毁堆成功\n");
- return 0;
- }
- // 交换
- void Swap(int *p1, int *p2)
- {
- int temp = *p1;
- *p1 = *p2;
- *p2 = temp;
- }
- // 向上调整算法
- void AdjustUp(HP *st, int child)
- {
- int parent = (child - 1) / 2;
- while (child > 0)
- {
- if (st->a[child] < st->a[parent])
- {
- Swap(&st->a[child], &st->a[parent]);
- child = parent;
- parent = (child - 1) / 2;
- }
- else
- {
- break;
- }
- }
- }
- // 向下调整算法
- void AdjustDown(HP *st, int praent)
- {
- int child = praent * 2 + 1;
- while (child < st->size)
- {
- if (child + 1 < st->size && st->a[child + 1] < st->a[child])
- {
- child++;
- }
- if (st->a[child] < st->a[praent])
- {
- Swap(&st->a[child], &st->a[praent]);
- praent = child;
- child = praent * 2 + 1;
- }
- else
- {
- break;
- }
- }
- }
- void HeapInit(HP *php, HPDateType *a, int n)
- {
- if (php == NULL)
- return;
- php->a = (HPDateType *)calloc(1, sizeof(HPDateType) * n);
- if (php->a == NULL)
- exit(EXIT_FAILURE);
- memcpy(php->a, a, sizeof(HPDateType) * n);
- php->capacity = n;
- php->size = n;
- int i;
- //建小堆
- for (i = (php->size - 2) / 2; i >= 0; i--)
- AdjustDown(php, i);
- }
- void HeapDestroy(HP *php)
- {
- if (php == NULL)
- return;
- free(php->a);
- php->a = NULL;
- php->size = 0;
- php->capacity = 0;
- }
- //在堆里面插入一个数,并且让它的结构还是一个堆
- void HeapPush(HP *php, HPDateType x)
- {
- if (php == NULL)
- return;
- /* 如果堆的空间满了,就用realloc()重新扩大堆的数组空间的大小 */
- if (php->size == php->capacity)
- {
- HPDateType *temp = (HPDateType *)realloc(php->a, sizeof(HPDateType) * php->capacity * 2);
- if (temp == NULL)
- exit(EXIT_FAILURE);
- php->a = temp;
- php->capacity = php->capacity * 2;
- }
- php->a[php->size] = x;
- php->size++;
- AdjustUp(php, php->size - 1);
- }
- //删除堆的顶的数据,并且让它的结构还是一个堆
- void HeapPop(HP *php)
- {
- if (php == NULL)
- return;
- if (HeapEmpty(php))
- return;
- int end = php->size - 1;
- Swap(&php->a[0], &php->a[end]);
- php->size--;
- AdjustDown(php, 0);
- }
- //返回堆顶的数据
- HPDateType HeapTop(HP *php)
- {
- if (php == NULL)
- return;
- if (HeapEmpty(php))
- return;
- return php->a[0];
- }
- //返回堆里面数据的个数
- int HeapSize(HP *php)
- {
- if (php == NULL)
- return;
- return php->size;
- }
- //判断堆是否为空
- bool HeapEmpty(HP *php)
- {
- if (php == NULL)
- return;
- return (php->size == 0);
- }
- //将堆打印出来
- void HeapPrint(HP *php)
- {
- int i;
- int m = 1, n = 0;
- printf("\n从上到下输出堆的内容:\n");
- for (i = 0; i < php->size; i++)
- {
- printf("%d ", php->a[i]);
- if (i == n)
- {
- putchar('\n');
- m = m * 2;
- n += m;
- }
- }
- printf("\n\n");
- }
- void HeapSort(HP *php)
- {
- int t = php->size;
- while (php->size != 0)
- {
- HeapPop(php);
- }
- php->size = t;
- }
- </div></article>
复制代码 |
|