C语言数据结构-图-邻接表1-试在邻接表存储结构上实现图的基本操作 insert_vertex 和 insert_arc

邻接表1

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

//顶点为int
typedef int VertexType;

//图的种类:DG 表示有向图, DN 表示有向网, UDG 表示无向图, UDN 表示无向网
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){


}

参考答案

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

bool insert_arc(ListGraph* G, VertexType v, VertexType w)
{
    if (locate_vertex(G, v) == -1 || locate_vertex(G, w) == -1)
        return false;
    return true;
}
bool insert_arc(ListGraph* G, VertexType v, VertexType w)
{
    if (locate_vertex(G, v) == -1 || locate_vertex(G, w) == -1)
        return false;

    int V = locate_vertex(G, v);
    int W = locate_vertex(G, w);

    if (G->type == "DG")
    {
        ArcNode* p = G->vertex[V].firstarc;
        ArcNode* NewNodeV = (ArcNode*)malloc(sizeof(struct ArcNode));
        while (p->nextarc != NULL) {
            p = p->nextarc;
        }
        p->nextarc = NewNodeV;
        NewNodeV->adjvex = W;
        NewNodeV->nextarc = NULL;
        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));
        while (p->nextarc != NULL) {
            p = p->nextarc;
        }
        while (q->nextarc != NULL) {
            q = q->nextarc;
        }
        p->nextarc = NewNodeV;
        NewNodeV->adjvex = W;
        NewNodeV->nextarc = NULL;

        q->nextarc = NewNodeV;
        NewNodeV->adjvex = V;
        NewNodeV->nextarc = NULL;
        G->arcnum+=2;
    }
    return true;
}

 

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值