如果将其添加为全局结构,则如何将其添加为NULL

How do you add a value to a global struct if it is initialized as NULL?

本文关键字:添加 NULL 全局 如果 结构      更新时间:2023-10-16

通常,我只会在main中输入 head = new Node,这将设置所有内容,但规定是我无权与全局变量混乱。这是我只能访问MAIN的任务,并且由于其他后端功能,我必须完整地将全局变量留下,以便我无法用head = new Node覆盖它。

重点只是将字符添加到链接列表中。我只是用硬编码为例,但我仍然无法避免错误。

是否有正确的添加方法?

struct Node{
  char key;
  Node *next;
};
Node *head = NULL;
int main(){
    char x = 'a';
    cout<<x<<endl;
    head->key=x;
}

分配:在给定值范围之间找到BST中的所有节点。然后构建值的链接列表,列表应按上升顺序。

注意:链接列表的头部在后端在全球声明,其初始值为null。只需使用头部将节点添加到链接列表中。链接列表的打印也将在后端进行。可以使用辅助功能。

void rangesearch(treenode *node,char m,char n);

头只是指向NULL的指针。没有为节点分配的真实对象/内存。您首先要为其分配内存。

在您的作业中,您可以并且应该(据我所知)将节点添加到链接列表中,因此您必须分配新节点。

    struct Node {
    char key;
    Node *next;
};
Node *head = NULL;
int main() {
    char x = 'a';
    head = new Node();
    cout << x << endl;
    head->key = x;
    delete head;
    return 0;
}