广东工业大学anyview数据结构(2024)第八章参考答案

注:请注意对应题号,以下内容仅为个人代码,不代表最终答案,仅供参考。

2024.9.26更新:题目DC08PE17的BUG已经修复,现已添加代码

其他章节参考答案

目录

DC08PE12创建有向图的邻接数组存储结构

DC08PE15在图G中,相对于k顶点的当前邻接顶点m,求下一个邻接顶点

DC08PE17在图G中设置顶点v到顶点w的弧或边

DC08PE21计算以邻接表方式存储的有向图G中某顶点的出度

DC08PE22计算以邻接表方式存储的有向图G中某顶点的入度

DC08PE32创建有向图的邻接表存储结构

DC08PE34创建无向图的邻接表存储结构


DC08PE12创建有向图的邻接数组存储结构
Status CreateDG(MGraph &G, VexType *vexs, int n,
                           ArcInfo *arcs, int e) 
{    // Add your code here
    if(n<0 or !vexs or !InitGraph(G, DG, n)) return 0;
    G.e = e; G.n = n;
    for(int i=0; i<G.n; i++) G.vexs[i] = vexs[i];
    for(int i=0; i<G.e; i++) G.arcs[LocateVex(G, arcs[i].v)][LocateVex(G, arcs[i].w)].adj = 1;
    return 1;
}
DC08PE15在图G中,相对于k顶点的当前邻接顶点m,求下一个邻接顶点
int NextAdjVex(MGraph G, int k, int m) 
{   // Add your code here
    if(G.n == 0) return 0;
    if(k < 0 or k > G.n or G.e < 2) return -1;
    for(int i=m+1; i<G.n; i++) {
        if(G.kind == UDG or G.kind == DG and G.arcs[k][i].adj == 1 and i != k) return i;
        if(G.arcs[k][i].adj != INFINITY and i != k) return i;
    }
    return -1;
}
DC08PE17在图G中设置顶点v到顶点w的弧或边
Status SetArc(MGraph &G, VexType v, VexType w, ArcCell info) 
{  // Add your code here
    if(!v or !w) return -1;
    G.arcs[LocateVex(G,v)][LocateVex(G,w)] = info;
    G.e++;
    return 1;
}
DC08PE21计算以邻接表方式存储的有向图G中某顶点的出度
int outDegree(ALGraph G, int k) 
{  // Add your code here
    if(k < 0 or k > G.n) return -1;
    int count = 0;
    AdjVexNode *node = G.vexs[k].firstArc;
    while(node){
        count++;
        node = node->next;
    }
    return count;
}
DC08PE22计算以邻接表方式存储的有向图G中某顶点的入度
int inDegree(ALGraph G, int k) 
{   // Add your code here
    if(k<0 or k>G.n) return -1;
    int count = 0;
    for(int i=0; i<G.n; i++) {
        if(i == k) continue;
        AdjVexNode *node = G.vexs[i].firstArc;
        while(node){
            if(node->adjvex == k) {
                count++;
                break;
            }
            node = node->next;
        }
    }
    return count;
}
DC08PE32创建有向图的邻接表存储结构
Status CreateDG(ALGraph &G, VexType *vexs, int n,
                            ArcInfo *arcs, int e) 
{  // Add your code here
    if(n<0 or !vexs) return -1;
    G.n = n; G.e = e;
    G.kind = DG;
    for(int i=0; i<n; i++) {
        G.vexs[i].data = vexs[i];
        G.vexs[i].firstArc = NULL;
    }
    for(int i=0; i<n; i++) {
        for(int j=0; j<n; j++) {
            if(G.vexs[i].data == arcs[j].v) {
                AdjVexNode *node = G.vexs[i].firstArc;
                if(!node) {
                    node = (AdjVexNode *)malloc(sizeof(AdjVexNode));
                    node->adjvex = arcs[j].w;
                    node->next = NULL;
                }
                else{
                    while(node->next) node = node->next;
                    AdjVexNode *w = (AdjVexNode *)malloc(sizeof(AdjVexNode));
                    w->adjvex = arcs[j].w;
                    w->next = NULL;
                    node->next = w;
                }
            }
        }
    }
    return 1;
}
DC08PE34创建无向图的邻接表存储结构
Status CreateUDG(ALGraph &G, VexType *vexs, int n,
                             ArcInfo *arcs, int e) 
{  // Add your code here
    if(n<0 or !vexs) return -1;
    G.n = n; G.e = e;
    G.kind = UDG;
    for(int i=0; i<n; i++) {
        G.vexs[i].data = vexs[i];
        G.vexs[i].firstArc = NULL;
    }
    for(int i=0; i<n; i++) {
        for(int j=0; j<e; j++) {
            if(G.vexs[i].data == arcs[j].v){
                if(!G.vexs[i].firstArc) {
                    AdjVexNode *node = (AdjVexNode *)malloc(sizeof(AdjVexNode));
                    node->adjvex = arcs[j].w;
                    node->next = NULL;
                }
                else {
                    AdjVexNode *node = G.vexs[i].firstArc;
                    while(node->next) node = node->next;
                    AdjVexNode *w = (AdjVexNode *)malloc(sizeof(AdjVexNode));
                    w->adjvex = arcs[j].w;
                    w->next = NULL;
                    node->next = w;
                }
            }
            if(G.vexs[i].data == arcs[j].w){
                if(!G.vexs[i].firstArc) {
                    AdjVexNode *node = (AdjVexNode *)malloc(sizeof(AdjVexNode));
                    node->adjvex = arcs[j].v;
                    node->next = NULL;
                }
                else {
                    AdjVexNode *node = G.vexs[i].firstArc;
                    while(node->next) node = node->next;
                    AdjVexNode *w = (AdjVexNode *)malloc(sizeof(AdjVexNode));
                    w->adjvex = arcs[j].v;
                    w->next = NULL;
                    node->next = w;
                }
            }
        }
    }
    return 1;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值