图之基于邻接链表的实现

来源:互联网 发布:通话声音分析软件 编辑:程序博客网 时间:2024/06/05 14:25

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

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

看这个图理解结构


#ifndef LINKGRAPH_H_INCLUDED#define LINKGRAPH_H_INCLUDED#define MAX_VEX 30using 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;    }


贴一下运行结果: