我收到“错误:无法打开显示”消息在这个非常基本的 C 代码中,但我不明白为什么

发布于 2025-01-06 11:01:28 字数 2265 浏览 0 评论 0原文

所以我不久前做了一些 C 编程,基本上都忘记了,哈哈,但无论如何,我开始做我在网上找到的“C Refresher”事情,并且松散地遵循这个二叉搜索树示例,并遇到了错误。一旦我编译并运行它,它就会显示“错误:无法打开显示”。我在学校的某种 Linux 服务器上运行这段代码。无论如何,这是代码:

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

#define TRUE 1
#define FALSE 0 

typedef struct Node {
  int value;
  struct Node *left;
  struct Node *right;
} Node; 

void add (Node *node, int value)
{
  if (value < node->value) {
    //left side 
    if (node->left == NULL) {
      Node *newNode = malloc(sizeof(Node));
      newNode->value = value;
      newNode->left = NULL;
      newNode->right = NULL; 
      node->left = newNode;
    } else {
  add(node->left, value);
    }
  } else {
    //right side 
    if (node->right == NULL) {
      Node *newNode = malloc(sizeof(Node));
      newNode->value = value;
      newNode->left = NULL;
      newNode->right = NULL;
      node->right = newNode;
    } else {
      add(node->right, value);
    }
  }
}

int search(Node *node, int value)
{
  if (node == NULL) {
    return FALSE;
  } else if (node->value == value) {
    return TRUE;
  } else {
    if (value < node->value) {
      return search(node->left, value);
    } else {
      return search(node->right, value);
    }
  }
}

int main (int argc, char *argv[])
{
  Node root;

  root.value = 23;
  root.left = NULL;
  root.right = NULL; 

  add(&root, 5);
  add(&root, 50);
  add(&root, 8);
  add(&root, 2);
  add(&root, 34); 

  if (search(&root, 23)) {
    printf("23 lives in the tree.\n");
  } else {
    printf("23 does not live in the tree.\n");
  }

  if (search(&root, 42)) {
    printf("42 lives in the tree.\n");
  } else {
    printf("42 does not live in the tree.\n");
  }

  return 0;
}

代码可能看起来很长,但实际上非常基本。我想我可以在将代码粘贴到这里之前剪掉一些代码,但我想我会保留所有内容,以防我删除了对问题至关重要的内容。

另外我认为这可能与 Node 的事情有关,所以在我的 main 方法中,我在 Node root 之前放置了一个快速的 printf("hi"); ; 看看这是否会产生影响,但它仍然给了我同样的错误。我在这台学校服务器上的帐户上有另一个程序,该程序有一些 printf 语句,并且运行得很好。

我尝试用谷歌搜索这个问题,但所有这些奇怪的 Linux 线程都出现了,我无法真正理解它。我的计算机是 Windows,但我在一个名为 emacs 的程序中完成了所有这些编码,我通过一个名为 PuTTY 的程序进行了操作,该程序可让我连接到学校的 Linux 服务器。

我还用gcc -o tree tree.c编译了它。

抱歉写了这么多,我只是想提供尽可能多的信息。感谢任何可以提供帮助的人!

So I did a bit of C programming a while ago and basically forgot it all lol but anyways I started doing this "C Refresher" thing I found online and was following this binary search tree example kinda loosely and ran into an error. Once I compile it and run it, it says "Error: Can't Open Display". I am running this code on some kinda Linux server in school. Anyways, here's the code:

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

#define TRUE 1
#define FALSE 0 

typedef struct Node {
  int value;
  struct Node *left;
  struct Node *right;
} Node; 

void add (Node *node, int value)
{
  if (value < node->value) {
    //left side 
    if (node->left == NULL) {
      Node *newNode = malloc(sizeof(Node));
      newNode->value = value;
      newNode->left = NULL;
      newNode->right = NULL; 
      node->left = newNode;
    } else {
  add(node->left, value);
    }
  } else {
    //right side 
    if (node->right == NULL) {
      Node *newNode = malloc(sizeof(Node));
      newNode->value = value;
      newNode->left = NULL;
      newNode->right = NULL;
      node->right = newNode;
    } else {
      add(node->right, value);
    }
  }
}

int search(Node *node, int value)
{
  if (node == NULL) {
    return FALSE;
  } else if (node->value == value) {
    return TRUE;
  } else {
    if (value < node->value) {
      return search(node->left, value);
    } else {
      return search(node->right, value);
    }
  }
}

int main (int argc, char *argv[])
{
  Node root;

  root.value = 23;
  root.left = NULL;
  root.right = NULL; 

  add(&root, 5);
  add(&root, 50);
  add(&root, 8);
  add(&root, 2);
  add(&root, 34); 

  if (search(&root, 23)) {
    printf("23 lives in the tree.\n");
  } else {
    printf("23 does not live in the tree.\n");
  }

  if (search(&root, 42)) {
    printf("42 lives in the tree.\n");
  } else {
    printf("42 does not live in the tree.\n");
  }

  return 0;
}

The code might seem long but it is actually pretty basic. I think I coulda cut some of the code out before pasting it here but I figured I would leave everything in in case I took out something vital to the problem.

Also I thought it might have something to do with the Node thing so in my main method I put a quick printf("hi"); before the Node root; to see if that would make a difference but it still gave me the same error. And I have another program on my account on this school server and that program has some printf statements and it runs just fine.

I tried Googling the problem but all this weird Linux threads came up and I couldn't really understand it. My computer is Windows but I did all this coding in a program called emacs that I got to through a program called PuTTY that lets me connect to the school Linux server.

Also I compiled it with gcc -o tree tree.c.

Sorry for all the writing, I was just trying to give as much information as possible. Thanks to anyone who can help!

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

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

发布评论

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

评论(1

随遇而安 2025-01-13 11:01:28

您是像“./tree”一样调用它还是只是“tree”。看起来您正在尝试通过 ssh 运行 gui 应用程序。如果您使用的是 Linux 计算机,请尝试使用“ssh -XY HOST”连接来查看哪个应用程序。然后您应该看到一个应用程序启动。

Are you invoking it like "./tree" or just "tree". It looks like you are trying to run a gui application over ssh. To see which application try connecting with "ssh -XY HOST" if you are on a linux machine. Then you should see an application launching.

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