数据结构图的建立与深度和广度遍历代码

#include
#include
#include
using namespace std;
#define MAX_VERTEX_NUM 20//最大顶点数
#define MaxSize 400
typedef int QueueElementType;
typedef enum GraphKind{AG,DG,AN,DN,NONE};//图的种类{无向图,有向图,无向网络,有向网络,无种类}
bool visited[MAX_VERTEX_NUM];
typedef char InfoType;
typedef int VertexType;
typedef struct ArcNode//网络多一个int weight域
{
int adjvex;//该边(或弧)所指向的顶点的下标
struct ArcNode* nextarc;//指向下一个边结点的指针
//int weight;//在该程序中不需要使用
//InfoType info;//指向边或弧所代表的信息的指针,在该程序中不需要使用
}ArcNode;//边节点
typedef struct VNode//顶点节点
{
VertexType data;//顶点信息
ArcNode* firstarc;//指向第一个依附该顶点的边节点的指针
}VNode,AdjList[MAX_VERTEX_NUM];
typedef struct Graph
{
AdjList vertex;//邻接表
int vexnum,arcnum;//顶点数、边(或弧)数
GraphKind kind;//图的种类
}Graph;
typedef struct
{
QueueElementType *base;
int front,rear;
}SeqQueue;
SeqQueue InitQueue()
{
SeqQueue Q;
//申请存储空间
Q.base=(QueueElementType *)malloc(MaxSize *sizeof(QueueElementType));
if(!Q.base)
{
cout<<"内存申请失败,退出程序!"<exit(0);
}
Q.front=Q.rear=0;//队列置空
return Q;
}
int QueueEmpty(SeqQueue Q)
{
return (Q.front==Q.rear)?1:0;//队空返回1,非空返回0
}
int QueueFull(SeqQueue Q)
{
return (Q.front==(Q.rear+1)%MaxSize)?1:0;//队满返回1,非满返回0
}
void EnQueue(SeqQueue &Q,QueueElementType x)
{
if(QueueFull(Q))
{
cout<<"队满,入队操作失败!"<exit(0);
}
else
{
*(Q.base+Q.rear)=x;//插入元素存入队尾
Q.rear=(Q.rear+1)%MaxSize;//修改尾指针,入队完成
}
}
void DeQueue(SeqQueue &Q,QueueElementType &u)
{
if(QueueEmpty(Q))
{
cout<<"队空,出队操作失败!"<exit(0);
}
u=*(Q.base+Q.front);//取出队头元素,由实参带回
Q.front=(Q.front+1)%MaxSize;//修改头指针,出队完成
}
void CreateArcNode(Graph &G,int i,int j)
{//链入顶点i对应的边节点
ArcNode* s;
s=(ArcNode *)malloc(sizeof(ArcNode));
s->adjvex=j-1;
s->nextarc=G.vertex[i-1].firstarc;
G.vertex[i-1].firstarc=s;
}
void Create(Graph &G,bool isA,bool isG)
{
int i,j;
for (i=0;i{
G.vertex[i].data=i+1;
G.vertex[i].firstarc=NULL;
}
if(isA)
{
if(isG)
{
cout<<"您创建的图是无向图, 请顺序输入每条边的两个节点i j"<for (int n=0;n{
cin>>i>>j;//输入边(i,j)
CreateArcNode(G,i,j);
CreateArcNode(G,j,i);
}
}
else
{
cout<<"您创建的图是无向网络,请顺序输入每条边的两个节点i j"<for (int n=0;n{
cin>>i>>j;//输入边(i,j)

CreateArcNode(G,i,j);
CreateArcNode(G,j,i);
}
}
}
else
{
if(isG)
{
cout<<"您创建的图是有向图,请按每条边从i到j的顺序输入i j"<//w=0;
for (int n=0;n{

cin>>i>>j;//输入边(i,j)
CreateArcNode(G,i,j);
}
}
else
{
cout<<"您创建的图是有向网络,请按每条边从i到j的顺序输入i j"<for (int n=0;n{
cin>>i>>j;//输入边(i,j)
CreateArcNode(G,i,j);
}
}
}
}
void CreateAG(Graph &G)
{
Create(G,true,true);
}
void CreateDG(Graph &G)
{
Create(G,false,true);
}
void CreateAN(Graph &G)
{
Create(G,true,false);
}
void CreateDN(Graph &G)
{
Create(G,false,false);
}
void CreateGraph(Graph &G,int n,int e,GraphKind kind)
{
G.vexnum=n;
G.arcnum=e;
G.kind=kind;
if (G.kind==AG)
{
CreateAG(G);
}
if (G.kind==DG)
{
CreateDG(G);
}
if (G.kind==AN)
{
CreateAN(G);
}
if (G.kind==DN)
{
CreateDN(G);
}
}
int FirstAdjVex(Graph G,int v)
{
if(G.vertex[v].firstarc)
{
return G.vertex[v].firstarc->adjvex;
}
else
{
return -1;
}
}
int NextAdjVex(Graph G,VertexType v,VertexType w)
{
ArcNode* p=G.vertex[v].firstarc;
while(p->adjvex!=w)
{
p = p->nextarc;
}
if(p->nextarc)
{
return p->nextarc->adjvex;
}
else
{
return -1;
}
}
void Visit(Graph G,int v)
{
cout<}
void DFS(Graph G,int v)
{//从顶点v出发对v所在的连通分量(或强连通分量)进行深度优先搜索
visited[v] = true;
Visit(G,v);//访问顶点v
// 对v的未访问的邻接点w递归调用DFS
for(int w=FirstAdjVex(G,v);w!=-1;w=NextAdjVex(G,v,w))
{
if(!visited[w])
{
DFS(G,w);
}
}
}
void DFSGraph(Graph G)
{//对图G做深度优先搜索
int v;
for(v=0;v{
visited[v]=false;//初始化访问标志数组visited
}
for(v=0;v{
if(!visited[v])
{
DFS(G,v);//对尚未访问的顶点v调用DFS
}
}
}
void BFSGraph(Graph G)
{
SeqQueue Q;
Q=InitQueue();
VertexType v,u;
for(v=0;v{
visited[v] = false;
}
for(v=0;v{
if(!visited[v])
{
EnQueue(Q,v);
while(!QueueEmpty(Q))
{
DeQueue(Q,u);
if(!visited[u])
{
visited[u]=true;
Visit(G,u);
for(int w=FirstAdjVex(G,u);w!=-1;w=NextAdjVex(G,u,w))
{
if(!visited[w])
{
EnQueue(Q,w);
}
}
}
}
}
}
}
GraphKind setKind(string s)
{
if(s=="AG")
{
return AG;
}
else if(s=="AN")
{
return AN;
}
else if(s=="DG")
{
return DG;
}
else if(s=="DN")
{
return DN;
}
else
{
return NONE;
}
}
int main()
{
Graph G;
int n,e;
string s;
GraphKind kind;
char c;
bool b=true;
bool g=false;
cout<<"**************************************"<cout<<"* *"<

;
cout<<"* 1.图的建立 *"<cout<<"* 2.图的深度优先遍历 *"<cout<<"* 3.图的广度优先遍历 *"<cout<<"* 0.退出 *"<cout<<"* *"<cout<<"**************************************"<while(b)
{
cout<cin>>c;
switch(c)
{
case '1':
{
fflush(stdin);
cout<cin>>n>>e>>s;
kind=setKind(s);
if(kind==NONE)
{
cout<<"图的类型错误!"<break;
}
if(n>MAX_VERTEX_NUM)
{
cout<<"顶点个数超过上限!(至多20个)"<break;
}
if(kind==AG||kind==AN)
{
if(e>(n*(n-1)/2))
{
cout<<"边数超过合理上限!"<break;
}
}
else
{
if(e>(n*(n-1)))
{
cout<<"边数超过合理上限!"<break;
}
}
cout<<"请输入创建图:"<CreateGraph(G,n,e,kind);
g=true;
cout<<"图创建成功!"<break;
}
case '2':
{
if(g==false)
{
cout<}
else
{
cout<DFSGraph(G);
cout<}
break;
}
case '3':
{
if(g==false)
{
cout<}
else
{
cout<BFSGraph(G);
cout<}
break;
}
case '0':
{
cout<b=false;
break;
}
default:
{
cout<}
}
}
return 0;
}

相关文档
最新文档