分享
分享赚钱 收藏 举报 版权申诉 / 16

类型数据结构(visual c++)用邻接矩阵表示给定无向图并进行深度遍历.doc

  • 上传人:精品资料
  • 文档编号:10915910
  • 上传时间:2020-01-20
  • 格式:DOC
  • 页数:16
  • 大小:253.50KB
  • 配套讲稿:

    如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。

    特殊限制:

    部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。

    关 键  词:
    数据结构(visual c++)用邻接矩阵表示给定无向图并进行深度遍历.doc
    资源描述:

    1、1.给定无向图,请用邻接矩阵表示法表示该图v4 v5v3v2v1#include#includeusing namespace std;#define MAX 20typedef int AdjMAXMAX;typedef structstring vexsMAX; /顶点表Adj arcs; /邻接矩阵int vexnum,arcnum; /图的顶点和弧数MGraph;int LocateVex(MGraph int CreateUDN(MGraph string v1,v2;coutG.vexnumG.arcnum;coutG.vexsi; /构造顶点数for(i=0;iv1v2;i=Lo

    2、cateVex(G,v1); j=LocateVex(G,v2);G.arcsij=1;G.arcsji=1; /置的对称弧return 0;int LocateVex(MGraph for (i=0;i# include # include # include using namespace std;int visited30;# define MAX_VERTEX_NUM 30# define OK 1/typedef int VertexType;typedef int InfoType;typedef struct ArcNode /弧 int adjvex;struct ArcNod

    3、e *nextarc;ArcNode;typedef struct VNode/表头int data;ArcNode *firstarc;VNode,AdjListMAX_VERTEX_NUM;typedef struct/图 AdjList vertices;int vexnum,arcnum;int kind;ALGraph;void CreateDG(ALGraph coutG.vexnum;coutG.arcnum; for(i=1;iv2;coutv1;ArcNode *p;p=(ArcNode*)malloc(sizeof(ArcNode);if(!p) exit(-1);p-ad

    4、jvex=v1;p-nextarc=NULL;G.verticesk.firstarc=p;for(int i=1;im;ArcNode *q;q=(ArcNode *)malloc(sizeof(ArcNode);/动态指针if(!q) exit(-1);q-adjvex=m; /顶点给 Pq-nextarc=NULL;p-nextarc=q;p=q;/free(q);/free(p); void DFS (ALGraph G,int v )/深度搜索visitedv=1;coutnextarc) w=x-adjvex;if(visitedw=0)DFS(G,w);void DFSB (AL

    5、Graph G,int v)/深度搜索的边集visitedv=1;ArcNode *y;y=(ArcNode*)malloc(sizeof(ArcNode);if(!y) exit(-1);y=G.verticesv.firstarc;int u=G.verticesv.data;int w;for(;y;y=y-nextarc) w=y-adjvex;if(visitedw=0)cout“next=NULL;void EnQueue (LinkQueue p=(QNode*)malloc(sizeof(QNode);if(!p) exit(-1);p-data=e;p-next=NULL;Q

    6、.rear-next=p;Q.rear=p;/free(p);int DeQueue (LinkQueue QNode *p;p=(QNode*)malloc(sizeof(QNode);if(!p) exit(-1);p=Q.front-next;e=p-data;Q.front-next=p-next;if(Q.rear=p)Q.rear=Q.front;free(p);return e;int QueueEmpty (LinkQueue Q)/判断队列是否为空if(Q.front=Q.rear)return 1;return 0;void BFS(ALGraph G,int v)/广度搜

    7、索int u;LinkQueue Q;InitQueue(Q);if(visitedv=0)visitedv=1;coutadjvex;w=0;w=z-nextarc-adjvex)if(visitedw=0)visitedw=1;coutnextarc) w=z-adjvex;if(visitedw=0)visitedw=1;coutnextarc) w=r-adjvex;if(visitedw=0)visitedw=1;cout“x;coutadjvexnextarc;coutn;for( i=0;i#include#define MAX_VEXTEX_NUM 20#define M 20

    8、#define STACK_INIT_SIZE 100#define STACKINCREMENT 10#define OK 1#define ERROR 0typedef int ElemType;typedef struct ArcNodeint adjvex;struct ArcNode *nextarc;ArcNode;typedef struct VNodeint data;ArcNode *firstarc;VNode,AdjListMAX_VEXTEX_NUM;typedef structAdjList vertices;int vexnum, arcnum;ALGraph;ty

    9、pedef struct /构件栈ElemType *base;ElemType *top;int stacksize;SqStack;void InitStack(SqStack *); /函数声明int Pop(SqStack *, ElemType *);void Push(SqStack *,ElemType );int StackEmpty(SqStack *);void CreatGraph(ALGraph *);void FindInDegree(ALGraph , int * );void TopologicalSort(ALGraph );void InitStack(SqS

    10、tack *S)/初始化栈S-base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType);if(!S-base)printf(“memory allocation failed, goodbye“);exit(1);S-top=S-base;S-stacksize=STACK_INIT_SIZE;int Pop(SqStack *S,ElemType *e)/出栈操作if(S-top=S-base)return ERROR;*e=*-S-top;/printf(“%dn“,e);/ return e;return 0;void Push(S

    11、qStack *S,ElemType e)/进栈操作if(S-top-S-base=S-stacksize)S-base = (ElemType *)realloc(S-base,(S-stacksize+STACKINCREMENT)*sizeof(ElemType);if(!S-base)printf(“memory allocation failed, goodbye“);exit(1);S-top = S-base+S-stacksize;S-stacksize+=STACKINCREMENT;*S-top+=e;int StackEmpty(SqStack *S)/判断栈是否为空if

    12、(S-top=S-base)return OK;elsereturn ERROR;void CreatGraph(ALGraph *G)/构件图int m, n, i;ArcNode *p;printf(“请输入顶点数和边数:“);scanf(“%d%d“,for (i = 1; i vexnum; i+)G-verticesi.data = i;G-verticesi.firstarc = NULL;for (i = 1; i arcnum; i+) /输入存在边的点集合printf(“n 请输入存在边的两个顶点的序号:“);scanf(“%d%d“,while (n G-vexnum |

    13、m G-vexnum)printf(“输入的顶点序号不正确 请重新输入:“);scanf(“%d%d“,p = (ArcNode*)malloc(sizeof(ArcNode);if (p = NULL)printf(“memory allocation failed,goodbey“);exit(1);p-adjvex = m;p-nextarc = G-verticesn.firstarc;G-verticesn.firstarc = p;printf(“建立的邻接表为:n“); /输出建立好的邻接表for(i = 1; i vexnum; i+)printf(“%d“,G-vertice

    14、si.data);for(p = G-verticesi.firstarc; p; p = p-nextarc)printf(“%3d“,p-adjvex);printf(“n“);void FindInDegree(ALGraph G, int indegree)/求入度操作int i;for (i = 1; i adjvex+;G.verticesi.firstarc = G.verticesi.firstarc-nextarc;void TopologicalSort(ALGraph G) /进行拓扑排序int indegreeM;int i, k, n;int count = 0;ArcNode *p;SqStack S;FindInDegree(G, indegree);InitStack(for (i = 1; i nextarc)k = p-adjvex;if (!(-indegreek)Push(printf(“n“);if (count G.vexnum)printf(“出现错误n“);elseprintf(“排序成功n“);int main(void) /主函数ALGraph G;CreatGraph(TopologicalSort(G);system(“pause“);return 0;

    展开阅读全文
    提示  道客多多所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。
    关于本文
    本文标题:数据结构(visual c++)用邻接矩阵表示给定无向图并进行深度遍历.doc
    链接地址:https://www.docduoduo.com/p-10915910.html
    关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

    道客多多用户QQ群:832276834  微博官方号:道客多多官方   知乎号:道客多多

    Copyright© 2025 道客多多 docduoduo.com 网站版权所有世界地图

    经营许可证编号:粤ICP备2021046453号    营业执照商标

    1.png 2.png 3.png 4.png 5.png 6.png 7.png 8.png 9.png 10.png



    收起
    展开