[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
Libcx-Heap
Here is a little example how to use libc-x heap :

\#include <libcx/heap.h>
static int cmp(void *d1,void* d2)
{
	int i1= atoi(d1);
	int i2= atoi(d2);
	return (i1 < i2 ? -1 : (i1 > i2 ? 1 : 0));
}
int main()
{
	heap_t *h;
	int i;
        //init a heap with a comparator function
        //but no destroy function : we'll use static data
	heap_init(&h,100,NULL,cmp);
	heap_push(h,"1");
	heap_push(h,"1");
	heap_push(h,"20");
	heap_push(h,"3");
	heap_push(h,"3");
	heap_push(h,"4");
	heap_push(h,"15");
	heap_push(h,"5");
	heap_push(h,"6");
	heap_push(h,"7");
	heap_push(h,"7");
	heap_push(h,"31");
        //display content: remember that linear display isn't sorted
        //because of array based implementation
        //you could use a foreach loop instead
	for(i=1;i<h->_max;i++)
	{
	  printf("%s ",h->array_heap[i]);
	}
	printf("\n");
	while(h->_max>1)
	{
		void *data;
		heap_pop(h,&data);
		printf("poping: %s\n",data);
	}
	return 0;
}


last edited (January 31, 2008) by GCogniaux, Number of views: 973, Current Rev: 4 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.