邻接表

邻接表_哔哩哔哩_bilibili

 

// 图的存储:邻接表
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

#define MAXV 100 //最大顶点数

//边结点的定义
typedef struct ArcNode{
    int adjvex; //邻接点的下标
    int weight; //边的权值
    struct ArcNode *next; //指向下一个边结点
} ArcNode;

//顶点结点的定义
typedef struct{
    char data;      //顶点信息
    ArcNode *first; //指向该顶点的第一个边结点
} VexNode,AdjList[MAXV];

//邻接表的图类型定义
typedef struct{
    AdjList adjList;   //邻接表
    int vexnum,arcnum; //顶点数,边数
} ALGraph;

int main(){
    ALGraph G;
    G.vexnum=4;
    G.arcnum=3;
    G.adjList[0].data='A';
    G.adjList[1].data='B';
    G.adjList[2].data='C';
    G.adjList[3].data='D';
    
    ArcNode *p=(ArcNode*)malloc(sizeof(ArcNode));
    G.adjList[0].first=p;
    p->adjvex=1; //AB
    
    p=(ArcNode*)malloc(sizeof(ArcNode));
    G.adjList[0].first->next=p;
    p->adjvex=2; //AC

    p=(ArcNode*)malloc(sizeof(ArcNode));
    G.adjList[0].first->next->next=p;
    p->adjvex=3; //AD
    
    for(ArcNode *p=G.adjList[0].first; p; p=p->next)
        printf("%c ",G.adjList[p->adjvex].data); //输出A的邻接点
    return 0;
}

 

.

posted @ 2026-10-11 07:02  董晓  阅读(32)  评论(0)    收藏  举报