采用邻接表表示法创建无向图,并将其顶点表和邻接表进行存盘和读盘处理,最后对该无向图进行深度优先遍历
存盘和读盘的文件路径需根据个人来更改
//算法6.2 采用邻接表表示法创建无向图
#include <iostream>
using namespace std;
#include <cstdio>
#include <fstream>
#include<cstring>
#include<cstdlib>
#include<string>
#define MVNum 100 //最大顶点数
#define OK 1
int visited[MVNum];
typedef char VerTexType; //顶点信息
typedef int OtherInfo; //和边相关的信息
//- - - - -图的邻接表存储表示- - - - -
typedef struct ArcNode{ //边结点
int adjvex; //该边所指向的顶点的位置
struct ArcNode *nextarc; //指向下一条边的指针
OtherInfo info; //和边相关的信息
}ArcNode;
typedef struct VNode{
VerTexType data; //顶点信息
ArcNode *firstarc; //指向第一条依附该顶点的边的指针
}VNode, AdjList[MVNum]; //AdjList表示邻接表类型
typedef struct{
AdjList vertices; //邻接表
int vexnum, arcnum; //图的当前顶点数和边数
}ALGraph;
int LocateVex(ALGraph G , VerTexType v){
//确定点v在G中的位置
for(int i = 0; i < G.vexnum; ++i)
if(G.vertices[i].data == v)
return i;
return -1;
}//LocateVex
int CreateUDG(ALGraph &G){
//采用邻接表表示法,创建无向图G
int i , k;
void Cunpan(ALGraph G);
cout <<"请输入总顶点数,总边数中间以空格隔开:";
cin >> G.vexnum >> G.arcnum; //输入总顶点数,总边数
cout << endl;
cout << "输入点的名称,如 a " <<endl;
for(i = 0; i < G.vexnum; ++i){ //输入各点,构造表头结点表
cout << "请输入第" << (i+1) << "个点的名称:";
cin >> G.vertices[i].data; //输入顶点值
G.vertices[i].firstarc=NULL;
//初始化表头结点的指针域为NULL
cout << endl;
}//for
cout << "请输入一条边依附的顶点,如 a b" << endl;
for(k = 0; k < G.arcnum;++k){ //输入各边,构造邻接表
VerTexType v1 , v2;
int i , j;
cout << "请输入第" << (k + 1) << "条边依附的顶点:";
cin >> v1 >> v2; //输入一条边依附的两个顶点
i = LocateVex(G, v1); j = LocateVex(G, v2);
//确定v1和v2在G中位置,即顶点在G.vertices中的序号
ArcNode *p1=new ArcNode; //生成一个新的边结点*p1
p1->adjvex=j; //邻接点序号为j
p1->nextarc= G.vertices[i].firstarc; G.vertices[i].firstarc=p1;
//将新结点*p1插入顶点vi的边表头部
ArcNode *p2=new ArcNode; //生成另一个对称的新的边结点*p2
p2->adjvex=i; //邻接点序号为i
p2->nextarc= G.vertices[j].firstarc; G.vertices[j].firstarc=p2;
//将新结点*p2插入顶点vj的边表头部
}//for
Cunpan( G );
return OK;
}//CreateUDG
void Cunpan(ALGraph G)
{
int i;
ofstream outFile;
// 打开文件。 下面路径根据自己情况做修改。
outFile.open("D:\\GGtestC\\data.txt");
for (i = 0; i<G.vexnum; i++) //写入数据
{
outFile << G.vertices[i].data;
if (i == G.vexnum-1)
outFile << endl;
}
for(i = 0 ; i < G.vexnum ; ++i){
VNode temp = G.vertices[i];
ArcNode *p = temp.firstarc;
if(p == NULL){
outFile << G.vertices[i].data;
outFile << endl;
}
else{
outFile<< temp.data;
while(p){
outFile<< "->";
outFile<< p->adjvex;
p = p->nextarc;
}
}
outFile<< endl;
}
outFile.close(); //关闭文件
}
void Dupan()
{
ifstream infile("D:\\GGtestC\\data.txt", ios::in);
if(!infile.fail())
{
while(!infile.eof())
{
string str5;
infile>>str5;
cout<<str5<<endl;
}
}
infile.close();
}
int Stack[MVNum];
int Stackcount=-1;//堆栈指针
int StackEmpty(){//判断栈空
return Stackcount==-1;
}
int StackFull(){//判断栈满
return Stackcount==MVNum-1;
}
void Push(int e){//入栈
if(!StackFull())
Stack[++Stackcount]=e;
else
printf("Full");
}
void Pop(){//出栈
if(!StackEmpty())
Stackcount--;
else
printf("Empty");
}
void DFS1Traverse(ALGraph G){ //深度优先遍历(堆栈实现)
int i;
for (i=0; i<G.vexnum; ++i)
visited[i]=0;//初始化访问状态
i=0;//从i号顶点开始遍历
visited[i] = 1;
printf("%c ", G.vertices[i].data);
Push(i);//将起始节点进栈,以便将来正确返回
while(!StackEmpty())
{
ArcNode *p=G.vertices[Stack[Stackcount]].firstarc;//指向栈顶元素的邻接表头
while(p)//
{
if(!visited[p->adjvex])//若当前邻接顶点没有被访问过,则进行访问并入栈
{
printf("%c ",G.vertices[p->adjvex].data);
visited[p->adjvex]=1;
Push(p->adjvex);//访问顶点进栈
break;
}
else//若当前邻接顶点已经被访问过,则沿边找到下一个顶点
p=p->nextarc;
}
if(p==NULL)//若某一方向被访问完,则回溯寻找未被访问的顶点
Pop();
}
}
int main(){
cout << "************算法6.2 采用邻接表表示法创建无向图**************" << endl << endl;
ALGraph G;
CreateUDG(G);
int i;
cout << endl;
cout << "*****邻接表表示法创建的无向图*****" << endl;
for(i = 0 ; i < G.vexnum ; ++i){
VNode temp = G.vertices[i];
ArcNode *p = temp.firstarc;
if(p == NULL){
cout << G.vertices[i].data;
cout << endl;
}
else{
cout << temp.data;
while(p){
cout << "->";
cout << p->adjvex;
p = p->nextarc;
}
}
cout << endl;
}
int a;
printf("\n\n是否需要读盘(1 or 0):");
scanf("%d",&a);
if(a)
{
Dupan();
}
printf("\n图的深度优先遍历从右到左(堆栈实现)为:\t");
DFS1Traverse(G);
printf("\n\n");
return 0;
}//main
例如输入下面这个无向图运行结果为:
输入无向图: