实现队列出错 改了一天依然没进展 求改代码

风吹草地pp凉 2012-04-15 01:06:31
这是我练习二叉树的程序 在实现用队列进行按层排序时错误 应该是队列“入列 出列”那部分的问题 求修改 以达到按层输出的效果
#include<iostream>
#define EndChar '0'
using namespace std;
typedef class tree
{
public:
tree(char data=EndChar):data(data){}

void creat(tree *&t);
void lcreat(tree *&t,char last,int d);
void rcreat(tree *&t,char last,int d);
void depth(tree *&t,int h,int &depth);
int depth2(tree *&t);
void preorder(tree *&t);
void midorder(tree *&t);
void latorder(tree *&t);
void aorder(tree *&t);
void CreateBiTree(tree *&t, const char *s1, const char *s2, int len);
void tree::recover(tree *&t, const char *pre, const char *mid, int len);
void tree::draw(tree *t);

friend int main();
private:
char data;
tree *lchild,*rchild;
}bitnode ,*bitree;
void tree::creat(bitree &t)//根所用的创建函数
{
char ch;
cout<<"输入根数据(第1层)"<<endl;
cin>>ch;
if(EndChar==ch)t=NULL;
else
{
t->data =ch;
tree::lcreat (t->lchild ,ch,1);
tree::rcreat (t->rchild,ch,1);
cout<<"二叉树创建完毕!"<<endl;
}

}
void tree::lcreat(bitree &t,char last,int d)
{
char ch;
for(int i=0;i<d;i++)cout<<" ";
cout<<"输入"<<last<<"的左孩子(第"<<++d<<"层)"<<endl;
cin>>ch;
if(EndChar==ch)t=NULL;
else
{
t=new tree(ch);
tree::lcreat (t->lchild,ch,d);
tree::rcreat (t->rchild,ch,d);
}

}
void tree::rcreat(bitree &t,char last,int d)
{
char ch;
for(int i=0;i<d;i++)cout<<" ";
cout<<"输入"<<last<<"的右孩子(第"<<++d<<"层)"<<endl;
cin>>ch;
if(EndChar==ch)t=NULL;
else
{
t=new tree(ch);
tree::lcreat (t->lchild,ch ,d);
tree::rcreat (t->rchild,ch ,d);
}

}

void tree::depth(bitree &t,int h,int &depth)
{
if(t)
{
if(h>depth)depth=h;
tree::depth (t->lchild ,h+1,depth);
tree::depth (t->rchild ,h+1,depth);
}

}
int tree::depth2(bitree &t)
{
if(!t) return 0;
else
{
int hl=depth2(t->lchild );
int hr=depth2(t->rchild );
return hl>=hr?hl+1:hr+1;
}
}
void tree::preorder(bitree &t)
{
if(t)
{
cout<<" "<<t->data;
preorder(t->lchild );
preorder(t->rchild );
}
}
void tree::midorder(bitree &t)
{
if(t)
{
midorder(t->lchild );
cout<<" "<<t->data;
midorder(t->rchild );
}
}
void tree::latorder(bitree &t)
{
if(t)
{
latorder(t->lchild );
latorder(t->rchild );
cout<<" "<<t->data;
}
}
void tree::aorder (bitree &t)
{
cout<<"先序遍历:"<<endl;t->preorder(t);
cout<<endl;cout<<"中序遍历:"<<endl;t->midorder(t);
cout<<endl;cout<<"后序遍历:"<<endl;t->latorder(t);
cout<<endl;
}

void tree::recover(bitree &t, const char *pre, const char *mid, int len)
{
if(len<=0) t=NULL;
else
{
t=new bitnode;
t->data =*pre;
int i;
for(i=0;i<len;i++) if(*pre==mid[i]) break;
tree::recover (t->lchild ,pre+1,mid,i);
tree::recover (t->rchild ,pre+i+1,mid+i+1,len-(i+1));
}

}


