【发布时间】:2017-11-26 17:35:02
【问题描述】:
当我遇到这个问题时,我正在尝试编写二进制排序树: 指针有时不为 NULL。 本题中node2的left-child为NULL。但是当指针与NULL比较时,结果显示不是NULL。
Visual Studio 2015 提出了一个突破: 引发异常:
read access violation.
T is 0xCDCDCDCD.
If applicable to this exception handler, the program can safely continue to run.
那么如何编写 if 语句以使显示可用。 谢谢。
#include <stdio.h>
#define TRUE 1
#define FALSE -1
#define NULL 0
typedef struct Node {
int data;
struct Node* lchild, *rchild;
}BNode, *BTree;
int Display(BTree T) {
if (T != NULL ) {
Display(T->lchild);
printf("%d",T->data);
Display(T->rchild);
return TRUE;
}
else if(NULL == T){
return FALSE;
}
}
int main() {
BTree node2 = (BTree)malloc(sizeof(struct Node));
BTree node5 = (BTree)malloc(sizeof(struct Node));
BTree node7 = (BTree)malloc(sizeof(struct Node));
BTree node6 = (BTree)malloc(sizeof(struct Node));
BTree node9 = (BTree)malloc(sizeof(struct Node));
node2->data = 2;
node5->data = 5;
node7->data = 7;
node6->data = 6;
node9->data = 9;
node5->lchild = node2;
node5->rchild = node7;
node7->lchild = node6;
node7->rchild = node9;
BTree root = node5;
Display(root);
system("pause");
}
【问题讨论】:
标签: c pointers data-structures binary-search-tree