无向图 图的创建 输出邻接表 邻接矩阵

注:本程序邻接矩阵没有放进图结构体里

#include <stdio.h>
#include <stdlib.h>
#define MAXIMUM 100

typedef struct EdgeNode//弧(vi,vj)结点
{
	int vertexnum;//弧尾序号j
	struct EdgeNode *nextEdge;
}EdgeNode;

typedef struct VertexNode//顶点v结点
{
	char vertex;//顶点v的信息
	EdgeNode *FirstEdge;
}VertexNode[MAXIMUM];

typedef struct Graph
{
	VertexNode adjList;//顶点数组集
	int amountofvertex;//顶点数
	int amountofedge;//弧数
}Graph;

void InitGraph(Graph *G)//图的创建
{
	int i,j,k;
	EdgeNode *p;

	printf("请输入图的顶点数\n");	
	scanf("%d",&G->amountofvertex);
	printf("请输入图的边数\n");
	scanf("%d",&G->amountofedge);

	printf("请按顺序插入顶点信息(以空格隔开)\n");
	for(k=0;k<G->amountofvertex;k++)//插入顶点信息
	{
		getchar();
		scanf("%c",&G->adjList[k].vertex);
		G->adjList[k].FirstEdge=NULL;	//初始化顶点结构体指针为空指针(指向NULL)
	}

	printf("请按顺序输入边的两个顶点下标(vi,vj)(i和j以空格隔开)\n");
	printf("一行输入一条边\n");
	for(k=0;k<G->amountofedge;k++)//插入顶点信息
	{
		getchar();
		scanf("%d%d",&i,&j);
		p=(EdgeNode *)malloc(sizeof(EdgeNode));
		p->vertexnum=j;						//储存弧尾(邻接点)下标
		p->nextEdge=G->adjList[i].FirstEdge;	//头插法
		G->adjList[i].FirstEdge=p;			

		p=(EdgeNode *)malloc(sizeof(EdgeNode));
		p->vertexnum=i;						//储存弧头(邻接点)下标
		p->nextEdge=G->adjList[j].FirstEdge;	//头插法
		G->adjList[j].FirstEdge=p;			
	}
	printf("\n");
}

void printadjGraph(Graph *G)//输出邻接表
{
	int i,j,k;
	EdgeNode *p;
	printf("邻接表:\n");
	for(k=0;k<G->amountofvertex;k++)
	{
		for(p=G->adjList[k].FirstEdge;p;p=p->nextEdge)
			printf("(%c,%c)",G->adjList[k].vertex,G->adjList[p->vertexnum].vertex);//打印边(如(A,B))
		printf("\n");
	}
	printf("\n");
}

void printadjMatrix(Graph *G)//输出邻接矩阵
{
	int adjMatrix[MAXIMUM][MAXIMUM]={0};
	int i,j,k;
	EdgeNode *p;

	for(k=0;k<G->amountofvertex;k++)
	{
		for(p=G->adjList[k].FirstEdge;p;p=p->nextEdge)
			adjMatrix[k][p->vertexnum]=1;//相连顶点赋值为1
	}

	printf("邻接矩阵:\n\n   ");
	for(i=0;i<G->amountofvertex;i++)//打印列头
		printf("%3c",G->adjList[i].vertex);
	printf("\n\n");

	for(i=0;i<G->amountofvertex;i++)
	{
		printf("%2c|",G->adjList[i].vertex);//打印行头
		for(j=0;j<G->amountofvertex;j++)
			printf("%3d",adjMatrix[i][j]);//输出矩阵
		printf("|");
		printf("\n");
	}
	printf("\n");
}

int main(void)
{
	Graph G;
	InitGraph(&G);
	printadjGraph(&G);
	printadjMatrix(&G);
	return 0;
}

在这里插入图片描述

  • 7
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
好的,我可以为您提供一个 Python 代码示例,用于交互式建立无向图邻接表表示。代码如下: ```python class Graph: def __init__(self, vertices): self.vertices = vertices self.adj_list = {} for v in vertices: self.adj_list[v] = [] def add_edge(self, u, v): self.adj_list[u].append(v) self.adj_list[v].append(u) def display(self): for vertex in self.vertices: print(vertex, ':', ' -> '.join([str(i) for i in self.adj_list[vertex]])) vertices = [] num_vertices = int(input('Enter the number of vertices in the graph: ')) for i in range(num_vertices): vertex = input(f'Enter vertex {i+1}: ') vertices.append(vertex) g = Graph(vertices) while True: print('1. Add an edge') print('2. Display the graph') print('3. Exit') choice = int(input('Enter your choice: ')) if choice == 1: u = input('Enter vertex u: ') v = input('Enter vertex v: ') g.add_edge(u, v) elif choice == 2: g.display() elif choice == 3: break ``` 该代码首先定义了一个 `Graph` 类,以及它的成员变量 `vertices` 和 `adj_list`,分别表示中的顶点和邻接表。在类的构造函数中,我们初始化了邻接表,并将每个顶点的邻接表初始化为空列表。 接下来,通过 `add_edge` 方法,我们可以向中添加一条边,该方法将 `u` 和 `v` 两个顶点加入彼此的邻接表中。注意到这里是无向图,因此我们需要将边加入两个顶点的邻接表中。 最后,我们在一个无限循环中,提供了三个选项:添加边,显示和退出程序。用户可以选择相应的选项进行操作,例如添加边时需要输入两个顶点的名称。显示时,我们遍历每个顶点的邻接表,并将其打印出来。 希望这个示例能够帮助您理解如何使用交互式方式建立无向图邻接表表示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值