The whole point of my C code is to insert string nodes in alphabetical order. Here is my code....
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node {
char * word;
struct node * left;
struct node * right;
}treeNode;
treeNode * head = NULL;
void traverse (treeNode * h)
{
if(h->left == NULL){
scanf("%s ", h->word);
if(h->right != NULL){
traverse(h->right);
}
}
else{
traverse(h->left);
printf("%s ", h->word);
if(h->right != NULL){
traverse(h->right);
}
}
treeNode *newNode(char * s)
{
treeNode *insert = (treeNode*)malloc(100*sizeof(treeNode));
insert->left = NULL;
insert->right = NULL;
insert->word = s;
return insert;
}
treeNode * addNode (treeNode * h, char * s)
{
if(h == NULL){
return newNode(s);
}
else{
if (strcmp (h->word, s)> 0){
h->left = addNode(h->left,s);
}
else{
h->right = addNode(h->right,s);
}
}
return h;
}
void main()
{
printf("\nTest Animals 1");
head = insert(head, "dog");
insert(head, "horse");
insert(head, "frog");
insert(head, "fish");
insert(head, "cow");
traverse(head);
head = NULL;
printf("\nTest Food 2");
head = insert(head, "pizza");
insert(head, "sushi");
insert(head, "burger");
insert(head, "salad");
insert(head, "nuggets");
traverse(head);
head = NULL;
printf("\nTest Sports 3");
head = insert(head, "soccer");
insert(head, "basketball");
insert(head, "football");
insert(head, "tennis");
insert(head, "gymnastics");
traverse(head);
head = NULL;
}
It compiles perfectly with no errors at all but my main method isn't allowing me to print out my sample test cases. Could it be a problem in the code itself? I've looked it all over and I do not see what is wrong with it. This is also my first C code so I apologize if there are mistakes that I may have missed.