#include <iostream>
using namespace std;
struct childbro
{
struct childbro *child;
int data;
struct childbro *bro;
};
struct childbro *init(struct childbro *head)
{
head=new(struct childbro);
struct childbro *a=head;
struct childbro *b=new(struct childbro);
struct childbro *c=new(struct childbro);
struct childbro *d=new(struct childbro);
struct childbro *e=new(struct childbro);
struct childbro *f=new(struct childbro);
struct childbro *g=new(struct childbro);
struct childbro *h=new(struct childbro);
struct childbro *i=new(struct childbro);
//节点A//
a->bro=NULL;
a->child=b;
cout<<"请输入A节点的数据:";
cin>>a->data;
b->bro=c;
b->child=d;
cout<<"请输入B节点的数据:";
cin>>b->data;
c->bro=NULL;
c->child=g;
cout<<"请输入C节点的数据:";
cin>>c->data;
d->bro=e;
d->child=NULL;
cout<<"请输入D节点的数据:";
cin>>d->data;
e->bro=f;
e->child=i;
cout<<"请输入E节点的数据:";
cin>>e->data;
f->bro=NULL;
f->child=NULL;
cout<<"请输入F节点的数据:";
cin>>f->data;
g->bro=h;
g->child=NULL;
cout<<"请输入G节点的数据:";
cin>>g->data;
h->bro=NULL;
h->child=NULL;
cout<<"请输入H节点的数据:";
cin>>h->data;
i->bro=NULL;
i->child=NULL;
cout<<"请输入I节点的数据:";
cin>>i->data;
return head;
}
int main()
{
struct childbro *head=NULL;
head=init(head);
cout<<"输出A节点和G节点的数据:";
cout<<head->data<<" "<<head->child->bro->child->data;
return 0;
}
数据结构 树的存储,兄弟孩子法
最新推荐文章于 2024-06-13 23:56:26 发布