#include <iostream>
#include <stdio.h>
#include <list>
#include <iterator>
#define MAX_VERTEX_NUM 100
int visited[MAX_VERTEX_NUM];
using namespace std;
typedef char v_type; //定点的数据类型
typedef struct ArcNode {
int adjvex;
struct ArcNode *nextarc;
int weight;
}ArcNode;
typedef struct VNode { //边结点
v_type data;
ArcNode *firstarc;
}VNode,AdjList[MAX_VERTEX_NUM];
typedef struct {
AdjList vexs;
int vexnum,arcnum;
}ALGraph;
//-----------------判断函数-------------------------
//用来选择是否是有向图
bool DirectedorNot() {
char d;
cout << "Is your graph directed or not?(Y/N):" ;
A:
cin >> d;
if (d=='Y') return true;
else if (d=='N') return false;
else {
cout << "Please input correctly!" << endl ;
goto A;
}
}
//------------------位置确定函数---------------------
//用来确定u所处的位置
int LocateVex(ALGraph G, v_type u) {
int i;
for (i=0; i<G.vexnum; i++) {
if (u==G.vexs[i].data) return i;
}
if (i==G.vexnum) {
cout << "Error!" ;
exit (1);
}
}
//------------------图的构造函数------------------------
void CreatALGraph(ALGraph &G) {
int i,j,w,k;
v_type v1,v2;
ArcNode *p;
bool flag = DirectedorNot();
cout << "Input vexnum & arcnum:" ;
cin >> G.vexnum >> G.arcnum ;
cout << "Input vertices" ;
for (i=0; i<G.vexnum; i++) { //初始化边结点
cin >> G.vexs[i].data;
G.vexs[i].firstarc = NULL;
}
for (k=0; k<G.arcnum; k++) { //输入两定点和他们之间弧的权
cout << "Input arcs(v1, v2 & w):" ;
cin >> v1 >> v2 >> w ;
i = LocateVex(G, v1);
j = LocateVex(G, v2);
p = (ArcNode*)malloc(sizeof(ArcNode));
p->adjvex = j;
p->weight = w;
p->nextarc = G.vexs[i].firstarc;
G.vexs[i].firstarc = p;
if (!flag) { //如果是无向图,执行此步骤
p=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=i;
p->weight = w;
p->nextarc=G.vexs[j].firstarc;
G.vexs[j].firstarc=p;
}
}
}
//-------------------深度优先遍历----------------------
void DFS(ALGraph G, int v) { //v代表从第v个顶点开始遍历
ArcNode *p;
cout << G.vexs[v].data << ' ' ;
visited[v] = 1;
p = G.vexs[v].firstarc;
while (p) {
if (!visited[p->adjvex]) DFS(G, p->adjvex);
p = p->nextarc;
}
}
void DFSTraverse(ALGraph G) { //防止非连通图不能全部遍历到
int i;
for (i=0; i<G.vexnum; i++) {
visited[i] = 0;
}
for (i=0; i<G.vexnum; i++) {
if (!visited[i]) DFS(G, i);
}
}
//--------------------广度优先遍历-----------------------
void BFS(ALGraph G, int v) { //v代表从第v个定点开始遍历
list<int> v_list; //定义一个链表
list<int>::iterator iter_front;
ArcNode *p;
cout << G.vexs[v].data << ' ' ;
visited[v] = 1;
v_list.push_back(v);
while (!v_list.empty()) {
iter_front = v_list.begin(); //先把值赋给迭代器,然后通过迭代器提领传值给v
v = *iter_front;
v_list.pop_front();
p = G.vexs[v].firstarc;
while (p) {
if (!visited[p->adjvex]) {
cout << G.vexs[p->adjvex].data << ' ' ;
visited[p->adjvex] = 1;
v_list.push_back(p->adjvex);
}
p = p->nextarc;
}
}
}
void BFSTraverse(ALGraph G) {
int i;
for (i=0; i<G.vexnum; i++) {
visited[i] = 0;
}
for (i=0; i<G.vexnum; i++) {
if (!visited[i]) BFS(G, i);
}
}
//---------------------主函数-----------------------------
int main() {
ALGraph G;
CreatALGraph(G);
cout << "深度优先遍历:" << endl;
DFSTraverse(G);
cout << endl;
cout << "广度优先遍历:" << endl;
BFSTraverse(G);
cout << endl;
system("pause");
return 0;
}