N
negative
Dear all,
I want to create a tree, where every node has an undetermined number
of children (during the population of the tree, the exact number of
children of each node , will become clear). So i thought that i could
use in each node a queue in which we will add, every time a new child
is born, the address of this node. Here are the structures i have used:
// The queue node. Every node contains a pointer to a tree node and a
pointer to the next
// element of the queue
typedef struct queue_tag {
struct TreeNode_tag * child_ptr;
struct queue_tag * next;
} QueueNode;
// the queue
typedef struct {
QueueNode *head;
QueueNode *tail;
} queue;
// the tree. Every leaf contains an integer i and a queue which
contains the pointers to node's children
typedef struct TreeNode_tag {
int i;
queue *q;
} TreeNode;
// The tree
typedef TreeNode * tree;
The problem is that i get a segmentation fault every time i try to
access the queue of the tree node. Here is the code i use to start the
tree :
tree *t;
*t = (TreeNode *)malloc(sizeof(TreeNode));
(*t)->i = 0; // a random value
(*t)->q->head = NULL; // initially the node has no children.
(*t)->q->tail = NULL;
I use gcc.
Thank you for your time.
I want to create a tree, where every node has an undetermined number
of children (during the population of the tree, the exact number of
children of each node , will become clear). So i thought that i could
use in each node a queue in which we will add, every time a new child
is born, the address of this node. Here are the structures i have used:
// The queue node. Every node contains a pointer to a tree node and a
pointer to the next
// element of the queue
typedef struct queue_tag {
struct TreeNode_tag * child_ptr;
struct queue_tag * next;
} QueueNode;
// the queue
typedef struct {
QueueNode *head;
QueueNode *tail;
} queue;
// the tree. Every leaf contains an integer i and a queue which
contains the pointers to node's children
typedef struct TreeNode_tag {
int i;
queue *q;
} TreeNode;
// The tree
typedef TreeNode * tree;
The problem is that i get a segmentation fault every time i try to
access the queue of the tree node. Here is the code i use to start the
tree :
tree *t;
*t = (TreeNode *)malloc(sizeof(TreeNode));
(*t)->i = 0; // a random value
(*t)->q->head = NULL; // initially the node has no children.
(*t)->q->tail = NULL;
I use gcc.
Thank you for your time.