邻接表实现图的遍历
2008-05-02 18:17
176 查看
#include <iostream>
#include <cstdio>
#define N 10
using namespace std;
char B
;
class AdjGraph...{ //链式无向图
public:
AdjGraph()...{ vexnum=arcnum=0; }
~AdjGraph()...{ system("pause");}
void CreateGraph();
void DestroyGraph();
void GetVex(int v); //输出下标v的信息
void DeleteArc(int v); //删除下标v的结点和与其对应的弧
void InsertArc(char v,int m); //插入data为v的结点建立与其对应的弧
void DfsTraverse(int v); //深度优先遍历
void BfsTraverse(int v); //广度优先遍历
private:
typedef struct ArcNode...{ //表域
int adjvex;
struct ArcNode *next;
}ArcNode;
ArcNode *p;
typedef struct VNode...{ //顶点域
char data;
ArcNode *firstarc;
}VNode;
VNode AdjList
;
int vexnum,arcnum; //顶点数和弧数
};
void AdjGraph::CreateGraph()...{
printf("Input Vexnums Arcnums: ");
cin>>vexnum>>arcnum;
printf("Input Node value: ");
for(int i=0;i<vexnum;++i)
...{
cin>>AdjList[i].data;
AdjList[i].firstarc=NULL;
}
int m=0,n=0;
printf("Input the Arc message: ");
for(int j=0;j<arcnum;++j)
...{
cin>>m>>n;
p=new ArcNode;
p->adjvex=n;
p->next=AdjList[m].firstarc;
AdjList[m].firstarc=p; //插入到表头
p=new ArcNode;
p->adjvex=m;
p->next=AdjList
.firstarc;
AdjList
.firstarc=p;
}
}
void AdjGraph::DestroyGraph()...{
for(int i=0;i<vexnum;++i)
...{
while(AdjList[i].firstarc)
...{
p=AdjList[i].firstarc;
AdjList[i].firstarc=p->next;
delete p;
}
}
}
void AdjGraph::GetVex(int v)...{
p=AdjList[v].firstarc;
int i=0;
printf("Index %d vex message is: ",v);
while(p)
...{
printf("[%c] --- [%c] ",AdjList[v].data,AdjList[p->adjvex].data);
p=p->next;
}
}
void AdjGraph::DeleteArc(int v)...{ //delete index 2 arc
p=AdjList[v].firstarc;
while(p)
...{
ArcNode *q1=AdjList[p->adjvex].firstarc;
ArcNode *q2=q1;
if(q1->adjvex==v)
AdjList[p->adjvex].firstarc=q1->next;
else
...{
while(q1->adjvex!=v)
...{
q2=q1;
q1=q1->next;
}
q2->next=q1->next;
}
delete q1;
AdjList[v].firstarc=p->next;
delete p;
p=AdjList[v].firstarc;
}
AdjList[v].data='*';
}
void AdjGraph::InsertArc(char v,int m)...{
int i;
for(i=0;i<vexnum;++i)
if(AdjList[i].data=='*')
...{
AdjList[i].data=v;
break;
}
p=new ArcNode;
p->adjvex=m;
p->next=AdjList[i].firstarc;
AdjList[i].firstarc=p;
p=new ArcNode;
p->adjvex=i;
p->next=AdjList[m].firstarc;
AdjList[m].firstarc=p;
}
void AdjGraph::DfsTraverse(int v)...{
printf("%c ",AdjList[v].data);
B[v]='1';
p=AdjList[v].firstarc;
while(p)
...{
if(B[p->adjvex]=='0')
...{
printf("%c ",AdjList[p->adjvex].data);
B[p->adjvex]='1';
}
p=p->next;
}
for(v=0;v<vexnum;++v)
if(B[v]=='0')
DfsTraverse(v);
}
void AdjGraph::BfsTraverse(int v)...{
int Queue
;
int front=-1,rear=-1;
printf("%c ",AdjList[v].data);
B[v]='1';
Queue[++rear]=v;
while(front!=rear)
...{
v=Queue[++front];
p=AdjList[v].firstarc;
while(p)
...{
if(B[p->adjvex]=='0')
...{
printf("%c ",AdjList[p->adjvex].data);
B[p->adjvex]='1';
Queue[++rear]=p->adjvex;
}
p=p->next;
}
}
}
int main()...{
AdjGraph LG;
LG.CreateGraph();
LG.DeleteArc(2);
memset(B,'0',sizeof(B));
printf(" DfsTraverse: ");
LG.DfsTraverse(0);
printf(" Insert 'F',create arc [F] --- [A]. ");
LG.InsertArc('F',0);
LG.GetVex(0);
LG.GetVex(1);
LG.GetVex(2);
LG.GetVex(3);
LG.GetVex(4);
memset(B,'0',sizeof(B));
printf(" DfsTraverse: ");
LG.DfsTraverse(0);
memset(B,'0',sizeof(B));
printf(" BfsTraverse: ");
LG.BfsTraverse(1);
LG.DestroyGraph();
return 0;
}
相关文章推荐
- 数据结构学习笔记(四) 图之邻接表实现广度优先遍历
- 一种经典的邻接表的实现和遍历方式
- 无向图的构建及广度优先遍历---邻接表实现
- C++实现图的邻接表存储和广度优先遍历实例分析
- 邻接表存储图,DFS遍历图的java代码实现
- 图(有向图)的邻接表表示 C++实现(遍历,拓扑排序,最短路径,最小生成树) Implement of digraph and undigraph using adjacency list
- 无向图的实现(邻接表) 图的遍历
- 邻接矩阵(以顶点为中心),比较稀疏时,采用邻接表;图的两种遍历(邻接矩阵实现)
- 邻接表 实现图的遍历 C++
- Java实现基于邻接表的图的深度和广度遍历
- 图的邻接表存储 深度优先遍历 广度优先遍历 C语言实现
- 图的深度优先和广度优先遍历(图以邻接表表示,由C++面向对象实现)
- [置顶] 图:图的邻接表创建、深度优先遍历和广度优先遍历代码实现
- 基于邻接表的无向图的深度广度遍历实现
- 邻接表实现图的储存,遍历
- 邻接表存储图,DFS遍历图的java代码实现
- 邻接表实现了一个无向图即图的遍历
- DFS BFS遍历图 邻接表实现
- 无向图邻接表实现代码 以及深度优先遍历
- 用邻接表实现图的深度优先遍历、广度优先遍历、最短路径(无权图)