【问题标题】:Loading data to construct a ternary tree not working properly(C)加载数据构造三叉树不能正常工作(C)
【发布时间】:2021-08-15 09:08:27
【问题描述】:

我已经构建了一个三叉树的结构以及一些用于对其进行基本操作的函数。其中之一是从文件中逐字读取并构建树! 问题可能出在函数 read_words 中。 当我手动插入数据时,数据工作正常,但是当我尝试从文件中插入时,它只会使用上次数据输入的内容创建树。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>



typedef struct a{ 
    char * word ;
    int occurs;
    struct a * left;
    struct a * same;
    struct a * right; } Node; 
    
typedef Node * Node_ptr ;
typedef Node * TriTree ;

void inorder(TriTree x) {
    if(x==NULL) return;
    inorder(x->left);
    printf("%s(%d)--" , x->word, x->occurs);
    inorder(x->same);
    inorder(x->right);
    return;}

void strlower(char * lower){
    for (char *p = lower; *p; ++p) *p = tolower(*p);
    // printf("%s\n",lower);
};
// 1
Node_ptr create(char * word){
    Node_ptr tmp_ptr;
    tmp_ptr = (Node_ptr)malloc(sizeof(Node));
    tmp_ptr-> word = word;
    tmp_ptr-> occurs = 1;
    tmp_ptr-> left = NULL;
    tmp_ptr-> same = NULL;
    tmp_ptr-> right = NULL;
    return tmp_ptr;
}
TriTree insert(TriTree x, Node_ptr node_ptr){
    if(x==NULL){
        // printf("%s\n","Empty Tree!");
        x = node_ptr;
        return x;
    }

    int ret;
    strlower(x->word);
    strlower(node_ptr->word);
    ret = strcmp(x->word,node_ptr->word);
    if(ret < 0) {
    //   printf("str1 is less than str2");
      x->right = insert(x->right,node_ptr);
    } else if(ret > 0) {
    //   printf("str2 is less than str1");
      x->left = insert(x->left,node_ptr);
    } else {
    //   printf("str1 is equal to str2");
      x->same = insert(x->same,node_ptr);
    }
    return x;
} ;


TriTree read_words (FILE *f,TriTree x) {
    char c[1024];

    while (fscanf(f, " %1023s", c) == 1) {
        Node_ptr tmp; 
        // printf("%s\n",c);
        tmp = create(c);
        printf("%s\n",tmp->word);
        x = insert(x,tmp);
        //free(tmp);
    }
    fclose(f);

    return x;
}


int main()
{
    TriTree x;
    x = NULL;
   
    FILE * fp = fopen("input.txt", "r");
        
    x = read_words(fp,x);
    inorder(x);
    return 0;
}
input:
hello bye ask life BYE Hello night HeLLO

desired output:
ask bye BYE hello Hello HeLLo life night

my output:
hello hello hello hello hello hello hello hello

【问题讨论】:

  • tmp_ptr-&gt; word = word; 更改为tmp_ptr-&gt; word = strdup(word);。否则,您只是将每个节点指向同一个数组。不要忘记释放strdup 内存。

标签: c file struct tree scanf


【解决方案1】:

在函数内创建

Node_ptr create(char * word){
    Node_ptr tmp_ptr;
    tmp_ptr = (Node_ptr)malloc(sizeof(Node));
    tmp_ptr-> word = word;
   //...

您正在将用作参数的指针字分配给数据成员 tmp_ptr->字。

但是你在函数 read_words 的每次 create 调用中都传递了相同的指针

TriTree read_words (FILE *f,TriTree x) {
    char c[1024];

    while (fscanf(f, " %1023s", c) == 1) {
        Node_ptr tmp; 
        // printf("%s\n",c);
        tmp = create(c);
              ^^^^^^^^^

这是指向本地数组第一个元素的指针

    char c[1024];

而且退出函数后不会存活。

您需要在函数create中使用传递的字符串的副本动态创建一个字符串。

【讨论】:

  • @george.angelop 完全没有。不客气。:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多