#include<iostream>
#include<string.h>
#define MVnum 100
#include<string>
using namespace std;
bool visited[MVnum]; //判断状态的数组
typedef struct Information //除了下标结点的其他信息
{
char sex;
string name;
int age;
};
typedef struct ArcNode //相邻的点
{
int adjust; //点的下标
struct ArcNode *next;
};
typedef struct VNode //顶点
{
char data;
ArcNode *firstare; //相邻顶点
Information a1;
}AdjList[MVnum]; //AdjList为邻接表类型
typedef struct
{
AdjList ver;
int vernum, arcnum; //图当前的顶点数和边数、
}ALGraph;
typedef struct Stack
{
int MAX;
int *base, *next;
};
void Creat_ALG(ALGraph &G)
{
cout << "请输入结点数:";
cin >> G.vernum;
cout << "请输入边的条数:";
cin >> G.arcnum;
for (int i = 1; i <= G.vernum; ++i)
{
cout << "请输入节结点的编号:";
cin >> G.ver[i].data;
cout << "请输入姓名:";
cin >> G.ver[i].a1.name;
cout << "请输入性别:";
cin >> G.ver[i].a1.sex;
cout << "请输入年龄:";
cin >> G.ver[i].a1.age;
G.ver[i].firstare = NULL;
}
for (int i = 1; i <= G.arcnum; ++i)
{
int a, b;
cout << "请输入一条边所依附的两个顶点:";
cin >> a >> b;
ArcNode *p1;
p1 = new ArcNode;
p1->adjust = a;
p1->next = G.ver[b].firstare;
G.ver[b].firstare = p1;
ArcNode *p2;
p2 = new ArcNode;
p2->adjust = b;
p2->next = G.ver[a].firstare;
G.ver[a].firstare = p2;
}
return;
}
void Trave(ALGraph &G, int n)
{
cout << "数据:" << G.ver[n].data << endl;
cout << "姓名:" << G.ver[n].a1.name << endl;
cout << "年龄:" << G.ver[n].a1.age << endl;
cout << "性别:" << G.ver[n].a1.sex << endl << endl;;
}
void DFS_AL(ALGraph &G, int n) //深度优先遍历
{
cout << n << ":" << endl;
Trave(G, n);
visited[n] = true;
ArcNode *p;
p = G.ver[n].firstare;
while (p != NULL)
{
int w;
w = p->adjust;
//cout << w << endl;
if (!visited[w])
{
DFS_AL(G, w);
}
p = p->next;
}
return ;
}
void InitQueue(Stack &Q)
{
Q.base = new int[MVnum];
if (!Q.base) return;
Q.next = Q.base;
Q.MAX = MVnum;
return;
}
void EnQueue(Stack &Q, int n)
{
if (Q.next - Q.base == Q.MAX)
{
cout << "啊,栈满了耶~" << endl;
return;
}
*Q.next++ = n;
return;
}
bool QueueEmpty(Stack &Q)
{
if (Q.base == Q.next)
{
return 1;
}
else
{
return 0;
}
}
void DeQueue(Stack &Q, int &u)
{
if (Q.base == Q.next) {
cout << "栈满了啦~" << endl;
return;
}
u = *--Q.next;
return;
}
int First(ALGraph &G, int n)
{
int a = -1, i;
ArcNode *p;
p = G.ver[n].firstare;
if (p)
{
i = p->adjust;
if (!visited[i])
{
a = i;
}
}
return a;
}
int Next(ALGraph &G, int n)
{
int a = -1, i;
ArcNode *p;
p = G.ver[n].firstare;
for (; i <= G.vernum, p != NULL;)
{
i = p->adjust;
if (!visited[i])
{
a = i;
break;
}
else
{
p = p->next;
}
}//for
return a;
}
void BFS_AL(ALGraph &G, int n) //广度优先搜索
{
Stack Q;
visited[n] = true;
InitQueue(Q); //初始化,置空
EnQueue(Q, n); //将n压入栈
while (!QueueEmpty(Q))
{
int u=0;
DeQueue(Q, u); //将队头元素出栈
cout << u << ":" << endl;
Trave(G,u);
for (int w = First(G, u); w >= 0; w = Next(G, u))
//第一个邻接点;下一个邻接点
{
if (!visited[w])
{
visited[w] = true;
EnQueue(Q,w); //将w压入栈
} //if
} //for
}//while
}
int main()
{
ios::sync_with_stdio(false);
memset(visited, false, sizeof(visited));
ALGraph G;
Creat_ALG(G); //创建无向图
int n;
cout << "请输入第一个访问的结点的下标:";
cin >> n;
DFS_AL(G, n); //深度优先遍历
cout << "请输入第一个访问的结点的下标:";
cin >> n;
memset(visited, false, sizeof(visited));
BFS_AL(G, n); //广度优先遍历
system("pause");
return cout << "遍历结束啦啦啦~" << endl,0;
}
#include<string.h>
#define MVnum 100
#include<string>
using namespace std;
bool visited[MVnum]; //判断状态的数组
typedef struct Information //除了下标结点的其他信息
{
char sex;
string name;
int age;
};
typedef struct ArcNode //相邻的点
{
int adjust; //点的下标
struct ArcNode *next;
};
typedef struct VNode //顶点
{
char data;
ArcNode *firstare; //相邻顶点
Information a1;
}AdjList[MVnum]; //AdjList为邻接表类型
typedef struct
{
AdjList ver;
int vernum, arcnum; //图当前的顶点数和边数、
}ALGraph;
typedef struct Stack
{
int MAX;
int *base, *next;
};
void Creat_ALG(ALGraph &G)
{
cout << "请输入结点数:";
cin >> G.vernum;
cout << "请输入边的条数:";
cin >> G.arcnum;
for (int i = 1; i <= G.vernum; ++i)
{
cout << "请输入节结点的编号:";
cin >> G.ver[i].data;
cout << "请输入姓名:";
cin >> G.ver[i].a1.name;
cout << "请输入性别:";
cin >> G.ver[i].a1.sex;
cout << "请输入年龄:";
cin >> G.ver[i].a1.age;
G.ver[i].firstare = NULL;
}
for (int i = 1; i <= G.arcnum; ++i)
{
int a, b;
cout << "请输入一条边所依附的两个顶点:";
cin >> a >> b;
ArcNode *p1;
p1 = new ArcNode;
p1->adjust = a;
p1->next = G.ver[b].firstare;
G.ver[b].firstare = p1;
ArcNode *p2;
p2 = new ArcNode;
p2->adjust = b;
p2->next = G.ver[a].firstare;
G.ver[a].firstare = p2;
}
return;
}
void Trave(ALGraph &G, int n)
{
cout << "数据:" << G.ver[n].data << endl;
cout << "姓名:" << G.ver[n].a1.name << endl;
cout << "年龄:" << G.ver[n].a1.age << endl;
cout << "性别:" << G.ver[n].a1.sex << endl << endl;;
}
void DFS_AL(ALGraph &G, int n) //深度优先遍历
{
cout << n << ":" << endl;
Trave(G, n);
visited[n] = true;
ArcNode *p;
p = G.ver[n].firstare;
while (p != NULL)
{
int w;
w = p->adjust;
//cout << w << endl;
if (!visited[w])
{
DFS_AL(G, w);
}
p = p->next;
}
return ;
}
void InitQueue(Stack &Q)
{
Q.base = new int[MVnum];
if (!Q.base) return;
Q.next = Q.base;
Q.MAX = MVnum;
return;
}
void EnQueue(Stack &Q, int n)
{
if (Q.next - Q.base == Q.MAX)
{
cout << "啊,栈满了耶~" << endl;
return;
}
*Q.next++ = n;
return;
}
bool QueueEmpty(Stack &Q)
{
if (Q.base == Q.next)
{
return 1;
}
else
{
return 0;
}
}
void DeQueue(Stack &Q, int &u)
{
if (Q.base == Q.next) {
cout << "栈满了啦~" << endl;
return;
}
u = *--Q.next;
return;
}
int First(ALGraph &G, int n)
{
int a = -1, i;
ArcNode *p;
p = G.ver[n].firstare;
if (p)
{
i = p->adjust;
if (!visited[i])
{
a = i;
}
}
return a;
}
int Next(ALGraph &G, int n)
{
int a = -1, i;
ArcNode *p;
p = G.ver[n].firstare;
for (; i <= G.vernum, p != NULL;)
{
i = p->adjust;
if (!visited[i])
{
a = i;
break;
}
else
{
p = p->next;
}
}//for
return a;
}
void BFS_AL(ALGraph &G, int n) //广度优先搜索
{
Stack Q;
visited[n] = true;
InitQueue(Q); //初始化,置空
EnQueue(Q, n); //将n压入栈
while (!QueueEmpty(Q))
{
int u=0;
DeQueue(Q, u); //将队头元素出栈
cout << u << ":" << endl;
Trave(G,u);
for (int w = First(G, u); w >= 0; w = Next(G, u))
//第一个邻接点;下一个邻接点
{
if (!visited[w])
{
visited[w] = true;
EnQueue(Q,w); //将w压入栈
} //if
} //for
}//while
}
int main()
{
ios::sync_with_stdio(false);
memset(visited, false, sizeof(visited));
ALGraph G;
Creat_ALG(G); //创建无向图
int n;
cout << "请输入第一个访问的结点的下标:";
cin >> n;
DFS_AL(G, n); //深度优先遍历
cout << "请输入第一个访问的结点的下标:";
cin >> n;
memset(visited, false, sizeof(visited));
BFS_AL(G, n); //广度优先遍历
system("pause");
return cout << "遍历结束啦啦啦~" << endl,0;
}