7 邻接表的建立与遍历.doc_第1页
7 邻接表的建立与遍历.doc_第2页
7 邻接表的建立与遍历.doc_第3页
7 邻接表的建立与遍历.doc_第4页
7 邻接表的建立与遍历.doc_第5页
已阅读5页,还剩4页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

#include stdio.h#include stdlib.h#define MAX_SIZE 20int visitedMAX_SIZE;typedef struct ArcNode /弧的结点结构 int adjvex; ArcNode *nextarc;ArcNode;typedef struct VNode /顶点结构 char data; ArcNode *firstarc;AdjListMAX_SIZE;typedef struct /图的存储结构 AdjList vexs; int arcnum,vexnum; Graph;/* 图的基本操作 */void creatGraph(Graph *h);int Loc(Graph G,char ch);void DFS(Graph G,char ch);void DispGraphStruct(Graph G);void BFS(Graph G); /* 图的基本操作 */ * 队列结构及操作 */typedef int QElemType; typedef struct QNode QElemType data;struct QNode *next; QNode,*QueuePtr;typedef struct QueuePtr front; QueuePtr rear;LinkQueue;int InitQueue(LinkQueue &Q);int EnQueue(LinkQueue &Q,QElemType e);int DeQueue(LinkQueue &Q,QElemType &e);int QueueEmpty(LinkQueue Q);/ * 队列结构及操作 */void main() Graph H; creatGraph(&H);printf(n图的邻接表结构输出:); DispGraphStruct(H); printf(nn深度优先搜索遍历,顶点输出顺序:); DFS(H,H.vexs0.data); /深度优先搜索遍历 printf(nn广度优先搜索遍历,顶点输出顺序:); BFS(H) ; printf(n);void creatGraph(Graph *h) ArcNode *p; int i,j,n; char ch20; printf(输入图的顶点数,边数: ); scanf(%d,%d,&h-vexnum,&h-arcnum); printf(n输入%d个顶点(a-z): ,h-vexnum); scanf(%s,&ch); for(i=0;ivexnum;i+) h-vexsi.data=chi; h-vexsi.firstarc=NULL; printf(共有%d条边,下面逐条输入:n,h-arcnum); for(i=0;iarcnum;i+) printf(输入第 %d 条边:,i+1);scanf(%s,&ch);j=Loc(*h,ch0); n=Loc(*h,ch1);p=(ArcNode *)malloc(sizeof(ArcNode);p-adjvex=n;p-nextarc=h-vexsj.firstarc;h-vexsj.firstarc=p; p=(ArcNode *)malloc(sizeof(ArcNode);p-adjvex=j;p-nextarc=h-vexsn.firstarc;h-vexsn.firstarc=p; /无向图*/void DFS(Graph G,char ch) ArcNode *p;int i;i=Loc(G,ch);printf(%c ,ch);visitedi=1;p=G.vexsi.firstarc;while(p!=NULL) if(!visitedp-adjvex)DFS(G,p-adjvex+a);p=p-nextarc; void BFS(Graph G) int i,j;LinkQueue Q;ArcNode *p;for(i=0;iG.vexnum;i+) visitedi=0;InitQueue(Q);for(i=0;iadjvex) visitedp-adjvex=1;printf(%c ,G.vexsp-adjvex.data);EnQueue(Q,p-adjvex);p=p-nextarc; void DispGraphStruct(Graph G) int i,j;char ch;ArcNode *p,*h;for(i=0;i,ch,ch); if (h) p=h;while (p) j=p-adjvex;printf( %c -,G.vexsj.data);p=p-nextarc; printf( NULL);int Loc(Graph G,char ch) int i; for(i=1;inext=NULL;return 1; /InitQueueint EnQueue(LinkQueue &Q,QElemType e) QueuePtr p;p=(QueuePtr)malloc(sizeof(QNode);if (!p) return (-1);p-data=e; p-next=NULL;Q.rear-next=p; Q.rear=p;return 1; int DeQueue(LinkQueue &Q,QElemType &e) QueuePtr p;if (QueueEmpty(Q) return -1;p=Q.front-next; e=p-data;Q.front-nex

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论