数据结构——图
2015-12-19 20:23
507 查看
</pre><pre name="code" class="cpp">
#include<stdio.h> #include<iostream> #include<stdlib.h> #include<malloc.h> #include<string.h> using namespace std; #define MAX 10000 #define MAXVEX 20 typedef char diandian[10]; //定义邻接矩阵 typedef struct ArcCell{ int adj;//权值 }ArcCell,AdjMatrix[MAXVEX][MAXVEX]; //定义图 typedef struct { diandian vexs[MAXVEX];//顶点 AdjMatrix arcs;//邻接矩阵 int vexnum,arcnum;//顶点数,弧数 }MGraph; //链队列的定义 typedef struct QNode{ int data; struct QNode *next; }QNode,*QueuePtr; typedef struct { QueuePtr front; QueuePtr rear; }LinkQueue; void Creat(MGraph &G); int LoateVex(MGraph G,char v[10]); void Visit(MGraph G,int v); int FirstVex(MGraph G,int v); int NextVex(MGraph G,int v,int w); void Dfs(MGraph G,int v); void DfsTravel(MGraph G); void Bfs(MGraph G); int Init(LinkQueue &Q); int EnQueue(LinkQueue &Q,int e); int DeQueue(LinkQueue &Q,int &e); int EmptyQueue(LinkQueue Q); int main() { while(1) { cout<<"****有向带权图***\n"; cout<<"1:Creat map *\n"; cout<<"2:Bfs *\n"; cout<<"3:Dfs *\n"; cout<<"0:Break *\n"; cout<<"*****************\n"; cout<<"Your choice:\n"; MGraph G; int choice; cin>>choice; if(choice==1) { Creat(G); } else if(choice==2){Bfs(G);cout<<endl;} else if(choice==3){DfsTravel(G);cout<<endl;} else if(choice==0){cout<<"See you next time!\n";break;} else if(choice!=3||choice!=2||choice!=1||choice!=0){cout<<"No those choice!";} } return 0 ; } int Init(LinkQueue &Q) { Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode)); if(!Q.front)exit(0); Q.front->next=NULL; return 1; } int EnQueue(LinkQueue &Q,int e) { QueuePtr p; p=(QueuePtr)malloc(sizeof(QNode)); if(!p)exit(0); p->data=e;p->next=NULL; Q.rear->next=p; Q.rear=p; return 1; } int DeQueue(LinkQueue &Q,int &e) { QueuePtr p; if(Q.rear==Q.front)return 0; p=Q.front->next; e=p->data; Q.front->next=p->next; if(Q.rear==p)Q.rear=Q.front; free(p); return 1; } int EmptyQueue(LinkQueue Q) { if(Q.front==Q.rear) return 1; return 0; } void Visit(MGraph G,int v) { cout<<" "<<G.vexs[v]<<" "; } void Creat(MGraph &G) {//构造有向带权图 int vexnum,arcnum; cout<<"Input vexnum:"; cin>>vexnum; cout<<"Input arcnum:"; cin>>arcnum; G.vexnum=vexnum; G.arcnum=arcnum; //构造顶点向量 for(int k=0;k<G.vexnum;k++) { cout<<"Diandian"<<k+1<<":"; scanf("%s",G.vexs[k]); } //初始化邻接矩阵 for(int i=0;i<G.vexnum;i++) { for(int j=0;j<G.vexnum;j++) G.arcs[i][j].adj=MAX; } //赋值邻接矩阵 char v1[10],v2[10]; int w,Gi,Gj; for(int i=0;i<G.arcnum;i++) {//有向图 cout<<"第"<<i+1<<"条弧\n"; do{ cout<<"v1 v2:"; scanf("%s%s",v1,v2); cout<<"qz:"; cin>>w; Gi=LoateVex(G,v1); Gj=LoateVex(G,v2); G.arcs[Gi][Gj].adj=w; }while(Gi<0||Gj<0); } int count=0; for(int i=0;i<G.vexnum;i++) { for(int j=0;j<G.vexnum;j++) if(G.arcs[i][j].adj<MAX) { cout<<count++<<"th: "<<G.vexs[i]<<"->"<<G.vexs[j]<<":"<<G.arcs[i][j].adj<<endl; } } } int LoateVex(MGraph G,char v[10]) { for(int i=0;i<G.vexnum;i++) { if(!strcmp(v,G.vexs[i])) return i; } cout<<"Haven't this peak!\n"; return -1; } int FirstVex(MGraph G,int v) { for(int j=0;j<G.vexnum;j++) { if(G.arcs[v][j].adj<MAX) return j; } return -1; } int NextVex(MGraph G,int v,int w) { for(int j=w+1;j<G.vexnum;j++) { if(G.arcs[v][j].adj<MAX) return j; } return -1; } int visited[MAXVEX]; void DfsTravel(MGraph G) { for(int v=0;v<G.vexnum;v++) visited[v]=0; for(int v=0;v<G.vexnum;v++) { if(!visited[v])Dfs(G,v); } } void Dfs(MGraph G,int v) { visited[v]=1; Visit(G,v); for(int w=FirstVex(G,v);w>=0;w=NextVex(G,v,w)) { if(!visited[w])Dfs(G,w); } } void Bfs(MGraph G) { for(int v=0;v<G.vexnum;v++) visited[v]=0; LinkQueue Q; Init(Q); for(int v=0;v<G.vexnum;v++) { if(!visited[v]) { visited[v]=1; Visit(G,v); EnQueue(Q,v); while(!EmptyQueue(Q)) { int u; DeQueue(Q,u); for(int w=FirstVex(G,u);w>=0;w=NextVex(G,u,w)) { if(!visited[w]) { visited[w]=1; Visit(G,w); EnQueue(Q,w); } } } } } }
相关文章推荐
- 第十五周--数据结构(项目三)--B-树的基本操作
- 第十五周--数据结构(项目一)--哈希表及其运算的实现
- 数据结构之——用C++实现邻接表的DFS与BFS
- 【数据结构】插入排序
- 【数据结构】选择排序
- 【数据结构】冒泡排序
- 数据结构学习总结
- 数据结构期末总结
- 数据结构之查找——折半查找、插值查找、斐波那契查找
- 数据结构期末总结
- 数据结构期末总结
- 数据结构教程期末学习总结
- 数据结构学期总结
- 数据结构总结
- 数据结构期末总结
- 数据结构与算法
- Redis数据结构(一)
- 数据结构-堆实现优先队列(java)
- [置顶] 数据结构课程总结
- <LeetCode OJ> 21. Merge Two Sorted Lists