图的邻接矩阵、邻接表的表示

本文简要介绍了图的两种表示方法——邻接矩阵和邻接表,并指出程序中处理图的权重时存在的问题。针对权重只能存储1位数的情况,提出了初始化weight为0以及修改权重获取方式的解决方案,使得weight可以存储int范围内的任意正整数值。

关于邻接矩阵、邻接表的具体包含哪些数据我就不说了,鄙人时间有点赶,大家自行百度下哈。

程序中图的例子:
这里写图片描述


2.1号:补充,突然发现我的程序,边的权重只能为1位数,如果权重为多位数,只取个位数的值,因此需要进行下面的修改:(邻接矩阵、邻接表都一样)
1) weight初始化为0(以前没有初始化)
2)从控制台得到:weight = weight * 10 + int(c - ‘0’);(以前为:weight = int(c - ‘0’);)
这样weight就可以是int范围内的任意正整数值了。

GNode.h

#define MAXSIZE 10
//邻接矩阵
typedef struct AMGraph {
    char** vexs;
    int** arcs;
    int vexnum, arcnum;
}AMGraph;


//邻接表

typedef struct OtherInfo {
    //权重等
    int weight;
}OtherInfo;

//边结点
typedef struct ArcNode {
    int index;
    struct ArcNode* next;
    OtherInfo info;
}ArcNode;


//表头结点
typedef struct VNode {
    char* data;
    struct ArcNode* first;
}VNode;

typedef struct ALGraph {
    VNode* vertices;
    int vexnum, arcnum;
}ALGraph;

main.cpp

#include<stdio.h>
#include<stdlib.h>
#include <string.h>

#include"GNode.h"

//邻接矩阵
void CreateAMGraph(AMGraph *g);     //创建邻接矩阵
void GetIndex(char** vex, int size, char* resource, int &result);   //根据输入顶点信息得到顶点的索引
bool Equal(char* s1, char* s2);     //判断两个字符串是否相等
void PrintAMGraphMatrix(AMGraph *g);        //输出邻接矩阵

//邻接表
void CreateALGraph(ALGraph* g);     //创建邻接表
void GetIndex_s(ALGraph* g, int size, char* resource, int &result); //根据输入顶点信息得到顶点的索引
void PrintfALGraph(ALGraph* g);     //输出邻接表


int main() {

    //邻接矩阵
    printf("*******邻接矩阵******:\n");
    AMGraph* g;
    g = (AMGraph*)malloc(sizeof(AMGraph));
    CreateAMGraph(g);       //创建邻接矩阵
    PrintAMGraphMatrix(g);      //输出矩阵

  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值