图的邻接表存储表示(C)
//---------图的邻接表存储表示------- #include<stdio.h>
#include<stdlib.h> #define MAX_VERTEXT_NUM 20 typedef int InfoType;
typedef char VertextType; typedef struct ArcNode
{
int adjvex;
struct ArcNode *nextArc;
InfoType *info;
}ArcNode; typedef struct VNode
{
VertextType data;
ArcNode *firstArc;
}VNode, AdjList[MAX_VERTEXT_NUM]; typedef struct
{
AdjList verTices;
int vexNum;
int arcNum;
int kind;
}ALGraph; void CreateGraph(ALGraph *G);
void DisplayGraph(ALGraph *G); int main()
{
ALGraph *Graph = (ALGraph *)malloc(sizeof(ALGraph));
CreateGraph(Graph);
DisplayGraph(Graph); system("pause");
} void CreateGraph(ALGraph *G)
{
int i,j,k;
ArcNode *arcNode;
printf_s("请输入顶点数和边数:");
scanf_s("%d,%d",&G->vexNum, &G->arcNum); //建立顶点表
printf_s("建立顶点表\n");
for (i = ; i < G->vexNum; i++)
{
printf_s("请输入第%d个顶点:", i);
fflush(stdin);//刷新缓冲区
G->verTices[i].data = getchar();
G->verTices[i].firstArc = NULL;
} //建立边表
printf_s("建立边表\n");
for (k = ; k < G->arcNum; k++)
{
printf_s("请输入(vi-vj)的顶点对序号");
scanf_s("%d,%d", &i, &j);
arcNode = (ArcNode *)malloc(sizeof(ArcNode));
arcNode->adjvex = j;
arcNode->nextArc = G->verTices[i].firstArc;//插入表头
G->verTices[i].firstArc = arcNode; arcNode = (ArcNode *)malloc(sizeof(ArcNode));
arcNode->adjvex = i;
arcNode->nextArc = G->verTices[j].firstArc;//插入表头
G->verTices[j].firstArc = arcNode;
}
} void DisplayGraph(ALGraph *G)
{
int i;
for (i = ; i < G->vexNum; i++)
{
printf_s("%d->", i);
while (G->verTices[i].firstArc != NULL)
{
printf_s("%d->", G->verTices[i].firstArc->adjvex);
G->verTices[i].firstArc = G->verTices[i].firstArc->nextArc;
}
printf_s("\n");
}
}
请输入顶点数和边数:,
建立顶点表
请输入第0个顶点:
请输入第1个顶点:
请输入第2个顶点:
请输入第3个顶点:
请输入第4个顶点:
请输入第5个顶点:
建立边表
请输入(vi-vj)的顶点对序号0,
请输入(vi-vj)的顶点对序号0,
请输入(vi-vj)的顶点对序号1,
请输入(vi-vj)的顶点对序号1,
请输入(vi-vj)的顶点对序号2,
请输入(vi-vj)的顶点对序号2,
请输入(vi-vj)的顶点对序号3,
->->->
->->->->
->->->
->->->
->->->
->->->->
请按任意键继续. . .
图的邻接表存储表示(C)的更多相关文章
- 图的邻接表存储 c实现
图的邻接表存储 c实现 (转载) 用到的数据结构是 一个是顶点表,包括顶点和指向下一个邻接点的指针 一个是边表, 数据结构跟顶点不同,存储的是顶点的序号,和指向下一个的指针 刚开始的时候把顶点表初始化 ...
- 数据结构之---C语言实现图的邻接表存储表示
// 图的数组(邻接矩阵)存储表示 #include <stdio.h> #include <stdlib.h> #include <string.h> #defi ...
- c_数据结构_图_邻接表
课程设计------邻接表 图的遍历实现课程设计:https://files.cnblogs.com/files/Vera-y/图的遍历_课程设计.zip #include<stdio.h> ...
- 数据结构(11) -- 邻接表存储图的DFS和BFS
/////////////////////////////////////////////////////////////// //图的邻接表表示法以及DFS和BFS //////////////// ...
- 邻接表存储图,DFS遍历图的java代码实现
import java.util.*; public class Main{ static int MAX_VERTEXNUM = 100; static int [] visited = new i ...
- c++邻接表存储图(无向),并用广度优先和深度优先遍历(实验)
一开始我是用c写的,后面才发现广搜要用到队列,所以我就直接使用c++的STL队列来写, 因为不想再写多一个队列了.这次实验写了两个多钟,因为要边写边思考,太菜了哈哈. 主要参考<大话数据结构&g ...
- PTA 邻接表存储图的广度优先遍历(20 分)
6-2 邻接表存储图的广度优先遍历(20 分) 试实现邻接表存储图的广度优先遍历. 函数接口定义: void BFS ( LGraph Graph, Vertex S, void (*Visit)(V ...
- PTA 邻接表存储图的广度优先遍历
试实现邻接表存储图的广度优先遍历. 函数接口定义: void BFS ( LGraph Graph, Vertex S, void (*Visit)(Vertex) ) 其中LGraph是邻接表存储的 ...
- 数据结构学习笔记05图 (邻接矩阵 邻接表-->BFS DFS、最短路径)
数据结构之图 图(Graph) 包含 一组顶点:通常用V (Vertex) 表示顶点集合 一组边:通常用E (Edge) 表示边的集合 边是顶点对:(v, w) ∈E ,其中v, w ∈ V 有向边& ...
随机推荐
- 267. Palindrome Permutation II
题目: Given a string s, return all the palindromic permutations (without duplicates) of it. Return an ...
- 在Hadoop1.2.1分布式集群环境下安装hive0.12
在Hadoop1.2.1分布式集群环境下安装hive0.12 ● 前言: 1. 大家最好通读一遍过后,在理解的基础上再按照步骤搭建. 2. 之前写过两篇<<在VMware下安装Ubuntu ...
- Navicat
create table <表名>( <列名> <数据类型及长度> [not null], <列名> <数据类型及长度>, ...
- Hopcroft-Karp模板学习小结
最开始是因为做了一个题目接触到这个算法的,但是对于这个算法很多资料都只说了大概的方法: 首先从所有X的未盖点进行BFS,BFS之后对每个X节点和Y节点维护距离标号,如果Y节点是未盖点那么就找到了一条最 ...
- TestNG超详细教程
testNG官网:http://testng.org/doc/download.html howtodoinjava.com里的testNG教程,简单详细:http://howtodoinjava.c ...
- Git查看、删除、重命名远程分支和tag【转】
转自:http://zengrong.net/post/1746.htm 本站文章除注明转载外,均为本站原创或者翻译. 本站文章欢迎各种形式的转载,但请18岁以上的转载者注明文章出处,尊重我的劳动,也 ...
- UVA 11019 Matrix Matcher(ac自动机)
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 第四讲 :hibernate中的session
hibernate中的session中可以进行增删改差,通过工具类可以得到相关的工具类. 方法概要: Transaction beginTransaction()开始一个工作单元,得到关联的事务对象 ...
- HibernateTemplate 查询
Spring中常用的hql查询方法getHibernateTemplate()上 一.find(String queryString); 示例:this.getHibernateTempl ...
- ArrayList集合的语句示例
namespace ArrayList集合的语句示例{ class Program { static void Main(string[] args) { ...