Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ LinkedList ADT指针块_C++_List_Pointers_Tree_Nodes - Fatal编程技术网

C++ LinkedList ADT指针块

C++ LinkedList ADT指针块,c++,list,pointers,tree,nodes,C++,List,Pointers,Tree,Nodes,我正在为我的compsci类实现许多LinkedList ADT,我在每一个上都遇到了相同的问题。下面列出的代码是一个二叉树ADT。编译器在尝试将数据输入到新节点时丢失。代码编译时没有任何错误,但是编译器没有返回任何内容,我认为它在尝试查找指针时遇到了问题。我来自Java,所以我仍在围绕指针工作 #include <iostream> struct TreeNode { //represents a single node in a binary tree of int data

我正在为我的compsci类实现许多LinkedList ADT,我在每一个上都遇到了相同的问题。下面列出的代码是一个二叉树ADT。编译器在尝试将数据输入到新节点时丢失。代码编译时没有任何错误,但是编译器没有返回任何内容,我认为它在尝试查找指针时遇到了问题。我来自Java,所以我仍在围绕指针工作

#include <iostream>
struct TreeNode {
  //represents a single node in a binary tree of int data
  int data; //immediate data
  TreeNode *left; //left subtree
  TreeNode *right; //right subtree
  TreeNode(int in);
};

TreeNode::TreeNode(int in) {
  data = in;
  left = NULL;
  right = NULL;
}
#包括
树状结构{
//表示整数数据二叉树中的单个节点
int data;//立即数据
TreeNode*left;//左子树
TreeNode*right;//右子树
TreeNode(int-in);
};
TreeNode::TreeNode(int-in){
数据=英寸;
左=空;
右=空;
}
编译器似乎找不到这两个append函数中引用的指针

void addLeft(TreeNode *root, int newData) {
  TreeNode *new_node;
  new_node->data = newData;
  root->left = new_node;
}
void addRight(TreeNode *root, int newData) {
  TreeNode *new_node;
  new_node->data = newData;
  root->right = new_node;
}
//counts nodes in binary tree from designated root point
int countNodes(TreeNode *root) {
  if (!root) {
    return 0; //empty tree
  }
  int count = 1;
  count += countNodes(root->left); //adds left subtree nodes
  count += countNodes(root->right); //adds right subtree countNodes
  return count;
}
void preorderPrint(TreeNode *root) { //root first, then left, then right
  if (root) {
    std::cout << root->data << " ";
    preorderPrint(root->left);
    preorderPrint(root->right);
  }
}

void postorderPrint(TreeNode *root) { //left first, then right, then root
  if (root) {
    postorderPrint(root->left);
    postorderPrint(root->right);
    std::cout << root->data << " ";
  }
}
void inorderPrint(TreeNode *root) { //left first, then root, then right
  if (root) {
    inorderPrint(root->left);
    std::cout << root->data << " ";
    inorderPrint(root->right);
  }
}
bool tree_contains(TreeNode *root, int item) {
  if (!root) {
    return false; //if the root doesn't exist, the tree doesn't exist
  }
  else if (root->data = item) {
    return true; //item is found in the root node
  }
  else if (root->data > item) {

  }
}

int main() {
  TreeNode *root;
  root->data = 5;
  addLeft(root, 4);
  addRight(root,9);
  inorderPrint(root);
  return 0;
}
void addLeft(TreeNode*root,int newData){
树节点*新节点;
新建节点->数据=新建数据;
根->左=新节点;
}
void addRight(TreeNode*root,int newData){
树节点*新节点;
新建节点->数据=新建数据;
根->右=新节点;
}
//从指定的根点对二叉树中的节点进行计数
int countNodes(树节点*根节点){
如果(!root){
返回0;//空树
}
整数计数=1;
count+=countNodes(根->左);//添加左子树节点
count+=countNodes(根->右);//添加右子树countNodes
返回计数;
}
void preorderPrint(TreeNode*root){//首先是root,然后是left,然后是right
如果(根){
std::cout数据(左);
预订单打印(根->右);
}
}
void postorderPrint(TreeNode*root){//先左,后右,再根
如果(根){
postorderPrint(根->左);
postorderPrint(根目录->右侧);
std::cout数据(左);
std::cout数据权限);
}
}
布尔树包含(树节点*根,int项){
如果(!root){
返回false;//如果根不存在,则树不存在
}
else if(根->数据=项){
return true;//在根节点中找到项
}
else if(根->数据>项){
}
}
int main(){
树根;
根->数据=5;
addLeft(根,4);
addRight(根,9);
inorderPrint(根);
返回0;
}

您的
根目录未初始化。它当前有一个未定义的值。应该是:

TreeNode *root = new TreeNode(5);
... // Do whatever you want
// delete root and everything else.

指针只是一个变量,它在内存中保存对象的地址。当您定义一个指针时,如

int *foo;
您尚未初始化它,因此其值不确定。这意味着它没有可用于访问内存中对象的有效指针值。要使指针实际指向某个对象,必须为其分配一个地址:

int bar;
inf *foo = &bar;
现在
foo
保存
bar
的地址,您可以取消对
foo
的引用以写入
bar

*foo = 42;
// bar is now 42
在代码中

您尝试取消引用(
root->data
只是
(*root.data
)一个指针
root
,该指针未初始化或未分配有效的指针值

由于要创建随需增长的动态数据结构,所以需要在运行时分配内存。您可以使用
new
操作符执行此操作:

TreeNode *root = new TreeNode;           // allocates an object of the type                                             
                                         // TreeNode
root->data = 5;  // is now safe.
但是,由于您为
TreeNode
提供了一个构造函数,该构造函数接受
int
,因此您可以编写:

TreeNode *root = new TreeNode{ 5 };
代码中的许多其他位置也是如此

请记住,当不再需要动态分配的内存时,应将其释放:

`delete root;`

它不是“编译器”。最好说“我的代码找不到指针”,或者“函数找不到指针”,等等。Q:你的调试器是什么?问:你能在调试器中一步一步地检查代码以确定问题吗?GNU调试器。我发现我必须用输入数据初始化我的新节点,而不是访问它们的数据,这有助于解决这个问题assignment@seaneezy因此,您的代码远不是“固定的”。见(三、五、零法则)
`delete root;`