6-3 采用邻接表创建无向图

采用邻接表创建无向图G ,依次输出各顶点的度。

函数接口定义:

void CreateUDG(ALGraph &G);
#include <stdio.h>
#include <stdlib.h>
#define MVNum 100 
typedef struct ArcNode{
    int adjvex; 
    struct ArcNode *nextarc; 
    int info; 
}ArcNode; 

typedef struct VNode{ 
    char data; 
    ArcNode *firstarc; 
}VNode, AdjList[MVNum]; 

typedef struct{ 
    VNode vertices[MVNum]; 
    int vexnum, arcnum; 
}ALGraph;

void CreateUDG(ALGraph &G);

int main(){
    ALGraph G;     
    int i , j,sum=0;
    CreateUDG(G);
    ArcNode * p; 
    for(i = 0 ; i < G.vexnum ; ++i){
        sum=0;
        p=G.vertices[i].firstarc;
        for(; p!=NULL; p=p->nextarc){
                sum+=1;
        }
        if(i==0)
            printf("%d",sum);
        else
            printf(" %d",sum);
    }
    return 0;
}

/* 请在这里填写答案 */

输入格式:

输入第一行中给出2个整数i(0<i≤10),j(j≥0),分别为图G的顶点数和边数。
输入第二行为顶点的信息,每个顶点只能用一个字符表示。
依次输入j行,每行输入一条边依附的顶点。

输出格式:

依次输出各顶点的度,行末没有最后的空格。

输入样例:

5 7
ABCDE
AB
AD
BC
BE
CD
CE
DE

输出样例:

2 3 3 3 3
void CreateUDG(ALGraph &G){
    scanf("%d %d",&G.vexnum,&G.arcnum);
    getchar();
    int i;
    char c;
    for(i=0;i<G.vexnum;i++){
        scanf("%c",&c);
        G.vertices[i].data=c;
        G.vertices[i].firstarc=NULL;
    }
    getchar();
    char vex1,vex2;
    int vexnum1,vexnum2;
    int j=0;
    for(i=0;i<G.arcnum;i++){
        scanf("%c%c",&vex1,&vex2);
        getchar();
        for(j=0;j<G.vexnum;j++){
            if(G.vertices[j].data==vex1)vexnum1=j;
            if(G.vertices[j].data==vex2)vexnum2=j;
        }
        ArcNode* p;
        p=G.vertices[vexnum1].firstarc;
        if(!p){
            G.vertices[vexnum1].firstarc=(ArcNode*)malloc(sizeof(ArcNode));
            G.vertices[vexnum1].firstarc->adjvex=vexnum2;
        }else{
            while(p->nextarc){
                p=p->nextarc;
            }
            ArcNode* m=(ArcNode*)malloc(sizeof(ArcNode));
            m->adjvex=vexnum2;
            m->nextarc=NULL;
            p->nextarc=m;
        }
        //ArcNode* p;
        p=G.vertices[vexnum2].firstarc;
        if(!p){
            G.vertices[vexnum2].firstarc=(ArcNode*)malloc(sizeof(ArcNode));
            G.vertices[vexnum2].firstarc->adjvex=vexnum1;
        }else{
            while(p->nextarc){
                p=p->nextarc;
            }
            ArcNode* m=(ArcNode*)malloc(sizeof(ArcNode));
            m->adjvex=vexnum1;
            m->nextarc=NULL;
            p->nextarc=m;
        }
    }
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是使用邻接表创建无向图的示例代码,具体实现思路如下: 1. 定义一个结构体作为邻接表中的节点,该节点包含两个成员变量:顶点编号和指向下一个节点的指针。 2. 创建一个大小为n的邻接表数组adjList,其中n为顶点个数。 3. 遍历边数组edges,将每条边的两个顶点分别添加到对方的邻接表中。 4. 为每个节点的邻接表添加一个哨兵节点,以方便遍历。 ```c #include <stdio.h> #include <stdlib.h> // 邻接表中的节点 typedef struct Node { int vertex; // 顶点编号 struct Node* next; // 指向下一个节点的指针 } Node; // 创建无向图邻接表 void createGraph(Node** adjList, int n, int m, int* edges[]) { // 初始化邻接表 for (int i = 0; i < n; i++) { adjList[i] = (Node*)malloc(sizeof(Node)); adjList[i]->vertex = i; adjList[i]->next = NULL; } // 添加边到邻接表中 for (int i = 0; i < m; i++) { int u = edges[i][0]; int v = edges[i][1]; // 添加v到u的邻接表中 Node* node = (Node*)malloc(sizeof(Node)); node->vertex = v; node->next = adjList[u]->next; adjList[u]->next = node; // 添加u到v的邻接表中 node = (Node*)malloc(sizeof(Node)); node->vertex = u; node->next = adjList[v]->next; adjList[v]->next = node; } // 为每个节点的邻接表添加哨兵节点 for (int i = 0; i < n; i++) { Node* sentinel = (Node*)malloc(sizeof(Node)); sentinel->vertex = -1; sentinel->next = adjList[i]->next; adjList[i]->next = sentinel; } } // 输出邻接表 void printGraph(Node** adjList, int n) { for (int i = 0; i < n; i++) { printf("Node %d:", i); Node* p = adjList[i]->next; while (p != NULL) { printf(" %d", p->vertex); p = p->next; } printf("\n"); } } int main() { int n = 5; // 顶点个数 int m = 7; // 边数 int* edges[7] = {{0, 1}, {0, 4}, {1, 2}, {1, 3}, {1, 4}, {2, 3}, {3, 4}}; // 边数组 // 创建邻接表 Node** adjList = (Node**)malloc(n * sizeof(Node*)); createGraph(adjList, n, m, edges); // 输出邻接表 printGraph(adjList, n); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值