描述仅打印最后输入的内容

发布于 2024-08-26 21:48:04 字数 2162 浏览 4 评论 0原文

我对 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);
  }
}

目前它似乎适用于ints 但描述部分仅打印最后输入的内容。我认为它与 char 数组上的指针有关,但我没有运气让它工作。有什么想法或建议吗?

I'm quite new to C and I'm trying to implement a binary tree in C which will store a number and a string and then print them off e.g.

1 : Bread
2 : WashingUpLiquid
etc.

The code I have so far is:

#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);
  }
}

At the moment it seems to work for the ints but the description part only prints out for the last one entered. I assume it has something to do with pointers on the char array but I had no luck getting it to work. Any ideas or advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

当爱已成负担 2024-09-02 21:48:04

您总是对 def 执行 scanf,然后将其传递给插入例程,该例程仅保存指向 def 的指针。因此,由于所有条目都指向 def 缓冲区,因此它们都指向您存储在该缓冲区中的最后一个字符串。

您需要复制字符串并将指向副本的指针放入二叉树节点中。

You're always doing a scanf into def and then passing that to your insert routine which just saves the pointer to def. So, since all of your entries point to the def buffer, they all point to whatever was the last string you stored in that buffer.

You need to copy your string and place a pointer to the copy into the binary tree node.

难以启齿的温柔 2024-09-02 21:48:04

问题是您对字符串使用相同的缓冲区。请注意,您的结构体保存着一个指向 char 的指针,并且您每次都传递与该指针相同的 char 数组。

当您在缓冲区上调用 scanf 时,您正在更改它指向的数据,而不是指针本身。

要解决此问题,在将其分配给结构之前,您可以使用 strdup。因此,代码行将变为

tmp_*->definition = strdup(word);

请记住,一旦使用完 strdup 返回的 char 数组,就必须将其释放,否则将会发生泄漏。

The problem is that you're using the same buffer for the string. Notice your struct is holding a pointer to a char, and you are passing the same char array as that pointer each time.

When you call scanf on the buffer, you are changing the data it points to, not the pointer itself.

To fix this, before assigning it over to a struct, you can use strdup. So the lines of code would become

tmp_*->definition = strdup(word);

Keep in mind that the char array returned by strdup must be freed once you are done with it, otherwise you'll have a leak.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文