All pastes #584019 Raw Edit

Untitled

public text v1 · immutable
·published 2007-06-22 16:17 UTC
rendered paste body

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

struct heap{
	int nheap;
	int max;
	void **a;
	};
	
typedef struct heap* PQ;

PQ PQ_New(int max){
	h=(PQ)malloc(sizeof(struct heap));
	h->nheap=0;
	h->max=max;
	h->a=(void**)malloc(max*sizeof(void*));
	return h;
}

void UrcaValoare(PQ h, int poz){
	if (poz/2>1 && h->a[poz]>h->a[poz/2])
		{
		interschimba(h->a[poz], h->a[poz/2]);
		poz=poz/2;
		}
	}
	
void Adauga(PQ h, void* x){
	//%assert(!)
	h->nheap++;
	h->a[h->nheap]=x;
	UrcaValoare(h,h->nheap);
}
	

int main(){
	int v[30], i ;
	v[1]=2; v[2]=9; v[3]=5;
	v[4]=3; v[5]=6; v[6]=10;
	
	
	h=PQ_New(15);
	for(i=1;i<=6;i++)
		Adauga(h,&v[i]);
	
	for (i=1;i<=h->nheap;i++)
		printf("%d ",*(int*)h->a[i]);
		
	return 0;
}