|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
深度遍历没有错,单是广度遍历就会停止运行
#include<iostream>
#include<string>
using namespace std;
const maxsize=100;//图中顶点最大个数
int visited1[maxsize];//
struct WaiterBNode //边结点
{
int adjvex; //存放邻接点在顶点向量中的下标
struct WaiterBNode *next;//指向下一邻接点
};
struct WaiterNode //顶点结点,存放服务员信息
{
int id; //编号
string name;//姓名
string sex;//性别
int age;//年龄
int sal; //工资
int level; //服务星级
struct WaiterBNode *firstedge; //指向第一个邻接点的指针
};
class Waiter
{
public:
Waiter(){}
void creatWaiter();
void DFSTreverse(int v);//深度遍历
void BFSTreverse(int v);//广度遍历
int look(int ids);
private:
struct WaiterNode adj[maxsize]; //邻接表,结构体一维数组
int bnum,dnum;//边的数目,顶点数目
};
int Waiter::look(int ids)
{ int i;
for(i=0;i<dnum;i++)
if(adj[i].id==ids)
return i;
return -1;
}
void Waiter::creatWaiter()
{
int i,j,k;
int id1,id2;
cout<<"请输入你要创建顶点个数:";
cin>>dnum;
cout<<"请输入各个顶点信息:"<<endl;
for(i=0;i<dnum;i++)
{
cout<<"请输入"<<i+1<<"个服务员的信息:"<<endl;
cout<<"编号"<<endl;
cin>>adj[i].id;
cout<<"姓名"<<endl;
cin>>adj[i].name;
cout<<"性别"<<endl;
cin>>adj[i].sex;
cout<<"年龄"<<endl;
cin>>adj[i].age;
cout<<"工资"<<endl;
cin>>adj[i].sal;
cout<<"服务星级"<<endl;
cin>>adj[i].level;
adj[i].firstedge=NULL;
}
cout<<"请输入你要创建链表的边数:";
cin>>bnum;
for(k=0;k<bnum;k++)
{
cout<<"请输入以第"<<k+1<<"个服务员作为起点的服务员编号:";
cin>>id1;
cout<<"作为终点的服务员编号:";
cin>>id2;
i=look(id1);
j=look(id2);
struct WaiterBNode *s=new struct WaiterBNode;
s->adjvex=j;
s->next=adj[i].firstedge;
adj[i].firstedge=s;
s=new struct WaiterBNode;
s->adjvex=i;
s->next=adj[j].firstedge;
adj[j].firstedge=s;
}
}
void Waiter::DFSTreverse(int v)//深度遍历
{
int a;
if(v>dnum) throw"你要遍历的位置异常";
struct WaiterBNode *p;
cout<<"("<<adj[v].id<<" "<<adj[v].name<<" "<<adj[v].sex<<" "<<adj[v].age<<" "<<adj[v].sal<<" "<<adj[v].level<<endl;
visited1[v]=1;
p=adj[v].firstedge;
while(p!=NULL)
{a=p->adjvex;
if(visited1[a]==0) DFSTreverse(a);
p=p->next;
}
}
void Waiter::BFSTreverse(int v)//广度遍历
{
if(v>dnum) throw"你要遍历的位置异常";
int front,rear,i;
int adj1[maxsize];
struct WaiterBNode *p;
front=rear=-1;
cout<<"("<<adj[i].id<<" "<<adj[i].name<<" "<<adj[i].sex<<" "<<adj[i].age<<" "<<adj[i].sal<<" "<<adj[i].level<<")"<<endl;
visited1[v]=1;
adj1[++rear]=v;
while(front!=rear)
{ v=adj1[++front];
p=adj[v].firstedge;
while(p!=NULL)
{ i=p->adjvex;
if(visited1[i]==0)
{
cout<<"("<<adj[i].id<<" "<<adj[i].name<<" "<<adj[i].sex<<" "<<adj[i].age<<" "<<adj[i].sal<<" "<<adj[i].level<<")"<<endl;
visited1[i]=1;
adj1[++rear]=i;
}
p=p->next;
}
}
}
void main()
{
int i,choice,k1,k2;
int id3;
Waiter doc;
while(choice!=4)
{ cout<<"***************欢迎使用Waiter图结构数据处理程序*************************"<<endl;
cout<<"请选择以下操作:"<<endl;
cout<<"**********************【1】创建Waiter图*********************************"<<endl;
cout<<"**********************【2】按照深度遍历的方式打印所有的Waiter***********"<<endl;
cout<<"**********************【3】按照广度遍历的方式打印所有的Waiter***********"<<endl;
cout<<"**********************【4】退 出*********************************"<<endl;
cout<<"请输入你的选择:";
cin>>choice;
switch(choice)
{
case 1:cout<<"请开始创建Waiter图结构:"<<endl;
doc.creatWaiter();
cout<<"**************创建成功***************"<<endl;
cout<<endl;
break;
case 2:
for(i=0;i<maxsize;i++)
{
visited1[i]=0;
}
cout<<"请输入你要开始深度遍历的作为起始点的服务员编号:";
cin>>id3;
k1=doc.look(id3);
cout<<"从第"<<k1+1<<"个服务员开始的深度优先遍历图为:"<<endl;
doc.DFSTreverse(k1);
cout<<"******************完成遍历**************"<<endl;
cout<<endl;
break;
case 3:
for(i=0;i<maxsize;i++)
visited1[i]=0;
cout<<"请输入你要开始广度遍历的作为起始点的服务员编号:";
cin>>id3;
k2=doc.look(id3);
cout<<"从第"<<k2+1<<"个服务员开始的深度优先遍历图为:"<<endl;
doc.BFSTreverse(k2);
cout<<"******************完成遍历**************"<<endl;
cout<<endl;
break;
case 4:break;
}
}
}
|
|