I'm a newbie and trying to learn C, but faced some problem in implementing strings and pointer in a linked list. I was required to build a preorder tree using a linked list and one problem came out.
BTW here are my codes:
#include <stdio.h>
#include <stdlib.h>
struct BinTreeNode {
char *data ;
struct BinTreeNode *left;
struct BinTreeNode *right;
};
struct BinTreeNode *initBinTreeNode(char *data);
void visit(char *data);
void preorder(struct BinTreeNode *node);
void postorder(struct BinTreeNode *node);
void inorder(struct BinTreeNode *node);
int main(void)
{
struct BinTreeNode *root, *parent1, *parent2;
/* growing the tree */
root = initBinTreeNode("5");
root->left = initBinTreeNode("Mee (F) & Keong (M)");
root->right = initBinTreeNode("Saw (F)");
parent1 = root->left;
parent1->left = initBinTreeNode("Min (F) & Heng (M)");
parent1->right = initBinTreeNode("Ning (F)");
parent2 = parent1->left;
parent2->left = initBinTreeNode("Rui (F)");
/* traverse and print tree */
printf("\nPreorder traversal:\t"); preorder(root);
return(0);
}
void preorder(struct BinTreeNode *node)
{
if (node){ /* if Node exists */
visit(node->data);
preorder(node->left);
preorder(node->right);
}
}
void visit(char *data)
{
printf("%c -", *data);
}
struct BinTreeNode *initBinTreeNode(char *data)
{
struct BinTreeNode *temp;
temp = malloc(sizeof(struct BinTreeNode));
if (temp == NULL)
{
printf ("Memory allocation failed.");
return NULL;
}
(*temp).data = data;
(*temp).left = NULL;
(*temp).right = NULL;
return (temp);
}
When I have {"5"},{"Mee (F) & Keong (M)"},{"Saw (F)"},{"Min (F) & Heng (M)"},{"Ning (F)"},{"Rui (F)"} in my list, it comes out only the first word is printed out like this:
Preorder traversal: 5 -M -M -R -N -S -
The preorder output that I want, should be:
Preorder traversal: 5 -Mee (F) & Keong (M) -Min (F) & Heng (M) -Rui (F) -Ning (F) -Saw (F) -
May I know, what I am missing? I would be really grateful for any help and advice!

visitwhen you first wrote it, you would have noticed the error, and known that the bug was in that function.