該計劃是建立硬編碼的一棵樹,然後打印出硬代碼樹。我從一個結構開始,它包含一個對象名稱,一個問題以及指向同一結構的是或否節點。在主要方法中,我試圖一步一步構建結構。但我相信這不是用節點創建樹的正確方法。C語言創建樹節點,硬編碼,我想在這裏實現樹
我的設計說明:它是計算機和用戶之間的遊戲,計算機詢問問題,然後用戶回答是或否,計算機會猜測該對象。
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 <stdio.h>
#include <stdlib.h>
//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->objectname == NULL)
{
printf("Object : [NOTHING]");
printf("Question : %s", ptr->question);
printf("Yes : &s", ptr->yes_ptr);
printf("No : &s", ptr->no_ptr);
}else {
printf("Object : %s", ptr->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 = &thenode;
nodePrint(&a);
nodePrint(&b);
}