问题:用VC2010学习版直接运行下面的循环时,会在循环两次后自动跳出,然后程序就结束了。
while(key!=1)
{
T=InsertBST(T,key);
scanf_s("%d",&key);
}
但当在调试时运行,可以正常运行,可控的跳出。
换成这样编码时,也会在循环两次后出错。
for(int i=0;i<n;i++)
{
scanf_s("%d",&key);
T=InsertBST(T,key);
}
但用VC6直接运行上面的代码也是可控的跳出,并不会自己跳出。
完整代码
#include<stdio.h>
#include<conio.h>
//#include<stdlib.h>
#include<Windows.h>
typedef struct BSTNode
{
int data;
BSTNode *lchild;
BSTNode *rchild;
}BSTNode,*BSTree;
BSTree InsertBST(BSTree T,int key)
{
BSTNode *p,*q=T;
p=(BSTNode *)malloc(sizeof(BSTree));
p->data=key;
p->lchild=NULL;
p->rchild=NULL;
if(T==NULL)
T=p;
else
{
if(q->data<key)
{
if(q->rchild==NULL)
q->rchild=p;
else
InsertBST(q->rchild,key);
}
else if(q->data>key)
{
if(q->lchild==NULL)
q->lchild=p;
else
InsertBST(q->lchild,key);
}
else
printf("有相同元素输入,请重新输入正确元素");
}
return T;
}
BSTree CreatBST(BSTree T)
{
int key;
scanf_s("%d",&key);
while(key!=1)
{
T=InsertBST(T,key);
scanf_s("%d",&key);
}
return T;
}
void PrintBST(BSTree T)
{
if(!T)
return ;
PrintBST(T->lchild);
printf("%d\n",T->data);
PrintBST(T->rchild);
}
void Preface() //前言
{
printf("二叉树数集元素判定\n");
printf("完成时间:\n");
printf("按任意键继续\n");
}
void main()
{
BSTree A,B;
A=NULL;
B=NULL;
Preface();
getch();
system("cls");
A=CreatBST(A);
PrintBST(A);
return ;
}