#include<iostream>
using namespace std;
#include<stdlib.h>
#include<queue>
#define MaxInt 327167
#define MVNum 100
#define OK 1
typedef char VerTexType;//点类型
typedef int ArcType; //边类型
typedef int Status;
typedef struct{
VerTexType vexs[MVNum]; //点的一维数组
ArcType arcs[MVNum][MVNum]; //邻接矩阵
int vexnum,arcnum; //点,边实际个数
}AMGraph;
void menu();
Status CreateUDN(AMGraph &G);
Status LocateVex(AMGraph G,char n);
void DFS_AM(AMGraph G,int v);
void BFS_AM(AMGraph G,int v);
bool visited[MVNum];
int main(){
int choice;
char start;
AMGraph G;
while(true){
menu();
cout<<"请输入要选择的功能:";
cin>>choice;
switch(choice){
case 1:
CreateUDN(G);
system("pause");
break;
case 2:
cout<<"请输入从第几个点开始:";
cin>>start;
for(int i=0;i<G.vexnum;i++)
visited[i]=0;
DFS_AM(G,start);
system("pause");
break;
case 3:
cout<<"请输入从第几个点开始:";
cin>>start;
for(int i=0;i<G.vexnum;i++)
visited[i]=0;
BFS_AM(G,start);
system("pause");
break;
}
}
}
void menu(){
system("cls");
cout<<"1.创建图(邻接表)"<<endl;
cout<<"2.遍历图(深度优先搜索)"<<endl;
cout<<"3.遍历图(广度优先搜索)"<<endl;
}
Status CreateUDN(AMGraph &G){
cout<<"请输入顶点数和边数:";
cin>>G.vexnum>>G.arcnum;
cout<<"请输入点:";
for(int i=0;i<G.vexnum;i++){
cin>>G.vexs[i];
}
for(int i=0;i<G.vexnum;i++)
for(int j=0;j<G.vexnum;j++)//邻接矩阵 初始化
G.arcs[i][j]=MaxInt;
char v1,v2;
int w,i,j;
for(int k=0;k<G.arcnum;k++){
cout<<"请输入两个顶点及中间的权值:";
cin>>v1>>v2>>w;//输入两个顶点及中间的权值
i=LocateVex(G,v1);
j=LocateVex(G,v2);
G.arcs[i][j]=w;
G.arcs[j][i]=G.arcs[i][j];//无向图
}
cout<<"创建成功";
return OK;
}
Status LocateVex(AMGraph G,char n){
for(int i=0;i<G.vexnum;i++)
if(n==G.vexs[i])
return i;
}
void DFS_AM(AMGraph G,int v){
cout<<G.vexs[v]<<" ";
visited[v]=1;
for(int w=0;w<G.vexnum;w++)
if((G.arcs[v][w]!=MaxInt)&&(!visited[w]))
DFS_AM(G,w);
}
void BFS_AM(AMGraph G,int v){
std::queue<int>Q;
int i;
if(visited[v]!=1){
cout<<G.vexs[v]<<" ";
visited[v]=true;
Q.push(v);}
while(!Q.empty()){
i=Q.front();
Q.pop();
for(int j=0;j<G.vexnum;j++){
if(G.arcs[i][j]!=MaxInt&&visited[j]!=1){
cout<<G.vexs[j]<<" ";
visited[j]=1;
Q.push(j);
}
}
}
}
广度优先搜索到底哪里错了,求大神解释!