| C++关于指针问题
|
减小字体
增大字体
|
问题:题目:在二叉树中删除以x为根的子树。#include <iostream.h>class Bitree{public:char data;Bitree *Lchild,*Rchild;Bitree(){int judge;cout<<输入数据<<endl;cin>>data;cout<<输入1建立左子树<<endl;cin>>judge;if(judge==1)Lchild=new Bitree;else Lchild=NULL;cout<<输入1建立右子树<<endl;cin>>judge;if(judge==1)Rchild=new Bitree;else Rchild=NULL;return;}};int Delete(Bitree *a,char b){if(a->data==b)return 1;if(a->Lchild!=NULL){if(Delete(a->Lchild,b))a->Lchild=NULL;}if(a->Rchild!=NULL){if(Delete(a->Rchild,b))a->Rchild=NULL;}return 0;}void Display(Bitree *a){cout<<a->data<<endl;if(a->Lchild!=NULL){cout<<左子树 <<endl;Display(a->Lchild);}else cout<<左子树空<<endl;if(a->Rchild!=NULL){cout<<右子树<<endl;Display(a->Rchild);}else cout<<右子树空<<endl;}void main(){char k;Bitree *root;cout<<删除数据<<endl;cin>>k;Delete(root,k);Display(root);cin.get();}在编译时是没有问题的,但是运行时出现Loaded ntdll.dll, no matching symbolic information found.Loaded C:\WINDOWS\system32\kernel32.dll, no matching symbolic information found.First-chance exception in Cpp1.exe: 0xC0000005: Access Violation.同时有黄色指针指在Delete()第一个if语句上,我想大概是指针问题,但是不知道是哪里错了。各位牛人帮帮忙。
因为你使用指针,必须为指针开空间,而且在开空间时调用构造函数,才能进行初始话。 void main() { char k; Bitree *root; root = new Bitree; //加上这句话 cout<<"删除数据"<<endl; cin>>k; Delete(root,k); Display(root); cin.get(); } 如果你对题目:在二叉树中删除以x为根的子树。#include <iostream.h>class Bitree{public:char data;Bitree *Lchild,*Rchild;Bitree(){int judge;cout<<输入数据<<endl;cin>>data;cout<<输入1建立左子树<<endl;cin>>judge;if(judge==1)Lchild=new Bitree;else Lchild=NULL;cout<<输入1建立右子树<<endl;cin>>judge;if(judge==1)Rchild=new Bitree;else Rchild=NULL;return;}};int Delete(Bitree *a,char b){if(a->data==b)return 1;if(a->Lchild!=NULL){if(Delete(a->Lchild,b))a->Lchild=NULL;}if(a->Rchild!=NULL){if(Delete(a->Rchild,b))a->Rchild=NULL;}return 0;}void Display(Bitree *a){cout<<a->data<<endl;if(a->Lchild!=NULL){cout<<左子树 <<endl;Display(a->Lchild);}else cout<<左子树空<<endl;if(a->Rchild!=NULL){cout<<右子树<<endl;Display(a->Rchild);}else cout<<右子树空<<endl;}void main(){char k;Bitree *root;cout<<删除数据<<endl;cin>>k;Delete(root,k);Display(root);cin.get();}在编译时是没有问题的,但是运行时出现Loaded ntdll.dll, no matching symbolic information found.Loaded C:\WINDOWS\system32\kernel32.dll, no matching symbolic information found.First-chance exception in Cpp1.exe: 0xC0000005: Access Violation.同时有黄色指针指在Delete()第一个if语句上,我想大概是指针问题,但是不知道是哪里错了。各位牛人帮帮忙。这个问题有好的意见或
建议,请留言
|
|
[]
[返回上一页]
[打 印]
|
|
|