简体   繁体   English

打印节点数组

[英]Printing an array of nodes

Is it possible to print an array of nodes? 是否可以打印节点数组? I need to display the AVL tree as it is being built, but whenever I run this code, the program crashes. 我需要在构建时显示AVL树,但每当我运行此代码时,程序都会崩溃。 Any alternative ways around this? 有什么替代方法吗?

     int k = 0;
    t = NULL;
    node* nodearray[32];
    for( j = 0; j < 33; j++)
    {
        printf ("Table %d \n", j+1);
        printf ("LineNum Left Data Right\n");
        t = Insert(j, a[j], t );

    for (k= 0 ; k < j ; k ++)
    {
         printf ("%5d %5d %5d %5d", nodearray[k]->num, nodearray[k]->left->data, nodearray[k]->data, nodearray[k]-> right ->data);
    }
}

Problems: 问题:

  1. nodearray is uninitialized - the pointers in it have indeterminate values. nodearray未初始化 - 其中的指针具有不确定的值。 Dereferencing them invokes undefined behavior. 取消引用它们会调用未定义的行为。

  2. for( j = 0; j < 33; j++) - but you declared nodearray to be 32 elements long. for( j = 0; j < 33; j++) - 但是你将nodearray声明为32个元素。 It's hard to tell without seeing the implementation of Insert() , but probably you also have an off-by-one error (you're reading/writing past the end of the array). 没有看到Insert()的实现很难说,但是你可能还有一个一个一个错误(你正在读/写超过数组的末尾)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM