1、 struct SLNode*Next;slnodetype; slnodetype *Head; slnodetype *Rear;qqtype;int InitiateQQueue(qqtype*q) if(q-Head=(slnodetype*)malloc(sizeof(slnodetype)=NULL) printf(n申请空间失败!); return 0; q-Rear=q-Head;Head-Next=NULL; return 1;int AppendQQueue(qqtype*q,elemtype x)/进队列操作。 slnodetype*p; if(p=(slnodetype
2、*)malloc(sizeof(slnodetype)=NULL) p-Data=x;Rear-Next=p;Rear=p;elemtype DeleteQQueue(qqtype*q)/出队列操作算法。 elemtype x; if(q-Next=NULL)Next=q-Rear) q-p=q-Next;x=p-Data;else p=q-Next=p- x=p- free(p); return x;int NotEmtyQQueue(qqtype*q)Rear=q-Head)return 0; else return 1;int LocVertex(adjlisttype Graph,in
3、t v) int k;for(k=0;kAdjVertex.Vertex);int NextVertex(adjlisttype Graph,int v,int w)/求图的顶点v相邻顶点w的下一个相邻无向边。while(p!=NULL&Graphp-AdjVertex.Vertex!=w) p=p-NextArc; if(p-NextArc=NULL|p=NULL)printf(参数出错!nreturn(-1);p=p-void DeleteArc(adjlisttype Graph,int v,int w)/删除无向图v与w的边。arctype *r1,*r2; w=LocVertex(G
4、raph,w); r1=Graphv.FirstArc;/在v中删除w结点。 r2=r1; while(r1!r1-AdjVertex!=w) r1=r1- if(r1=NULL)n无此边return ; else if(r1=r2) Graphv.FirstArc=r1- free(r1); else r2-NextArc=r1-free(r1);r1=Graphw.FirstArc;/在w中删除v结点。=v) Graphw.FirstArc=r1-void BFSearch(adjlisttype Graph,int k,int Visited) arctype *p; qqtype a,
5、*QQueue=&a; int w; InitiateQQueue(QQueue); Visitedk=1;%d ,Graphk.Vertex); AppendQQueue(QQueue,k); while(NotEmtyQQueue(QQueue)=1) w=DeleteQQueue(QQueue); p=Graphw.FirstArc; while(p!=NULL) if(Visitedp-AdjVertex=0) Visitedp-AdjVertex=1; printf(,Graphp- AppendQQueue(QQueue,p-AdjVertex); p=p- void Trave(
6、adjlisttype Graph,int n)int i,VisitedMAXNNODE;for(i=0;iNextArc=NULL;n输入图中各边:/输入各边并将相应的边结点插入到链表中 e;%d %dv1,&v2);/注意是这样输入,如:1 3。 i=LocVertex(Graph,v1); j=LocVertex(Graph,v2); q=(arctype*)malloc(sizeof(arctype);AdjVertex=j;NextArc=Graphi.FirstArc; Graphi.FirstArc=q; p=(arctype*)malloc(sizeof(arctype);AdjVertex=i;NextArc=Graphj.FirstArc; Graphj.FirstArc=p;n图的邻接表结构为:Graph%d.,i); v1=Graphi.Vertex;Vertex%d:,v1); p=Graphi.FirstArc; while(p! v2=p-AdjVertex;-,v2);输出广度优先搜索结果: Trave(Graph,n);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1