C++ 堆的历遍和查找
来源:百度知道 编辑:UC知道 时间:2024/09/22 16:53:53
需要详细 简单 明了 易懂 的代码
谢谢各位有才之士 帮忙啦
10分报答。本新人
本人有Email yueyang87317@126.com
C++ Heap(堆) STL提供了heap API 可以供人使用,并且有iterator可以遍历
所以看他的source code比较好
下面是C的一个简单实现:
一个heap.h
++++++++++++++++++heap.h start+++++++++++++++++++++++++++
#ifndef __HEAP_H__
#define __HEAP_H__
/* Maximum number of values that can be stored in the heap. */
#define MAX_HEAP_ELEMS 100
/*
* A simple heap data structure, for storing floats.
*/
typedef struct
{
/* Number of values currently in the heap. */
int num_values;
/* The values in the heap. */
float values[MAX_HEAP_ELEMS];
} float_heap;
/* Initialize a heap data structure. */
void init_heap(float_heap *pHeap);
/* Returns the first (i.e. smallest) value in the heap. */
float get_first_value(float_heap *pHeap);
/* Adds another value to the heap, in the proper place. */
void add_value(float_heap *pHeap, float newval);
#endif /* __HEAP_H__ */
+++++++++++