#include#defineMAX_SIZE100boolVisited[MAX_SIZE]"> #include#defineMAX_SIZE100boolVisited[MAX_SIZE]" />
实验7:图的基础实验

实验7:图的基础实验

ID:25969987

大小:85.50 KB

页数:6页

时间:2018-11-23

实验7:图的基础实验_第1页
实验7:图的基础实验_第2页
实验7:图的基础实验_第3页
实验7:图的基础实验_第4页
实验7:图的基础实验_第5页
资源描述:

《实验7:图的基础实验》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、实验7:图的基础实验用深层遍历和广度遍历实现图的遍历操作#include"stdafx.h"#include#include#defineMAX_SIZE100boolVisited[MAX_SIZE];typedefstructarcNode{intadjvex;//该弧所指向的顶点的位置structarcNode*nextarc;//指向下一条弧的指针intinfo;//该弧相关信息的指针}ArcNode;typedefstructvNode{intdata;//顶点信息ArcNode*fi

2、rstarc;//指向第一条依附该顶点的弧的指针}VNode,AdjList[MAX_SIZE];typedefstruct{AdjListvertices;intvexnum,arcnum;//图的当前顶点数和弧数intkind;//图的种类标志}AlGraph;typedefstructQNode{intdata;structQNode*next;}QNode,*QueuePtr;typedefstruct{QueuePtrfront;//队头指针QueuePtrrear;//队尾指针}LinkQueue;voidcreateA

3、lGraph(AlGraph&G){printf("请输入图的顶点数:");scanf("%d",&G.vexnum);printf("请输入图的边数:");scanf("%d",&G.arcnum);G.kind=1;inti;intj;intk;for(i=0;i!=G.vexnum;i++){G.vertices[i].data=i+1;}inta[MAX_SIZE][MAX_SIZE];for(i=0;i!=G.arcnum;i++){printf("请输入边的相邻顶点:");scanf("%d%d",&j,&k);if(j

4、==k)continue;else{a[j-1][k-1]=1;a[k-1][j-1]=1;}}intmark;ArcNode*p;ArcNode*q;for(i=0;i!=G.vexnum;i++){mark=0;for(j=0;j!=G.vexnum;j++)if(j==i)continue;elseif(a[i][j]==1){if(mark==0){p=(ArcNode*)malloc(sizeof(ArcNode));p->adjvex=j;p->nextarc=NULL;G.vertices[i].firstarc=p;

5、mark=1;}else{q=(ArcNode*)malloc(sizeof(ArcNode));q->adjvex=j;q->nextarc=NULL;p->nextarc=q;p=p->nextarc;q=NULL;}}elsecontinue;}}//创建空队列voidInitQueue(LinkQueue&Q){Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));if(!Q.front)exit(1);Q.front->next=NULL;}voidEnQueue(LinkQueue

6、&Q,inte){//将元素e插入到队尾QueuePtro;o=(QueuePtr)malloc(sizeof(QNode));if(!o)exit(1);o->data=e;o->next=NULL;Q.rear->next=o;Q.rear=o;o=NULL;}voidDeQueue(LinkQueue&Q,int&e){//若队列不空,则删除队列的队头元素,并返回队头元素QueuePtro;o=(QueuePtr)malloc(sizeof(QNode));if(Q.front==Q.rear)printf("ERROR");

7、o=Q.front->next;e=o->data;Q.front->next=o->next;if(Q.rear==o)Q.rear=Q.front;free(o);}intQueueEmpty(LinkQueueQ){if(Q.front==Q.rear)return1;elsereturn0;}intfirstAdjvex(AlGraphG,intv){if(G.vertices[v].firstarc!=NULL)returnG.vertices[v].firstarc->adjvex;elsereturn-1;}intne

8、xtAdjvex(AlGraphG,intv,intw){ArcNode*r;r=(ArcNode*)malloc(sizeof(ArcNode));r=G.vertices[v].firstarc;while(r->nextarc!

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。