Attached there is an example of usage

Regards,
Ian L.

On Wed, Jun 16, 2010 at 10:32 AM, Manu Kaul <[email protected]> wrote:

> Hi Ian,
> I have already looked at that API but I am just wondering if I can see some
> sample code to see how it all hangs together and get a better idea?
>
> Cheers!
>
> On Wed, Jun 16, 2010 at 2:29 PM, <[email protected]> wrote:
>
>> GNode has a 'data' attribute, which you can set to any structure you want.
>>
>> Take a look at the GNode API and you will see every function you need to
>> manipulate the tree:
>> http://library.gnome.org/devel/glib/stable/glib-N-ary-Trees.html
>>
>> Em , Manu Kaul <[email protected]> escreveu:
>>
>> > Hi All,I am a newbie to this mailing list and I want to implement a
>> n-ary tree and came across "GNode" in glib and wanted to see if there was
>> some good sample code that I could look at to see how I can go about setting
>> up this tree. Also I needed the ability to be able to work upwards from a
>> node all the way to the root by just following the parent pointers. So I was
>> unsure as to how I can do this using GNode. I also wanted to know how I can
>> add additional pointers to the GNode node if I needed them?
>> >
>> >
>> >
>> > Cheers,
>> > CBro
>> >
>> >
>> >
>> >
>>
>
>
>
> --
>
> The greater danger for most of us lies not in setting our aim too high and
> falling short; but in setting our aim too low, and achieving our mark.
> - Michelangelo
>
#include <glib.h>

/*
 * Defines a macro for casting an object into a 'Person'
 */
#define PERSON(o) (Person*)(o)

typedef struct {
	gchar * name;
	gint age;
} Person;

Person * person_new(const gchar * name, gint age) {
	Person * p;
	p = g_new(Person, 1);
	p->name = g_strdup(name);
	p->age = age;
	return p;
}

void person_print(Person * p) {
	g_printf("Name: %s\nAge: %d\n\n", p->name, p->age);
}

void person_free(Person * p) {
	g_free(p->name);
	g_free(p);
}

gboolean traverse_free_func(GNode * node, gpointer data) {
	person_free(PERSON(node->data));
	return FALSE;
}

int main(int argc, char *argv[])
{
	GNode * father;
	GNode * dother;
	GNode * brother;

	father = g_node_new(person_new("John", 50));
	dother = g_node_new(person_new("Sophia", 21));
	brother = g_node_new(person_new("Bob", 20));

	g_node_append(father, dother);
	g_node_insert_after(father, dother, brother);

	/*
	 * Fetch data
	 */

	GNode * first_child;
	GNode * sibling;

	first_child = g_node_first_child(father);
	sibling = g_node_next_sibling(first_child);

	person_print(PERSON(father->data));
	person_print(PERSON(first_child->data));
	person_print(PERSON(sibling->data));

	/*
	 * We must free everything now.
	 */

	g_node_traverse(father, G_IN_ORDER, G_TRAVERSE_ALL, -1,
			traverse_free_func, NULL);
	g_node_destroy(father);

	return 0;
}
_______________________________________________
gtk-list mailing list
[email protected]
http://mail.gnome.org/mailman/listinfo/gtk-list

Reply via email to