ICODING-数据结构-图的存储-邻接表1(C语言)

题目
网上可以找到的本题答案通过率都在90%以下,于是乎有了本篇博客


邻接表1

试在邻接表存储结构上实现图的基本操作 insert_vertex 和 insert_arc,相关定义如下:

typedef int VertexType;

typedef enum{
    DG, UDG
}GraphType;

typedef struct ArcNode
{
    int adjvex;
    InfoPtr *info;
    struct ArcNode *nextarc;

}ArcNode;

typedef struct VNode
{
    VertexType data;
    ArcNode *firstarc;
}VNode;
typedef struct
{
    VNode vertex[MAX_VERTEX_NUM];
    int vexnum, arcnum;
    GraphType type;
}ListGraph;

int locate_vertex(ListGraph* G, VertexType v); //返回顶点 v 在vertex数组中的下标,如果v不存在,返回-1
bool insert_vertex(ListGraph *G, VertexType v);
bool insert_arc(ListGraph *G, VertexType v, VertexType w);

当成功插入顶点或边时,函数返回true,否则(如顶点或边已存在、插入边时顶点v或w不存在)返回false。

题目代码:

#include <stdio.h>
#include "graph.h" //请勿删除,否则检查不通过

bool insert_vertex(ListGraph* G, VertexType v) {

}

bool insert_arc(ListGraph* G, VertexType v, VertexType w) {

}

第一部分insert_vertex(ListGraph* G, VertexType v)函数比较简单,直接给出答案

bool insert_vertex(ListGraph* G, VertexType v)
{
    if (locate_vertex(G, v) != -1 || G->vexnum == MAX_VERTEX_NUM)
        return false;
    G->vertex[G->vexnum].data = v;
    G->vertex[G->vexnum].firstarc = NULL;
    G->vexnum++;
    return true;
}

不知道是icoding改了题还是如何,网上找到的所有答案通过率都在90%以下,有的还会报错爆红。仔细思考后发现主要有2个问题

  1. G->vertex[V].firstarc可能为NULL导致segmentation fault
  2. 未判断待插入边是否存在导致异常、边数不正确等

因此,需要在原有代码上加上判断G->vertex[V].firstarc是否为空和判断待插入边是否存在的部分。本题有时候会报错显示头文件缺失,在前面加上#include <stdlib.h>即可
代码如下:

bool insert_arc(ListGraph* G, VertexType v, VertexType w)
{
    if (locate_vertex(G, v) == -1 || locate_vertex(G, w) == -1)//v或者w不存在的情况
        return false;
 
    int V = locate_vertex(G, v);//v和w的下标
    int W = locate_vertex(G, w);
 
    if (G->type == 0)//有向图
    {
        ArcNode* p = G->vertex[V].firstarc;
        ArcNode* NewNodeV = (ArcNode*)malloc(sizeof(struct ArcNode));
        NewNodeV->adjvex = W;
        NewNodeV->nextarc = NULL;
        if(p != NULL)
        {
            while (p->nextarc != NULL)//遍历到当前表尾
            {
                if(p->adjvex == W)
                    return false;
                p = p->nextarc;
            }
            if(p->adjvex == W)//如果v后只连了一个节点,上面的判断就进不去,因此额外判断v后这一个节点是否为w
                return false;
            p->nextarc = NewNodeV;
        }
        else//v没有连接到任何节点
        {
            G->vertex[V].firstarc = NewNodeV;
        }
        G->arcnum ++;
    }
    else//与有向图思路一致
    {
        ArcNode* p = G->vertex[V].firstarc;
        ArcNode* q = G->vertex[W].firstarc;
        ArcNode* NewNodeV = (ArcNode*)malloc(sizeof(struct ArcNode));
        ArcNode* NewNodeW = (ArcNode*)malloc(sizeof(struct ArcNode));
        NewNodeV->adjvex = W;
        NewNodeV->nextarc = NULL;
        NewNodeW->adjvex = V;
        NewNodeW->nextarc = NULL;
        
        if(p != NULL){
            while (p->nextarc != NULL)
            {
                if(p->adjvex == W)
                    return false;
                p = p->nextarc;
            }
            if(p->adjvex == W)
                return false;
            p->nextarc = NewNodeV;
        }
        else
        {
            // if(p->adjvex == W)
            //     return false;
            G->vertex[V].firstarc = NewNodeV;
        }
        
        if(q != NULL){
            while (q->nextarc != NULL)
            {
                if(q->adjvex == V)
                    return false;
                q = q->nextarc;
            }
            if(q->adjvex == V)
                return false;
            q->nextarc = NewNodeW;
        }
        else
        {
            // if(q->adjvex == V)
            //     return false;
            G->vertex[W].firstarc = NewNodeV;
        }
        G->arcnum+=2;
    }
    return true;
}

不出意外这段代码通过率是100%
认真思考,不要盲目icopy哦~

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值