4、ata[maxsize];
int front,rear;
}sequeue;
//定义结构体
typedef struct node
{
datatype adjvex;
struct node *next;
}edgenode;
//定义结构体
typedef struct
{
vextype vertex;
edgenode *link;
}vexnode;
sequeue *Q;
vexnode ga[n];
int visited[n];
sequeue *sq;
//建立无向图的邻接表
void CREATADJLIST(vexnod
5、e ga[])
{
int i,j,k;
edgenode *s;
printf("读入树的结点信息:");
for(i=0;inext=ga[i].link;
ga[i].link=s;
s=(edgeno
6、de *)malloc(sizeof(edgenode));
s->adjvex=i;
s->next=ga[j]。link;
ga[j].link=s;
}
}
//置空队
void SETNULL(sequeue *sq)
{
sq->front=maxsize—1;
sq—〉rear=maxsize—1;
}
//判队空
int EMPTY(sequeue *sq)
{
if(sq-〉rear==sq-〉front)
return 1;
else
return 0;
}
//入队
int ENQUEUE(sequeue *sq,datatype
7、x)
{
if(sq—〉front==(sq—〉rear+1)%maxsize)
{
printf(”queue is full");
return NULL;
}
else
{
sq—>rear=(sq-〉rear+1)%maxsize;
sq—〉data[sq->rear]=x;
return 1;
}
}
//出队
datatype DEQUEUE(sequeue *sq)
{
if(EMPTY(sq))
{
printf("queue is empty”);
return NULL;
}
else
{
sq->front=(sq—〉fro
8、nt+1)%maxsize;
return (sq-〉data[sq—〉front]);
}
}
//广度优先遍历
void BFSL(int k)
{
int i;
edgenode *p;
Q=(sequeue *)malloc(sizeof(sequeue));
SETNULL(Q);
printf(”%c\n”,ga[k].vertex);
visited[k]=1;
ENQUEUE(Q,k);
while(!EMPTY(Q))
{
i=DEQUEUE(Q);
p=ga[i]。link;
while(p!=NULL)
{
if(!visited[
9、p—>adjvex])
{
printf("%c\n",ga[p—〉adjvex].vertex);
visited[p—〉adjvex]=1;
ENQUEUE(Q,p-〉adjvex);
}
p=p-〉next;
}
}
}
void main()
{
CREATADJLIST(ga);
BFSL(0);}
六、运行界面
七、实验中遇到的问题及总结
在图的存储的基础上广度优先遍历使我对图的基本知识更加了解,更加扎实。并且更加明确广度优先遍历和深度优遍历的区别和联系。在实验中我希望我可以更加的细心,更上一层楼.
八、参考文献
《数据结构——用C语言描述》