#include <iostream>
using namespace std;
#define MaxInt 32767
#define MaxNum 100
#define MaxSize 100
bool visited[MaxNum];
typedef char VertexType;
typedef int ArcType;
typedef struct
{
VertexType vexs[MaxNum];
ArcType arcs[MaxNum][MaxNum];
int vexnum, arcnum;
}MGraph;
MGraph G;
typedef struct
{
int *base;
int front, rear;
}SQueue;
void InitQueue(SQueue & Q)
{
Q.base = new int[MaxSize];
if (!Q.base)
exit(0);
Q.front = Q.rear = 0;
}
void EnQueue(SQueue &Q, int e)
{
if ((Q.rear + 1) % MaxSize == Q.front)
return ;
Q.base[Q.rear] = e;
Q.rear = (Q.rear + 1) % MaxSize;
}
void DEQueue(SQueue &Q, int &e)
{
if (Q.front == Q.rear)
return ;
e = Q.base[Q.front];
Q.front = (Q.front + 1) % MaxSize;
}
int QueueEmpty(SQueue &Q)
{
if (Q.front == Q.rear)
return 1;
else
return 0;
}
int LocateVex(MGraph G, int b)
{
for (int i = 0; i < G.vexnum; i++)
if (G.vexs[i] == b)
return i;
}
void CreateUDN(MGraph &G)
{
cout << "请输入顶点的个数:\n";
cin >> G.vexnum;
cout << "请输入边的条数:\n";
cin >> G.arcnum;
int v1, v2;
cout << "依次输入顶点的信息:\n";
for (int i = 0; i < G.vexnum; i++)
cin >> G.vexs[i];
for (int i = 0; i < G.arcnum; i++)
for (int j = 0; j < G.arcnum; j++)
G.arcs[i][j] = 0;
for (int i = 0; i < G.arcnum; i++)
{
cout << "请输入一条边依附的顶点:";
cin >> v1 >> v2;
G.arcs[v1][v2] = 1;
G.arcs[v2][v1] = 1;
}
}
void DFS(MGraph G, int v)
{
cout << G.vexs[v];
visited[v] = 1;
for (int i = 0; i < G.vexnum; i++)
if ((G.arcs[v][i] ==1) && (visited[i]==0))
DFS(G, i);
}
void BFS(MGraph G, int v)
{
int Q[MaxSize];
int front = -1, rear = -1;
cout << G.vexs[v];
visited[v] = 1;
Q[++rear] = v;
while (front != rear)
{
v = Q[++front];
for (int j = 0; j < G.vexnum; j++)
{
if (G.arcs[v][j] == 1 && visited[j] == 0)
{
cout << G.vexs[j];
visited[j] = 1;
Q[++rear] = j;
}
}
}
}
int main()
{
CreateUDN(G);
cout << "DFS:" << endl;
DFS(G, 1);
for (int a = 0; a < MaxNum; a++)
visited[a] = 0;
cout << "\nBFS:" << endl;
BFS(G, 1);
return 0;
}