我遇到了传递结构指针的问题

时间:2016-02-28 07:35:17

标签: c++ data-structures

我是家庭作业的问题。我们正在研究二叉树,每当我尝试将头节点传递给insert()函数时,节点就不会被更改。这让我相信我在某种程度上没有通过引用传递,但我无法弄清楚错误在哪里。感谢您提前提供任何帮助。

/*Linked List
**Code inspired by Linked List by Daniel Ross
**Code written by Collin Bardini
**Assignment 6
*/

#include <iostream>
using namespace std;

//Our node
struct node {
    int data;
    node* left; //lower
    node* right; //greater
};

//function declarations
void insert(node * head, int);
void print_preorder(node * root);
void print_postorder(node * root);
void print_inorder(node * root);
int search(int data, node * root);

//main for testing the access functions
void main(void)
{
    node* headA = 0;
    node* headB = 0;
    const size_t as = 7;
    const size_t bs = 100;
    int a[as] = {1,5,4,6,7,2,3};
    int b[bs] = {118,119,158,166,163,123,108,116,117,184,165,137,141,111,138,122,109,194,143,183,178,173,139,
        126,170,190,140,188,120,195,113,104,193,181,185,198,103,182,136,115,191,144,145,155,153,151,
        112,129,199,135,146,157,176,159,196,121,105,131,154,107,110,175,187,134,132,179,133,102,172,
        106,177,171,156,168,161,149,124,189,167,174,147,148,197,160,130,164,152,142,162,150,186,169,
        127,114,192,180,101,125,128,100 };

    for (int i = 0; i < as; i++)
        insert(headA, a[i]);
    for (int i = 0; i < bs; i++)
        insert(headB, b[i]);

    print_preorder(headA);

    cout << "search 196: " << search(196, headB) << endl <<
        "search 137: " << search(137, headB) << endl <<
        "search 102: " << search(102, headB) << endl <<
        "search 190: " << search(190, headB) << endl;
}

// creates a new node and inserts it in the correct location in the tree
void insert(node * head, int d)
{
    //make a new node
    node *p = new node;
    p->right = 0;
    p->left = 0;
    p->data = d;

    if (head == 0) //list is empty
        head = p;
    else //append to tail end
    {
        node* c1 = head;
        node* c2 = head;
        while (c1)
        {
            if (d > c1->data)
            {
                c2 = c1;
                c1 = c1->right;
            }
            else
            {
                c2 = c1;
                c1 = c1->left;
            }
        }
        if (d > c2->data)
            c2->right = p;
        else
            c2->left = p;
    }
}

1 个答案:

答案 0 :(得分:0)

简短回答:查看我的answer to this identical question并在void *square_integer(void *tid) 函数中使用双指针。

关于问题的“传递参考”部分,这取决于这些词的确切含义。您通过引用传递节点,为true,但是您按值将指针传递给节点。请记住,指针只是另一个包含非负整数值的变量(过度简化,但仍然是真的)。​​

答案很长:修改函数应该使用双指针或指针引用。原因是,在像insert()这样的函数定义中,指针本身是按值传递的。也就是说,创建原始指针的临时副本,并在函数内部进行修改。实际指针保持不变。即,在这些方面:

void insert(node * head, int d) { /*...*/ }

修改临时局部变量// definition if (head == 0) //list is empty head = p; // client code for (int i = 0; i < as; i++) insert(headA, a[i]); ,而不是传递给函数的head变量。请参阅链接的问题,其中我发布了在这种情况下指针值发生的情况的示意图。 你可以改为例如像这样的双指针:

headA

或者通过引用传递指针。

注意:对于只读函数,例如void insert(node ** head, int d) { // snip... if (*head == 0) //list is empty *head = p; ,您不需要使用双指针或任何其他传递引用机制。