树的孩子兄弟表示法在打印时不成功,但调试时说明建树没有出问题,请帮忙指正,谢谢!
#include "iostream.h"
typedef struct node
{
char data;
struct node *lchild; //指向最左孩子结点
struct node *rsibling; //指向右邻兄弟结点
}CSNode;
CSNode *create();
void main()
{
CSNode *t,*p,*r,*q;
int choice;
r=t=NULL;
cout<<"1.输入根结点\n";
cout<<"2.输入孩子结点(输入'#'结束)\n";
cout<<"3.输入兄弟结点(输入'#'结束)\n";
cout<<"4.打印根结点的孩子结点\n";
cout<<"5.退出\n";
do
{
cout<<"请输入操作:";
cin>>choice;
if(choice>5||choice<1)
{
cout<<"请重新输入:";
continue;
}
switch(choice)
{
case 1:
if(t==NULL)
r=t=create();
break;
case 2:
if(t==NULL)
{
cout<<"根结点未输入!";
break;
}
while(1)
{
p=new CSNode;
cout<<"请输入孩子结点(输入'#'结束)";
cin>>p->data;
if(p->data=='#')
break;
t->lchild=p;
t=p;
}
break;
case 3:
if(t==NULL)
{
cout<<"根结点未输入!";
break;
}
while(1)
{
p=new CSNode;
cout<<"请输入兄弟结点(输入'#'结束)";
cin>>p->data;
if(p->data=='#')
break;
t->rsibling=p;
t=p;
}
break;
case 4:
q=r->lchild;
while(q!=NULL)
{
cout<<q->data<<" ";
q=q->rsibling;
}
break;
default:
return;
}
}while(1);
}
CSNode *create()
{
CSNode *c;
c=new CSNode;
cout<<"请输入根结点:";
cin>>c->data;
c->rsibling=NULL;
return c;
}