typedef struct node
{
bitree data;
struct node *next;

}qnode;
///**********
class linkq
{
private:
qnode *front,*rear;
public:
static int init(linkq &q);
int in(linkq &q,bitree x);
int empty(linkq &q);
int out(linkq &q,bitree &x);
friend class tree;
};
////应该是这部分出错了 起点
int linkq::init (linkq &q)
{
if((q.front=(qnode *)malloc(sizeof(qnode))) ==NULL) return 0;
q.rear =q.front ;

q.front ->next =NULL;
return 1;
}
int linkq::in(linkq &q,bitree c)
{
qnode *p;
if((p=(qnode *)malloc(sizeof(qnode))) ==NULL) return 0;
p->data =c;p->next =NULL;
q.rear ->next =p;
q.rear =p;
return 1;


}
int linkq::empty(linkq &q)
{
if(q.front ==q.rear ) return 0;//空的
else return 1;
}
int linkq::out(linkq &q,bitree &x)
{
if(!empty(q)){cout<<"空队"<<endl; return 0;}
else
{
qnode *p=q.front ->next ;
x=p->data ;
p=q.front ->next ;
q.front ->next =p->next ;

free(p);
if(q.front ->next =NULL)
q.rear=q.front ;
return 1;
}

}
void tree::draw (bitree t)
{
bitree xx;
//linkq *lq=new linkq;
linkq lq;
linkq::init (lq);
lq.in (lq,t);
lq.front ->next ;
do
{
if(lq.front ->data !=NULL)
{
lq.in (lq,lq.front ->next->data ->lchild);
lq.in (lq,lq.front ->next->data ->rchild);
}
lq.out (lq,xx);
cout<<xx->data ;

}while(lq.empty(lq));
}
////应该是这部分出错了 终止
int main()
{
cout<<"先序创建二叉树 0为结束符"<<endl;
bitree p=new bitnode;
p->creat (p);
system("cls");
int flag=1;
while(flag)
{
cout<<"\n1=遍历 2=深度 3=深度2 4先+中恢复 5=按层输出 0=退出"<<endl;
cin.sync ();
cin>>flag;cin.clear();
switch(flag)
{
case 1:p->aorder (p);break;
case 2:{int tdepth=1;p->depth (p,1,tdepth);cout<<"\n深度为"<<tdepth<<endl<<endl;break;}
case 3:cout<<p->depth2 (p)<<endl;break;
case 4: {char pre[20],mid[20]; cout<<"输入先序 中序"<<endl;cin>>pre>>mid;bitree q=new bitnode;q->recover (q,pre,mid,strlen(pre));q->aorder(q);break;}
case 5:{p->draw (p);}
case 0:exit(0);break;
default:cout<<"输入错误"<<endl;
}
}
system("pause");
return 0;
}
...全文
134 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
leihengxin 2012-04-15
  • 打赏
  • 举报
回复
写得好。
evencoming 2012-04-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

引用 1 楼 的回复:
C/C++ code



int linkq::out(linkq &amp;q,bitree &amp;x)
{
if(!empty(q)){cout<<"空队"<<endl; return 0;}
else
{
qnode *p=q.front ->next ;
x=p->data ;
p=q.front ->next ;
……
[/Quote]

int linkq::out(linkq &q,bitree &x)
{
//changed
if(empty(q)){cout<<"空队"<<endl; return 0;}
else
{
qnode *p=q.front;
x=p->data ;
q.front=p ->next;
free(p);

if(q.front ->next =NULL)
q.rear=q.front ;
return 1;
}

}


风吹草地pp凉 2012-04-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
C/C++ code



int linkq::out(linkq &q,bitree &x)
{
if(!empty(q)){cout<<"空队"<<endl; return 0;}
else
{
qnode *p=q.front ->next ;
x=p->data ;
p=q.front ->next ;
q.front ->next ……
[/Quote]
还是老问题啊
evencoming 2012-04-15
  • 打赏
  • 举报
回复


int linkq::out(linkq &q,bitree &x)
{
if(!empty(q)){cout<<"空队"<<endl; return 0;}
else
{
qnode *p=q.front ->next ;
x=p->data ;
p=q.front ->next ;
q.front ->next =p->next ;

free(p);
if(q.front ->next =NULL)
q.rear=q.front ;
return 1;
}

}

改了试试看
==>

int linkq::out(linkq &q,bitree &x)
{
if(!empty(q)){cout<<"空队"<<endl; return 0;}
else
{
qnode *p=q.front;
x=p->data ;
q.front=p ->next;
free(p);

if(q.front ->next =NULL)
q.rear=q.front ;
return 1;
}

}

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