Example #1
0
int main()
{
	int i,n,x;
	heap *p=NULL;
	p=heapAlloc();
	scanf("%d",&n);
	heapInit(p,sizeof(int),n+1,gt);
	for(i=0;i<n/2;i++)
	{
		x=i+n/2;
		heapPush(p,&x);
	}
	for(i=0;i<n/2;i++)
		heapPush(p,&i);
	while(!heapEmpty(p))
	{
		heapPop(p,&x);
		printf("%d%c",x,heapEmpty(p)?'\n':' ');
	}
	return 0;
}
void *heapExtractMin(struct heap *h)
/* Extract the smallest element from the heap. */
{
struct heapEl *el = NULL;
void *val = NULL;
if(heapEmpty(h))
    errAbort("heap.c::heapExtractMin() - No more elements to removes, heap is empty.");
el = h->array[0];
h->array[0] = h->array[h->count - 1];
heapMinHeapify(h, 0);
val = el->val;
h->count--;
freez(&el);
return val;
}
Example #3
0
int main() {
    
    Heap* hh = heapNew( 1 );
    
    heapInsert( hh, "first", 1 );
    
    long pp;
    char* itemout = heapPop( hh, &pp );
    
    printf( "%s\n", itemout );
    printf( "size:%d\n", hh->size );
    
    heapInsert( hh, "one", 1 );
    heapInsert( hh, "ten", 10 );

    while( !heapEmpty( hh ) ) {
        itemout = heapPop( hh, &pp );
        printf( "%s\n", itemout );
        printf( "size:%d\n", hh->size );
    }
    
    heapInsert( hh, "ten", 10 );
    heapInsert( hh, "one", 1 );

    while( !heapEmpty( hh ) ) {
        itemout = heapPop( hh, &pp );
        printf( "%s\n", itemout );
        printf( "size:%d\n", hh->size );
    }
    
    heapInsert( hh, "384", 384 );
    heapInsert( hh, "887", 887 );
    heapInsert( hh, "778", 778 );

    while( !heapEmpty( hh ) ) {
        itemout = heapPop( hh, &pp );
        printf( "%s\n", itemout );
        printf( "size:%d\n", hh->size );
    }
    
    int i;
    for(i=0; i<1000; i++) {
        char* payload = (char*)malloc(200*sizeof(char));
        long priority = rand()%1000+1;
        sprintf(payload, "%ld", priority);
        
        printf( "inserting '%s' with priority %ld\n", payload, priority );
        
        heapInsert( hh, payload, priority );
    }
    
    while( !heapEmpty( hh ) ) {
        itemout = heapPop( hh, &pp );
        printf( "%s\n", itemout );
        printf( "size:%d\n", hh->size );
        free(itemout);
    }
    
    heapDestroy( hh );
    
    return 1;
}