C++ 如何将节点值从一个数据结构复制到另一个数据结构

C++ 如何将节点值从一个数据结构复制到另一个数据结构,c++,C++,具体的任务是,我必须从二叉树复制整数值,并用这些数值创建一个链表。我遇到的问题是,当我遍历二叉树得到每个节点上的每个整数时,我无法将节点的值发送给链表分配函数 // Setting up the data structures: struct node // node for the binary tree: { int data; // variable to store each integer value in the tree node * left; // pointe

具体的任务是,我必须从二叉树复制整数值,并用这些数值创建一个链表。我遇到的问题是,当我遍历二叉树得到每个节点上的每个整数时,我无法将节点的值发送给链表分配函数

// Setting up the data structures:
struct node // node for the binary tree:
{
    int data; // variable to store each integer value in the tree
    node * left; // pointer for each right node
    node * right; // pointer for each left node
};
struct list_node // node for the linked list
{
    int list_data;
    list_node* next;
};

// Function for linked list insertion:
list_node * appendList(list_node *current, int newData)
{
    list_node *newNode = new list_node();
    newNode->list_data = newData;
    newNode->next = NULL; // now end of list
    current->next = newNode;
    return newNode;
}

void traverse(node* root)
{
    if (root == NULL)
    {
        return;
    }
    traverse(root->left); // first recursive
    // then send these values to the linked list
    //cout << root->data << " ";
    appendList(root, root->data);
    traverse(root->right); // second recusive
}
此电话:

appendList(root, root->data);
接受一个
list\u节点
指针,但您给它一个
节点
指针

换句话说,您正试图将二叉树的节点用作列表节点,这并不是您真正想要做的。尝试使用列表结构的根,而不是二进制结构。

在下面的函数调用中:
In below function call:
 appendList(root, root->data);

You are passing the incorrect first parameter i.e. root which is node of binary tree i.e. 'struct node' not 'struct list_node'.

**Solution:**
You have to pass the root of the list i.e. object 'struct list_node' as first parameter in function appendList.

**CHANGES**

struct list_node // node for the linked list
{
    int list_data;
    list_node* next;
};

// Function for linked list insertion:
list_node * appendList(list_node *root, int newData)
{
    list_node *newNode = new list_node();
    newNode->list_data = newData;
    newNode->next = NULL; // now end of list
    current->next = newNode;
    return newNode;
}

void traverse(node* root, list_node* rootList)
{
    if (root == NULL)
    {
        return;
    }
    traverse(root->left, rootList); // first recursive
    // then send these values to the linked list
    //cout << root->data << " ";
    rootList = appendList(rootList, root->data);
    traverse(root->right, rootList); // second recusive
}
附录列表(根,根->数据); 您传递的第一个参数不正确,即root,它是二叉树的节点,即“struct node”不是“struct list_node”。 **解决方案:** 必须将列表的根,即对象“struct list_node”作为函数appendList中的第一个参数传递。 **变化** 结构列表\节点//链接列表的节点 { int list_数据; 列出_节点*下一步; }; //用于插入链表的函数: 列表节点*附录列表(列表节点*根,int newData) { 列表_节点*新节点=新列表_节点(); newNode->list_data=newData; newNode->next=NULL;//现在列表结束 当前->下一步=新节点; 返回newNode; } 无效遍历(节点*根,列表\节点*根列表) { if(root==NULL) { 返回; } 遍历(根->左,根列表);//第一次递归 //然后将这些值发送到链接列表 //数据); 遍历(根->右,根列表);//第二次递归 }
好的,这会告诉我该朝哪个方向走。这也意味着我不能指向已经被指向的内存位置。若节点指向某个地方,那个么节点列表就不能指向它?没错。如果列表没有根节点,则必须分配一个。将它们视为完全独立的对象。
In below function call:
 appendList(root, root->data);

You are passing the incorrect first parameter i.e. root which is node of binary tree i.e. 'struct node' not 'struct list_node'.

**Solution:**
You have to pass the root of the list i.e. object 'struct list_node' as first parameter in function appendList.

**CHANGES**

struct list_node // node for the linked list
{
    int list_data;
    list_node* next;
};

// Function for linked list insertion:
list_node * appendList(list_node *root, int newData)
{
    list_node *newNode = new list_node();
    newNode->list_data = newData;
    newNode->next = NULL; // now end of list
    current->next = newNode;
    return newNode;
}

void traverse(node* root, list_node* rootList)
{
    if (root == NULL)
    {
        return;
    }
    traverse(root->left, rootList); // first recursive
    // then send these values to the linked list
    //cout << root->data << " ";
    rootList = appendList(rootList, root->data);
    traverse(root->right, rootList); // second recusive
}