This is Heap.h
Code:
#ifndef _HEAP_H_
#define _HEAP_H_
typedef int ( *heapcomparefunc )( void *p0, void *p1 );
typedef struct _heap {
void **ap;
unsigned int cp, cpAlloc;
heapcomparefunc phcf;
} heap;
extern int HeapCreate( heap *ph, unsigned int c, heapcomparefunc phcf );
extern void HeapDestroy( const heap *ph );
extern int HeapInsert( heap *ph, void *p );
extern void *HeapDelete( heap *ph );
extern int HeapDeleteItem( heap *ph, const void *p );
extern void *HeapLookup( const heap *ph );
#endif
The implementation of the library of the Heap.h is here in the Heap.c
Code:
#include <stddef.h>
#include <stdlib.h>
#include "heap.h"
extern int HeapCreate( heap *ph, unsigned int c, heapcomparefunc phcf ) {
if( ( ph->ap = (void**)malloc( c * sizeof( void * ) ) ) == NULL )
return -1;
ph->cp = 0;
ph->cpAlloc = c;
ph->phcf = phcf;
return 0;
}
extern void HeapDestroy( const heap *ph ) {
free( ph->ap );
}
extern int HeapInsert( heap *ph, void *p ) {
unsigned int i;
void *pTemp;
if( ph->cp == ph->cpAlloc ) {
if( ( ph->ap = (void**)realloc( ph->ap, ( ph->cpAlloc << 1 ) *
sizeof( void * ) ) ) == NULL )
return -1;
ph->cpAlloc <<= 1;
}
ph->ap[ ph->cp ] = p;
for( i = ph->cp++; i && ph->phcf( ph->ap[ i ],
ph->ap[ ( i - 1 ) >> 1 ] ) < 0;
i = ( i - 1 ) >> 1 ) {
pTemp = ph->ap[ i ];
ph->ap[ i ] = ph->ap[ ( i - 1 ) >> 1 ];
ph->ap[ ( i - 1 ) >> 1 ] = pTemp;
}
return 0;
}
static int DeleteItem( heap *ph, unsigned int i ) {
void *pTemp;
unsigned int iSwap;
ph->ap[ i ] = ph->ap[ --ph->cp ];
while( ( i << 1 ) + 1 < ph->cp ) {
iSwap = i;
if( ph->phcf( ph->ap[ iSwap ], ph->ap[ ( i << 1 ) + 1 ] ) > 0 )
iSwap = ( i << 1 ) + 1;
if( ( i << 1 ) + 2 < ph->cp &&
ph->phcf( ph->ap[ iSwap ], ph->ap[ ( i << 1 ) + 2 ] ) > 0 )
iSwap = ( i << 1 ) + 2;
if( iSwap == i )
break;
pTemp = ph->ap[ i ];
ph->ap[ i ] = ph->ap[ iSwap ];
ph->ap[ iSwap ] = pTemp;
i = iSwap;
}
if( ph->cpAlloc > 1 && ( ph->cp << 1 < ph->cpAlloc ) )
return (( ph->ap = (void**)realloc( ph->ap, ( ph->cpAlloc >>= 1 ) *
sizeof( void * ) ) ) == NULL) ? 0 : -1;
return 0;
}
extern int HeapDeleteItem( heap *ph, const void *p ) {
unsigned int i;
for( i = 0; i < ph->cp; i++ )
if( ph->ap[ i ] == p )
return DeleteItem( ph, i );
return -1;
}
extern void *HeapDelete( heap *ph ) {
void *p = HeapLookup( ph );
if( !ph->cp )
return NULL;
return DeleteItem( ph, 0 ) ? NULL : p;
}
extern void *HeapLookup( const heap *ph ) {
return ph->cp ? ph->ap[ 0 ] : NULL;
}
And, this is testing Main function.
Code:
#include "heap.h"
#include <stdio.h>
#include <string.h>
int rec_cmp (const char *a, const char *b)
{
return strcmp(a,b);
}
int main(int argc, char *argv[]){
heap *heap;
/*
*I think the problem is here. When no nodes are present the *heap hold garbage
* Values only. So, for allocating the Root Node we need to use malloc to initialize it?
*/
//Just to create the Root Node of the Heap
HeapCreate(heap, 1, (heapcomparefunc)rec_cmp);
}
So, where is the problem ??