数据构造实验报告_1.docx
数据构造实验报告实验名称:数据构造实验五实验内容:1.使用邻接矩阵建立一个图,深度遍历。2使用邻接表建立一个图,广度遍历。3.建立一个图,存储构造本人确定,并进行拓扑排序。实验代码:1.#include"stdio.h"#defineInfinity100#defineMaxVertexNum20typedefenumDG,DN,UDG,UDNGraphKind;typedefintVRType;typedefcharVertexType;boolVisitMaxVertexNum;typedefstructArcCellVRTypeadj;ArcCell,AdjMatrixMaxVertexNumMaxVertexNum;typedefstructVertexTypevexsMaxVertexNum;AdjMatrixarcs;/邻接矩阵intvexnum,arcnum;/图的当前顶点数和弧数GraphKindkind;MGraph;intLocateVex(MGraphG,VertexTypev)for(inti=0;iinti,j,k;printf("请输入顶点数:n");scanf("%d",&G.vexnum);printf("请输入弧数:n");scanf("%d",&G.arcnum);i=0;while(igetchar();scanf("%c%c%d",&v1,i=LocateVex(G,v1);j=LocateVex(G,v2);G.arcsij.adj=w;G.arcsji=G.arcsij;return;voidDFSTraverse(MGraph&G,inti)printf("%c",G.vexsi);Visiti=true;for(intj=0;j针InfoType*info;ArcNode;typedefstructVNodeVertexTypedata;/顶点信息ArcNode*firstarc;/指向第一条依附该顶点的弧的指针VNode,AdjListMaxVertexNum;typedefstructAdjListvertices;intvexnum,arcnum;/图的当前顶点数和弧数ALGraph;typedefstructQNodeQElemTypedata;structQNode*next;QNode,*Queueptr;typedefstructQueueptrfront;Queueptrrear;LinkQueue;voidInitQueue(LinkQueue&Q)Q.front=Q.rear=(Queueptr)malloc(sizeof(QNode);if(!Q.front)return;Q.front->next=NULL;return;voidEnQueue(LinkQueue&Q,QElemTypee)Queueptrp=NULL;p=(Queueptr)malloc(sizeof(QNode);if(!p)return;p->data=e;p->next=NULL;Q.rear->next=p;Q.rear=p;return;QElemTypeDeQueue(LinkQueue&Q,QElemType&e)Queueptrp;if(Q.front=Q.rear)return''p=Q.front->next;e=p->data;Q.front->next=p->next;if(Q.rear=p)Q.rear=Q.front;free(p);returne;intQueueEmpty(LinkQueueQ)if(Q.front=Q.rear)return1;elsereturn0;intLocate(ALGraphG,VertexTypev)for(intk=0; k