邻接表求各顶点入度之和

#include"ALGraph.h"
//邻接表法求节点的入度
void InDegree2(ALGraph* G, const char* v)
{
	int i, j;//i用于获取所给顶点的下标
	int sum = 0;//sum用于求给出顶点的入度之和
	i = get_ALpos(G, v);
	ArcNode* p;
	for (j = 0; j < G->vexnum; j++)
	{
		p = G->vertex[j].nextarc;
		while (p != NULL)
		{
			if (p->index == i)
			{
				sum += p->weight;
				p = p->next;
			}
			else
			{
				p = p->next;
			}
		}
		
	}
	printf("\n%s的入度之和为%d",v,sum);
}
//求所有顶点的入度之和
void Allindegree2(ALGraph* G)
{
	for (int i = 0; i < G->vexnum; i++)
	{
		InDegree2(G,G->vertex[i].data);
	}
}
int main(int argc, char* argv[])
{
	ALGraph* g;
	CreateALGraph(g);
	PrintALGraph(g);
	//求图G中每个顶点的入度
	Allindegree2(g);
	DestroyALGraph(g);
	return 0;
}

在这里插入图片描述

头文件和其它函数

//此为有向带权图的创建
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
#include<string.h>
#define MAX 100
//定义边节点
typedef struct ArcNode
{
	int index;//用作下标的标签来控制相关操作
	ArcNode* next;//定义下一条边的指针
	int weight;
}ArcNode;
//定义顶点节点
typedef struct Vnode
{
	char data[20];
	//若为带权值的话则加上下面这句
	//int weight;
	ArcNode* nextarc;//为指向的边
}VNode;
//定义邻接表
typedef struct
{
	int arcnum;//定义边数
	int vexnum;//定义顶点数
	VNode vertex[MAX];//定义邻接数组(实质上是顶点数组)
}ALGraph;
//声明
//根据传回来的下标值来创建边节点
ArcNode* Create_Arcnode(int index, int weight);
//用返回来的下标创建边节点再进行链接的操作
void insert_arcnode(ArcNode** headnode, int index, int weight);//传入头结点的指针,再进行相应的修改操作(指向NULL指针的指针的知识/二重指针)
//定位下标
int get_ALpos(ALGraph* G, const char* v);
//图的创建
void CreateALGraph(ALGraph*& G);
//图的打印输出
void PrintALGraph(ALGraph* G);
void DestroyALGraph(ALGraph* G);


#include"ALGraph.h"
//根据传回来的下标值来创建边节点
ArcNode* Create_Arcnode(int index,int weight)
{
	ArcNode* newnode = (ArcNode*)malloc(sizeof(ArcNode));
	assert(newnode);
	newnode->index = index;
	newnode->weight = weight;
	return newnode;
}
//用返回来的下标创建边节点再进行链接的操作
void insert_arcnode(ArcNode** headnode, int index,int weight )//传入头结点的指针,再进行相应的修改操作(指向NULL指针的指针的知识/二重指针)
{
	ArcNode* newnode = Create_Arcnode(index,weight);
	newnode->next = *headnode;
	*headnode = newnode;
}
//定位下标
int get_ALpos(ALGraph* G, const char* v)
{
	for (int i = 0; i < G->vexnum; i++)
	{
		if (strcmp(G->vertex[i].data, v) == 0)
		{
			return i;
		}
	}
	return -1;
}
//图的创建
void CreateALGraph(ALGraph*& G)
{
	G = (ALGraph*)malloc(sizeof(ALGraph));
	assert(G);
	printf("请输入边数和顶点数(用逗号间隔):");
	scanf_s("%d,%d", &G->arcnum, &G->vexnum);
	printf("请输入顶点信息(用空格间隔):");
	for (int i = 0; i < G->vexnum; i++)
	{
		scanf_s("%s", G->vertex[i].data, 20);
		G->vertex[i].nextarc = NULL;
	}
	char v1[20] = "";
	char v2[20] = "";
	int i, j,weight=0;
	for (int k = 0; k < G->arcnum; k++)
	{
		printf("请输入第%d条边的起点和终点和权值(均用空格间隔):", k + 1);
		scanf_s("%s%s%d", v1, 20, v2, 20,&weight);
		i = get_ALpos(G, v1);
		j = get_ALpos(G, v2);
		if (i == -1 || j == -1)
		{
			printf("边的输入有误,请重新输入\n");
			k--;
			continue;
		}
		insert_arcnode(&G->vertex[i].nextarc, j,weight); // 意为在顶点中添加了一条从顶点i指向顶点j的边
		//无向图则做两次操作
		//insert_arcnode(&G->vertex[j].nextarc, i);//意为在顶点中添加了一条从顶点j指向顶点i的边
		//带权图的话则赋上权值
		
	}

}
#include"ALGraph.h"
//图的打印输出
void PrintALGraph(ALGraph* G)
{
	for (int i = 0; i < G->vexnum; i++)
	{//先将顶点打印出来,再打印邻接的边的相关信息
		printf("%s:\t", G->vertex[i].data);
		ArcNode* current = G->vertex[i].nextarc;
		while (current != NULL)
		{
			int index = current->index;//用于记录边终点下标
			printf("%s(%d)\t", G->vertex[index].data, current->weight);//G->vertex[i].nextarc->
			current = current->next;
		}
		printf("\n");
	}
}
void DestroyALGraph(ALGraph* G)
{
	free(G);
}
/*
int main(int argc, char* argv[])
{
	ALGraph* G;
	CreateALGraph(G);
	PrintALGraph(G);
	DestroyALGraph(G);
	return 0;
}
*/


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值