学校地图管理功能——C语言的简单实现

学校地图管理功能。该功能使用图形结构进行数据存储,并实现基本初始化、增加、修改、删除、查找功能。

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

#define MAX_NAME_LEN 50
#define MAX_DESC_LEN 100
#define MAX_VERTEX_NUM 100

// 图形结构体
typedef struct {
   
    char name[MAX_NAME_LEN]; // 地点名称
    char desc[MAX_DESC_LEN]; // 地点描述
    int x, y; // 地点坐标
} Vertex;

typedef struct {
   
    int edge[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; // 邻接矩阵
    Vertex vertex[MAX_VERTEX_NUM]; // 顶点数组
    int vertex_num; // 顶点数量
} Graph;

// 初始化图
void init_graph(Graph *graph) {
   
    graph->vertex_num = 0;
    memset(graph->edge, 0, sizeof(graph->edge));
    memset(graph->vertex, 0, sizeof(graph->vertex));
}

// 添加顶点
void add_vertex(Graph *graph, Vertex vertex) {
   
    if (graph->vertex_num >= MAX_VERTEX_NUM) {
   
        printf("顶点数量已达到最大值,无法添加新的顶点!\n");
        return;
    }
    graph->vertex[graph->vertex_num++] = vertex;
}

// 添加边
void add_edge(Graph *graph, int from, int to, int weight) {
   
    graph->edge[from][to] = weight;
}

// 修改顶点
void modify_vertex(Graph *graph, int index, Vertex vertex) {
   
    graph->vertex[index] = vertex;
}

// 删除顶点
void delete_vertex(Graph *graph, int index) {
   
    int i, j;
    // 删除顶点
    for (i = index; i < graph->vertex_num - 1; i++) {
   
        graph->vertex[i] = graph->vertex[i + 1];
    }
    graph->vertex_num--;
    // 删除边
    for (i = 0; i < graph->vertex_num; i++) {
   
        for (j = index; j < graph->vertex_num - 1; j++) {
   
            graph->edge[i][j] = graph->edge[i][j + 1];
        }
        graph->edge[i][graph->vertex_num - 1] = 0;
    }
    for (j = 0; j < graph->vertex_num; j++) {
   
        for (i = index; i < graph->vertex_num - 1; i++) {
   
            graph->edge[i][j] = graph->edge[i + 
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值