邻接表存储的实现(改进版)

邻接表的实现(改进版)

     这次代码是在上一次实现邻接表的基础上进行的改进,减少了一个类(用于存储横向结构即直接相邻节点的类),同时在Headlinklist增设了一个Topoint类的end指针用于为每个出发点插入相应的邻接节点,较上次代码来说减少了很多指针,结构上大大简化,降低了思维难度,比较容易想清楚结构。

矩阵中存储的图的结构如下:

实现代码如下:

#include <iostream>
#include <string.h>
using namespace std;
#define WHITE -1;
#define GRAY 0;
#define BLACK 1;
class Topoint
{
    public:
    string topointname;
    int topointweight;
    int topointcolor;
    Topoint* nexto;
    Topoint()
    {
        nexto=NULL;
    }
    Topoint(string _topointname,int _topointweight,int _topointcolor)
    {
        topointname=_topointname;
        topointweight=_topointweight;
        topointcolor=_topointcolor;
        nexto=NULL;
    }
    void Display()
    {
        cout<<"ToPoint name: "<<topointname<<" weight: "<<topointweight<<" pointcolor: "<<topointcolor<<endl;
    }
};
class Frompoint
{
    public:
    string frompointname;
    int frompointcolor;
    Frompoint* nextpoint;
    Topoint* nextopoint;
    Frompoint()
    {
        nextpoint=NULL;
        nextopoint=NULL;
    }
    Frompoint(string _frompointname,int _frompointcolor)
    {
        frompointname=_frompointname;
        frompointcolor=_frompointcolor;
        nextopoint=NULL;
        nextpoint=NULL;
    }
    void Display()
    {
        cout<<"Frompoint name: "<<frompointname<<" pointcolor: "<<frompointcolor<<endl;
    }
};
class Headlinklist
{
    public:
    Frompoint* head;
    Frompoint* rear;
    Topoint* end;
    Headlinklist()
    {
        head=NULL;
        rear=NULL;
        end=NULL;
    }
    int InsertFrompoint(string frompointname,int frompointcolor)              //竖直方向添加节点
    {
        Frompoint* p=new Frompoint(frompointname,frompointcolor);
        if(head==NULL)
        {
            head=rear=p;
            return 1;
        }
        else
        {
            rear->nextpoint=p;
            rear=rear->nextpoint;
            return 1;
        }
        return 0;
    }
    int InsertTopoint(string _topointname,int _topointweight,int _topointcolor)         //横向添加节点
    {
        Topoint* p=new Topoint(_topointname,_topointweight,_topointcolor);
        if(rear->nextopoint==NULL)
        {
            rear->nextopoint=p;
            end=p;
            return 1;
        }
        else
        {
            end->nexto=p;
            end=end->nexto;
            return 1;
        }
        return 0;
    }
    void Display()
    {
        Frompoint* p=head;
        while(p!=NULL)
        {
            cout<<"出发节点:"<<endl;
            p->Display();
            Topoint* q=p->nextopoint;
            cout<<"直接邻接节点:"<<endl;
            while(q!=NULL)
            {
                q->Display();
                q=q->nexto;
            }
            p=p->nextpoint;
        }
    }
};
int main()
{
    Headlinklist head;

    head.InsertFrompoint("1",-1);
    head.InsertTopoint("2",1,-1);
    head.InsertTopoint("5",1,-1);

    head.InsertFrompoint("2",-1);
    head.InsertTopoint("1",1,-1);
    head.InsertTopoint("3",1,-1);
    head.InsertTopoint("4",1,-1);
    head.InsertTopoint("5",1,-1);

    head.InsertFrompoint("3",-1);
    head.InsertTopoint("2",1,-1);
    head.InsertTopoint("4",1,-1);

    head.InsertFrompoint("4",-1);
    head.InsertTopoint("2",1,-1);
    head.InsertTopoint("3",1,-1);
    head.InsertTopoint("5",1,-1);

    head.InsertFrompoint("5",-1);
    head.InsertTopoint("1",1,-1);
    head.InsertTopoint("2",1,-1);
    head.InsertTopoint("4",1,-1);

    head.Display();
    return 0;
}

输出结果如下:

 

 

posted @ 2012-12-30 11:47  再见,少年  Views(292)  Comments(0Edit  收藏  举报