数据结构 图的建立 C语言版
来源:互联网 发布:php oa 开源 编辑:程序博客网 时间:2024/06/05 04:04
#include <stdio.h>#include <stdlib.h>#define max_vertex_num 100 //最多顶点个数typedef char VertexData;typedef int AdjType ;typedef int OtherInfo ;typedef struct ArcNode{AdjType adj; //对于无权图 用1表示相邻 0表示不相邻 ;对于带权图,则为权值类型 OtherInfo info;} ArcNode;typedef struct{VertexData vertex[max_vertex_num]; //定义存储顶点的数组ArcNode arcs[max_vertex_num][max_vertex_num]; //定义邻接矩阵int vexnum,arcnum; //定义顶点的个数和边的个数} AdjMatrix; //邻接矩阵的结构体int LocateVertex(AdjMatrix *G,VertexData v) //求顶点的位置{int j=-1,k,i;for(k=0;k<G->vexnum;k++)if(G->vertex[k]==v){j=k;break;}return (j);}void CreateMatrix(AdjMatrix *G) //用邻接矩阵创建无向图{int i,k,j;char v1,v2;printf("用邻接矩阵来创建图 请输入图的顶点个数和弧数\n") ;scanf("%d%d",&G->vexnum,&G->arcnum);for(i=0;i<G->vexnum;i++)for(j=0;j<G->vexnum;j++)G->arcs[i][j].adj=0; //初始化邻接矩阵printf("请输入图的各个顶点\n");getchar();for(i=0;i<G->vexnum;i++)scanf("%c",&G->vertex[i]); //输入图的各个顶点printf("请输入两个顶点之间的弧 一行两个顶点用,隔开\n");for(k=0;k<G->arcnum;k++){getchar();scanf("%c,%c",&v1,&v2);i=LocateVertex(G,v1);j=LocateVertex(G,v2);G->arcs[i][j].adj=1;G->arcs[j][i].adj=1;}printf("打印出用邻接矩阵创建的无向图\n");for(i=0;i<G->vexnum;i++) { for(j=0;j<G->vexnum;j++) printf("%d ",G->arcs[i][j].adj); printf("\n"); }}typedef struct Node{int adjvex; //定义该弧指向顶点的位置struct Node *nextarc; //定义下一条弧上网指针} Node;typedef struct VertexNode{char data; //定义顶点的数据Node *firstarc; //定义该顶点第一条弧的指针} VertexNode[max_vertex_num];typedef struct{VertexNode vertex;int vexnum,arcnum;} AdjList; //定义邻接表void CreateList(AdjList *G) //用邻接表创建无向图{int i,j,k,weight;char v1,v2;printf("用邻接表来创建图 请输入图的顶点个数和弧数\n") ;scanf("%d%d",&G->vexnum,&G->arcnum);printf("请输入图的各个顶点\n");getchar();for(i=0;i<G->vexnum;i++) { scanf("%c",&G->vertex[i].data); //输入图的各个顶点 G->vertex[i].firstarc=NULL; }printf("请输入弧两端的序号顶点\n");int a1,a2;for(i=0;i<G->arcnum;i++){ scanf("%d%d",&a1,&a2); Node *temp=(Node *)malloc(sizeof(Node)); temp->adjvex=a2; temp->nextarc=G->vertex[a1].firstarc; //前插法 G->vertex[a1].firstarc=temp; Node *temp1=(Node *)malloc(sizeof(Node)); temp1->adjvex=a1; temp1->nextarc=G->vertex[a2].firstarc; G->vertex[a2].firstarc=temp1;}}void print(AdjList *G) //打印邻接表建立的图{ int i; for(i=0;i<G->vexnum;i++) { printf("%c---->",G->vertex[i].data); Node *t; t=G->vertex[i].firstarc; while(t!=NULL) { printf(" %c ",G->vertex[t->adjvex].data); t=t->nextarc; } printf("\n"); }}int main(){AdjMatrix G;CreateMatrix(&G);AdjList GG;CreateList(&GG);print(&GG);return 0;}
阅读全文
1 0
- 数据结构 图的建立 C语言版
- 数据结构 链表 单链表的建立 C语言版
- 数据结构 栈的建立及入栈出栈操作 C语言版
- 数据结构 顺序串的建立及其函数 C语言版
- 数据结构 链队列的建立及其函数 C语言版
- 数据结构 链表 循环单链表的建立 C语言版
- 数据结构 树 二叉树的建立及遍历 C语言版
- 《数据结构(C语言版)》- 图
- 数据结构 图的遍历 C语言版
- 数据结构与算法(C语言版)__图的搜索
- 清华大学出版社 数据结构(C语言版)的实现
- 数据结构C语言版单链表的实现
- 数据结构 二叉树的实现 c语言版
- 数据结构c语言版串的操作
- 数据结构C语言版--单链表的基本功能实现
- 数据结构(C语言版)摘录--图
- 数据结构(C语言版)摘录--图
- 数据结构C语言版 哈希表
- 数据结构 图的遍历 C语言版
- ImageJ 综述
- HTTP 协议入门
- hql/sql语句查询英文字符时是不区分大小写的
- Android应用组件之加载器(Loader)
- 数据结构 图的建立 C语言版
- 素数距离问题
- 王爽 《汇编语言》 读书笔记 七 更灵活段定位内存地址段方法
- C#中 IoC 的实现
- c++ 重载 = 为什么返回引用类型
- 焦点事件
- 【Stack】表达式合法判断
- keyCode与charCode\mouseover与mouseenter
- 将给定字符串分割成多个小的回文串