이유를 모르겠는데 도움이 필요합니다. 아래 코드는 입니다. 루트 인덱스 만 쓰면됩니다. 루트의 왼쪽 및 오른쪽 포인터는 여전히 비어 있습니다. 하지만 메모리에 할당하고 색인을 복사 한 문자열.CST의 BST - 루트에만 씁니다.
typedef struct bst {
char index[128];
struct bst *left;
struct bst *right;
}bst;
int main(void)
{
bst *root = malloc (sizeof(bst));
root->left = NULL;
root->right = NULL;
strcpy(root->index, "indexisnull");
bst *temp;
temp = root;
FILE *fp, *fp2; //file pointers
char word[128];
fp = fopen("Input1.txt", "r"); //opening text files in read mode
fp2 = fopen("Input2.txt", "r");
while(fscanf(fp,"%s", word) == 1)
{
//getting strings from file to string 'word'
temp = root;
if(strcmp(root->index, "indexisnull") == 0)
{
strcpy(root->index, word);
}
if(strcmp(word, root->index) < 0)
{
//temp = temp->left;
while(temp != NULL)
{
if(strcmp(word, temp->index) < 0)
{
printf("lefttoleft\n");
temp = temp->left;
}
else if(strcmp(word, temp->index) > 0)
{
temp = temp->right;
printf("lefttoright\n");
}
}
temp = malloc(sizeof(bst));
strcpy(temp->index, word);
temp->left = NULL;
temp->right = NULL;
}
else if(strcmp(word, root->index) > 0)
{
temp = temp->right;
while(temp != NULL)
{
if(strcmp(word, temp->index) < 0)
{
temp = temp->left;
printf("righttoleft\n");
}
else if(strcmp(word, temp->index) > 0)
{
temp = temp->right;
printf("righttoright\n");
}
}
temp = malloc(sizeof(bst));
strcpy(temp->index, word);
temp->left = NULL;
temp->right = NULL;
}
}
temp = root;
printf("%s\n", temp->index);
printf("%s\n", temp->right->index);
printf("%s\n", temp->left->index);
fclose(fp); //closing files
fclose(fp2);
return 0;
}
* 항상 게시물에 세부 정보를 추가 할 수 있습니다. [도움말 페이지] (http://stackoverflow.com/help), 특히 [여기서 어떤 주제에 관해서 물어볼 수 있습니까?] (http://stackoverflow.com/help/) 섹션을 읽어보십시오. on-topic) 및 [ "어떤 유형의 질문을하지 않아야합니까?"] (http://stackoverflow.com/help/dont-ask). 또한 [둘러보기] (http://stackoverflow.com/tour)와 [좋은 질문을하는 방법에 대해 읽어보십시오.] (http://stackoverflow.com/help/how-to-ask). 마지막으로 [** Minimal **, Complete, Verifiable Example] (http://stackoverflow.com/help/mcve)을 만드는 방법을 배우십시오. –
이번에는 들여 쓰기가 있습니다. 다음에 나는 네 엄마 한테 그것에 대해 말할거야! – WedaPashi
'fopen'이 필요한 것을 반환하는지 확인 했습니까? 'fp'와'fp2'를 확인하십시오. 시작하기 좋은 곳입니다. – WedaPashi