저는 C에서 매우 익숙합니다. C에서 숫자와 문자열을 저장 한 다음이 예에서이를 인쇄합니다.설명은 마지막으로 입력 한 것에 대해서만 인쇄됩니다.
1 : Bread
2 : WashingUpLiquid
etc.
내가 지금까지 가지고있는 코드는 다음과 같습니다 순간
#include <stdio.h>
#include <stdlib.h>
#define LENGTH 300
struct node {
int data;
char * definition;
struct node *left;
struct node *right;
};
struct node *node_insert(struct node *p, int value, char * word);
void print_preorder(struct node *p);
int main(void) {
int i = 0;
int d = 0;
char def[LENGTH];
struct node *root = NULL;
for(i = 0; i < 2; i++)
{
printf("Please enter a number: \n");
scanf("%d", &d);
printf("Please enter a definition for this word:\n");
scanf("%s", def);
root = node_insert(root, d, def);
printf("%s\n", def);
}
printf("preorder : ");
print_preorder(root);
printf("\n");
return 0;
}
struct node *node_insert(struct node *p, int value, char * word) {
struct node *tmp_one = NULL;
struct node *tmp_two = NULL;
if(p == NULL) {
p = (struct node *)malloc(sizeof(struct node));
p->data = value;
p->definition = word;
p->left = p->right = NULL;
}
else {
tmp_one = p;
while(tmp_one != NULL) {
tmp_two = tmp_one;
if(tmp_one->data > value)
tmp_one = tmp_one->left;
else
tmp_one = tmp_one->right;
}
if(tmp_two->data > value) {
tmp_two->left = (struct node *)malloc(sizeof(struct node));
tmp_two = tmp_two->left;
tmp_two->data = value;
tmp_two->definition = word;
tmp_two->left = tmp_two->right = NULL;
}
else {
tmp_two->right = (struct node *)malloc(sizeof(struct node));
tmp_two = tmp_two->right;
tmp_two->data = value;
tmp_two->definition = word;
tmp_two->left = tmp_two->right = NULL;
}
}
return(p);
}
void print_preorder(struct node *p) {
if(p != NULL) {
printf("%d : %s\n", p->data, p->definition);
print_preorder(p->left);
print_preorder(p->right);
}
}
int
의 만 설명 부분은 마지막 입력에 대한 출력한다 위해 작동하는 것 같다. char
배열의 포인터와 관련이 있다고 가정하지만 작동시키지 않아도됩니다. 어떤 아이디어 또는 조언?
그게 다 :) :) 대단히 고마워, 정말 짜증나! – Paul
옆에있는 녹색 체크를 클릭하여 수락 할 답변을 선택해야합니다. 그러면 질문에 대한 답변이 시스템에 전달되고 답변자와 평판도 제공됩니다. –