IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 迟来的数据结构-堆(优先队列)-C语言实现 -> 正文阅读

[C++知识库]迟来的数据结构-堆(优先队列)-C语言实现

纸上得来终觉浅,绝知此事要躬行。

头文件 lc_heap.h?

#ifndef LC_HEAP_H
#define LC_HEAP_H#define LC_HeapMinData -1
typedef int LC_HeapElement;

typedef int(*LC_HeapElementCmp)(const LC_HeapElement* l,const LC_HeapElement* r);

typedef struct tagLCHeap{
    int capacity;
    int count;
    LC_HeapElement  * elements;
    LC_HeapElementCmp cmp;
}LC_Heap;

LC_Heap* LC_HeapInit(int capacity,LC_HeapElementCmp cmp);
void LC_HeapDestroy(LC_Heap* heap);
void LC_HeapPrint(LC_Heap* heap);
void LC_HeapInsert(LC_Heap* heap, LC_HeapElement e);
LC_HeapElement LC_HeapPop(LC_Heap* heap);
LC_HeapElement LC_HeapTop(LC_Heap* heap);
int LC_HeapIsFull(LC_Heap* heap);
int LC_HeapIsEmpty(LC_Heap* heap);

#endif //LC_HEAP_H

函数实现? lc_heap.c

#include "lc_heap.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
static int parent(int i)
{
    return i / 2;
}

static int left(int i)
{
    return i * 2;
}

static int right(int i)
{
    return i * 2 + 1;
}

LC_Heap* LC_HeapInit(int capacity, LC_HeapElementCmp cmp)
{
    LC_Heap* heap = ( LC_Heap* )malloc(sizeof(LC_Heap));
    if (heap == NULL) {
        return NULL;
    }
    heap->capacity = capacity + 1;
    heap->elements = ( LC_HeapElement* )malloc(sizeof(LC_HeapElement) * heap->capacity);
    if (heap->elements == NULL) {
        free(heap);
        return NULL;
    }
    memset(heap->elements, 0, sizeof(LC_HeapElement) * heap->capacity);
    heap->elements[0] = LC_HeapMinData;
    heap->cmp         = cmp;
    heap->count       = 0;
    return heap;
}

void LC_HeapDestroy(LC_Heap* heap)
{
    if (heap != NULL) {
        if (heap->elements != NULL) {
            free(heap->elements);
            heap->elements = NULL;
        }
        free(heap);
        heap = NULL;
    }
}

static void LcHeapUpImp(LC_Heap* heap, int index)
{
    while (index != 1) {
        if (heap->cmp(&heap->elements[index], &heap->elements[parent(index)]) < 0) {
            break;
        }
        LC_HeapElement eleTmp = heap->elements[index];
        heap->elements[index] = heap->elements[parent(index)];
        heap->elements[parent(index)] = eleTmp;
        index = parent(index);
    }
}

void LC_HeapInsert(LC_Heap* heap, LC_HeapElement e)
{
    int pos             = ++heap->count;
    heap->elements[pos] = e;
    LcHeapUpImp(heap, pos);
}

static void LcHeapDownImp(LC_Heap* heap, int index)
{
    if (left(index) > heap->count) {
        return;
    }
    int child = left(index);
    if (child < heap->count && heap->cmp(&heap->elements[child + 1], &heap->elements[child]) > 0) {
        child++;
    }
    LC_HeapElement eleTmp = heap->elements[index];
    heap->elements[index] = heap->elements[child];
    heap->elements[child] = eleTmp;
    LcHeapDownImp(heap, child);
}

LC_HeapElement LC_HeapPop(LC_Heap* heap)
{
    LC_HeapElement ele = LC_HeapTop(heap);
    if (ele == LC_HeapMinData) {
        return ele;
    }
    heap->elements[1] = heap->elements[heap->count--];
    LcHeapDownImp(heap, 1);
    return ele;
}

LC_HeapElement LC_HeapTop(LC_Heap* heap)
{
    if (LC_HeapIsEmpty(heap)) {
        return heap->elements[0];
    }
    return heap->elements[1];
}

int LC_HeapIsFull(LC_Heap* heap)
{
    return heap->count + 1 == heap->capacity;
}
int LC_HeapIsEmpty(LC_Heap* heap)
{
    return heap->count == 0;
}


void LC_HeapPrint(LC_Heap* heap)
{
    for (int i = 0; i <= heap->count; ++i) {
        printf("%d-", heap->elements[i]);
    }
    printf("\r\n");
}

测试程序 main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "lc_heap.h"

int LC_HeapElementCmpImp(const LC_HeapElement* l,const LC_HeapElement* r)
{
    return (*r) - (*l);
}

int main()
{
    LC_Heap* heap = LC_HeapInit(100,LC_HeapElementCmpImp);
    LC_HeapElement elems[] = {1,3,4,5,2,6,9,7,8,0};
    for (int i = 0; i < sizeof(elems) / sizeof(elems[0]); ++i) {
        LC_HeapInsert(heap, elems[i]);
        //LC_HeapPrint(heap);
    }
    while(!LC_HeapIsEmpty(heap)) {
        printf("%d - ", LC_HeapPop(heap));
    }
    LC_HeapDestroy(heap);
    return 0;
}

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-07-15 15:59:28  更:2021-07-15 16:00:12 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/26 13:42:50-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码