
		arthur.afarias
		
		(usa Nenhuma)
		
		Enviado em 18/07/2010 - 10:45h 
		Gente, agora o código não tem mais defeito, aparente. Consigo adicionar nós e atributos ao root, porém quando tento pendurar um atributo a um nó dentro de root dá um erro em tempo de execução!
O interessante é quando penduro um atributo em root não ocorre nenhum erro! Vejam, compilem o código!
Só uma curiosidade, vocês conseguem ver algum tipo de utilidade para este código como eu consigo?
Novo Código:
/*
 ============================================================================
 Name        : MenuArvore.c
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in
  C, Ansi-style
 ============================================================================
 */
#include <stdio.h>
#include <stdlib.h>
typedef struct Attribute_t{
	char *label;
	char *content;
} Attribute;
typedef struct Node_t{
	int nChilds;
	int nAttributes;
	Attribute **attribute;
	struct Node_t **child;
} Node;
// Node Operations
int append_child(Node *, Node *);
int remove_child(Node *, int);
int append_attribute(Node *, Attribute *);
int remove_attribute(Node *, int);
int set_attribute(Node *, char *, char *);
char *get_attribute(Node *, char *);
int main(void) {
	Node root, child1;
	Attribute attr1;
	attr1.label = "etiqueta";
	attr1.content = "conteudo";
	root.nChilds = 0;
	root.nAttributes = 0;
	append_child(&root, &child1);
	append_attribute (root.child[0], &attr1);
	printf("%s %s\n", root.child[0]->attribute[0]->label, root.child[0]->attribute[0]->content);
	//printf("%s", append_child(root, child));
	return EXIT_SUCCESS;
}
int append_child(Node *node, Node * child) {
	if (node->nChilds == 0) {
		if ((node->child = (Node **) calloc (1, sizeof(Node *))) == NULL) {
			return -1;
		} else {
			node->nChilds = 1;
			node->child[0] = child;
		}
	} else {
		if ((node->child = (Node **) realloc(node->child, (node->nChilds+1)*sizeof(Node *))) == NULL) {
			return -1;
		} else {
			node->nChilds++;
			node->child[node->nChilds-1] = child;
		}
	}
	return 0;
}
int remove_child(Node *node, int child_position) {
	int i;
	if (node->nChilds == 0) {
		return -1;
	} else {
		for (i = 0; i < node->nChilds-child_position; i++) {
			node->nChilds--;
			node->child[child_position-1] = node->child[child_position];
			node->child = (Node **) realloc(node, node->nChilds * sizeof(Node *));
		}
	}
	return 0;
}
int append_attribute(Node * node, Attribute * attr) {
	if (node->nAttributes == 0) {
		if ((node->attribute = (Attribute **) calloc (1, sizeof (Attribute *))) == NULL) {
			return -1;
		}
		else {
			node->nAttributes = 1;
			node->attribute[0] = attr;
		}
	} else {
		if ((node->attribute = (Attribute **) realloc (node->attribute, sizeof (Attribute *))) == NULL) {
			return -1;
		} else {
			node->attribute[node->nAttributes] = attr;
			node->nAttributes++;
		}
	}
	return 0;
}
int remove_attribute(Node *node, int attribute_position) {
	int i;
	if (node->nAttributes == 0) {
		return -1;
	} else {
		for (i = 0; i < node->nAttributes-attribute_position; i++) {
			node->nAttributes--;
			node->attribute[attribute_position-1] = node->attribute[attribute_position];
			node->attribute = (Attribute **) realloc(node, node->nAttributes * sizeof(Attribute *));
		}
	}
	return 0;
}
int set_attribute(Node * node, char * label, char * content) {
	int i;
	for (i=0; i<node->nAttributes && node->attribute[i]->label != label; i++);
	node->attribute[i]->label = label;
	node->attribute[i]->content = content;
	return 0;
}
char *get_attribute(Node * node, char * label) {
	int i;
	for (i=0; i<node->nAttributes && node->attribute[i]->label != label; i++);
	if (i == node->nAttributes) {
		return NULL;
	} else {
		return node->attribute[i]->content;
	}
	return 0;
}