图之基于邻接链表的实现

上一篇介绍了图的基本概念和基于邻接矩阵的表示方法,这一篇介绍一下基于邻接链表的实现方法

我们定义头结点作为图的顶点的存储结构,头结点包含数据域、顶点的度、邻接顶点单链表的结点指针;用单链表作为顶点的邻接顶点的存储形式,包含邻接顶点在头结点表中的下标位置权值、以及指向下一表结点的指针;

看这个图理解结构


#ifndef LINKGRAPH_H_INCLUDED
#define LINKGRAPH_H_INCLUDED
#define MAX_VEX 30
using namespace std;
typedef int InfoType;
typedef char VexType;
typedef string GraphKind;
//表结点的定义
typedef struct LinkNode
{
    int adjvex;//邻接结点在头结点中的下标位置;
    InfoType info;//权值等基本信息;
    struct LinkNode *nextarc;//指向下一个表结点;

}LinkNode;
//头结点的定义
typedef struct VexNode
{
    VexType data;
    int degreen;//顶点的度
    LinkNode *firstarc;
}VexNode;
//弧或边的定义
typedef struct ArcType
{
    VexType vex1,vex2;
    InfoType info;//权值
}ArcType;
//图的定义
typedef struct ALGraph
{
    GraphKind kind;
    int vexnum;
    VexNode AdjList[MAX_VEX];
}ALGraph;
//图的创建
void Create_ALGraph(ALGraph *G)
{
    cout<<"输入图的种类:"<<endl;
    string s;
    cin>>s;
    G->kind=s;
    G->vexnum=0;
}
//图的顶点定位
int Locate_ALGraph_Vex(ALGraph *G,VexType *vex)
{
    for(int k=0;k<G->vexnum;k++)
    {
        if(G->AdjList[k].data==*vex)
            return k;
    }
    return -1;
}
//图的插入顶点
int AddVex_ALGraph(ALGraph *G,VexType *vex)
{
    if(G->vexnum>=MAX_VEX)
    {
        cout<<"number overflow"<<endl;
        return -1;
    }
    if(Locate_ALGraph_Vex(G,vex)!=-1)
    {
        cout<<"vex has existed"<<endl;
        return -1;
    }
    G->AdjList[G->vexnum].data=*vex;
    G->AdjList[G->vexnum].degreen=0;
    G->AdjList[G->vexnum].firstarc=NULL;
    G->vexnum++;
    return 1;
}
//图的插入弧
int AddArc_ALGraph(ALGraph *G,ArcType *arc)
{
    //无向图修改一个单链表,有向图两个
    int k,j;
    LinkNode *p,*q;
    k=Locate_ALGraph_Vex(G,&arc->vex1);
    j=Locate_ALGraph_Vex(G,&arc->vex2);
    if(k==-1 || j==-1)
    {
        cout<<"vex has not existed"<<endl;
        return -1;
    }
    p=(LinkNode *)malloc(sizeof(LinkNode));
    p->adjvex=arc->vex1;
    p->info=arc->info;
    p->nextarc=NULL;
    q=(LinkNode *)malloc(sizeof(LinkNode));
    q->adjvex=arc->vex2;
    q->info=arc->info;
    q->nextarc=NULL;
    //
    if(G->kind=="DG" || G->kind=="WDG")//有向图
    {
        //正邻接链表,出度
        q->nextarc=G->AdjList[k].firstarc; //单链表头插入法
        G->AdjList[k].firstarc=q;
    }
    else
    {
        q->nextarc=G->AdjList[k].firstarc; //单链表头插入法
        G->AdjList[k].firstarc=q;
        p->nextarc=G->AdjList[j].firstarc;
        G->AdjList[j].firstarc=p;
    }
    return 1;
}
#endif // LINKGRAPH_H_INCLUDED

测试代码:

cout<<"ALGraph : "<<endl;
    ALGraph *G;
    VexType vexs[4]={'A','B','C','D'};
    ArcType arc;
    G=(ALGraph *)malloc(sizeof(ALGraph));
    Create_ALGraph(G);
    for(int i=0;i<4;i++)
    {
        AddVex_ALGraph(G,&vexs[i]);
    }
    for(int i=0;i<4;i++)
    {
        arc.info=i+1;
        arc.vex1=vexs[i];
        arc.vex2=vexs[(i+1)%4];
        AddArc_ALGraph(G,&arc);
    }
    for(int i=0;i<4;i++)
    {
        cout<<G->AdjList[i].data<<" ";
    }
    cout<<endl;
    for(int i=0;i<4;i++)
    {
        LinkNode *p;
        p=(LinkNode *)malloc(sizeof(LinkNode));
        p=G->AdjList[i].firstarc;
        cout<<G->AdjList[i].data<<"度:"<<G->AdjList[i].degreen<<"    邻接结点:"<<endl;
        while(p!=NULL)
        {
            cout<<G->AdjList[p->adjvex].data<<",权值: "<<p->info<<endl;
            p=p->nextarc;
        }
        cout<<endl;
    }


贴一下运行结果:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值