以下是用邻接表存储表示,实现图的广度优先遍历的示例。
用于遍历的无向图如下:
//v0-v7依次为a-h
#include<iostream>
#define MaxVertexNum 6
#define MaxSize 7
using namespace std;
//抽象数据类型
typedef char vertextype;
typedef int datatype;
//队列
typedef struct
{
datatype Q[MaxSize];
int front;
int rear;
}SeqQueue;
//边结点类型
typedef struct edge
{
int mark;
int no;
struct edge *next;
}edgetype;
//点结点类型
typedef struct
{
int mark;
vertextype vertex;
edgetype *firstArc;
}vertexNode;
//图
typedef struct
{
vertexNode vex[MaxVertexNum];
int n,e;
}ALGraph;
ALGraph G;
edgetype *p;//用于遍历边结点
//置空队
void ClearQueue(SeqQueue &QU)
{
QU.front=0;
QU.rear=0;
}
//入队,使用循环队列
void EnQueue(SeqQueue &QU,datatype x)
{
//参数检查
if(QU.front==(QU.rear+1)%MaxSize)
{
cout<<"overflow!"<<endl;
return;
}
QU.Q[QU.rear]=x;
QU.rear=(QU.rear+1)%MaxSize;
}
//出队
void DeQueue