Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate tree node in c, hard code the tree
    text
    copied!<p>The program that I want achieve here is to construct a tree by hard coding, and then print out the hard code tree. I start with a struct which contain a object name, a question, and a yes or no node that point to the same struct. In the main method, i tried to construct the struct step by step. But I believe that is not the right way of creating a tree with node. </p> <p>Description of my design: It s a game between computer and user, the computer ask for the question, and then the user answer it with yes or no, and the computer will guess for the object. </p> <pre><code> Start here | v Does it have a tail? /yes no\ v v a pangolin Is it flat, round and edible? /yes no\ v v a pizza Pete #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; //object name as key, questions as value struct node { char *objectname;// a string declaration to hold an object-name (which may be NULL) char *question;// a string declaration to hold a question (which may be NULL) struct node *yes_ptr; // only NULL for objects struct node *no_ptr; // only NULL for objects }; typedef struct node thenode; thenode *objectname = NULL; thenode *question =NULL; void nodePrint(struct node *ptr){ if(ptr-&gt;objectname == NULL) { printf("Object : [NOTHING]" ); printf("Question : %s", ptr-&gt;question); printf("Yes : &amp;s", ptr-&gt;yes_ptr); printf("No : &amp;s", ptr-&gt;no_ptr); }else { printf("Object : %s", ptr-&gt;objectname); printf("Question : [NOTHING]"); } } int main(argc, **argv){ //if ((new_obj = malloc(sizeof(thenode))) == NULL) { abort(); } thenode a={NULL, "Does it have a tail?", "a pangolin", "pete"}; thenode b={"a pizza",NULL, NULL, NULL}; //thenode c={NULL, "Is it flat, round and edible?", "a pizza", "pete"}; //thenode c={NULL, "Can you dip it in your tea? ", "biscuit", "a pizza"}; struct node *ptr = &amp;thenode; nodePrint(&amp;a); nodePrint(&amp;b); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload