C++邻接表实现无向图、有向图

来源:互联网 发布:手机比特币挖矿软件 编辑:程序博客网 时间:2024/05/16 07:58
/*********************邻接表实现有向图******************///邻接表实现有向图缺点:只有出度,没有入度;解决方法:逆邻接表、十字链表//边节点类template<class Type>class EdgeNode{public:    int adjvex;//邻节点域,存储该节点对应的下标    //int weight;//用于存储权值,非网图不需要    EdgeNode *next;//链域,指向下一个邻节点    //EdgeNode(){}    EdgeNode(int adj,  EdgeNode *n = nullptr) :adjvex(adj),  next(n){}};//顶点表节点类template<class Type>class VertexNode{public:    Type data;//顶点表域,存储顶点信息    EdgeNode<Type> *firstedge;//边表头指针    VertexNode(){}    VertexNode(Type d, EdgeNode<Type> *fir = nullptr) :data(d), firstedge(fir){}};//无向图类template<class Type>class UndirectedGraph{private:    VertexNode<Type> AdjList[MAX];//顶点数组    int numVertexes, numEdges;//图中当前顶点数和边数    Type readType();//外界读取一个Type型数据    int getPosition(Type el);//获取el在邻接表表头节点的位置public:    UndirectedGraph();//创建图(自己输入)    ~UndirectedGraph();//析构函数    void print();//打印邻接表};//获取el在邻接表表头节点的位置template<class Type>int UndirectedGraph<Type>::getPosition(Type el){    for (int i = 0; i < this->numVertexes; i++)    {        if (this->AdjList[i].data == el)            return i;    }    return -1;}//外界读取一个Type型数据template<class Type>Type UndirectedGraph<Type>::readType(){    cout << "input a data: ";    Type el;    cin >> el;    return el;}//创建图template<class Type>UndirectedGraph<Type>::UndirectedGraph(){    Type c1, c2;    int p1, p2;    EdgeNode<Type> *enode1, *enode2;    cout << "input vertex number: ";    cin >> this->numVertexes;    cout << "input edge number: ";    cin >> this->numEdges;    //判断输入节点数与边数是否合法    if (this->numVertexes < 1 || this->numEdges<1 || this->numEdges>this->numVertexes*(this->numVertexes - 1))    {        cout << "input error: invalid input!" << endl;        return;    }    //初始化顶点表    for (int i = 0; i < this->numVertexes; i++)    {        cout << "vertex(" << i << "): ";        cin >> this->AdjList[i].data;        this->AdjList[i].firstedge = nullptr;    }    //初始化边表    for (int i = 0; i < this->numEdges; i++)    {        cout << "edge " << i << endl;        c1 = readType();        c2 = readType();        p1 = getPosition(c1);        p2 = getPosition(c2);        if (p1 == -1 || p2 == -1)        {            cout << "input error!" << endl;            return;        }        enode1 = new EdgeNode<Type>(p2, nullptr);         if (this->AdjList[p1].firstedge == nullptr)            this->AdjList[p1].firstedge = enode1;        else        {            EdgeNode<Type> *move = this->AdjList[p1].firstedge;//这里要注意            while (move->next != nullptr)                move = move->next;            move->next = enode1;        }        enode2 = new EdgeNode<Type>(p1, nullptr);        if (this->AdjList[p2].firstedge == nullptr)            this->AdjList[p2].firstedge = enode2;        else        {            EdgeNode<Type> *move = this->AdjList[p2].firstedge;//这里要注意            while (move->next != nullptr)                move = move->next;            move->next = enode2;        }    }}//析构函数template<class Type>UndirectedGraph<Type>::~UndirectedGraph(){    cout << "析构函数" << endl;}//打印邻接表template<class Type>void UndirectedGraph<Type>::print(){    EdgeNode<Type> *p;    for (int i = 0; i < this->numVertexes; i++)    {        cout << "vertexs[" << i << "].data= " << this->AdjList[i].data << " : ";        p = this->AdjList[i].firstedge;        while (p != nullptr)        {            cout << p->adjvex << " ";            p = p->next;        }        cout << endl;    }}//测试程序int main(){    UndirectedGraph<char> graph;    graph.print();    return 0;}/*********************邻接表实现有向图******************///邻接表实现有向图缺点:只有出度,没有入度;解决方法:逆邻接表、十字链表//边节点类template<class Type>class EdgeNode{public:    int adjvex;//边节点域,存储该节点对应的下标    //int weight;//用于存储权值,对应非网图可以不需要    EdgeNode *next;//链域,指向下一邻节点    EdgeNode(int adj, EdgeNode *n = nullptr) :adjvex(adj),  next(n){}//构造函数};//顶点类template<class Type>class VertexNode{public:    Type data;//顶点域,存储顶点信息    EdgeNode<Type> *firstedge;//边表头指针    VertexNode(){}    VertexNode(Type d, EdgeNode<Type> *fir = nullptr) :data(d), firstedge(fir){}//构造函数};//有向图类template<class Type>class DirectedGraph{private:    VertexNode<Type> AdjList[MAX];//顶点表    int numVertexes;//当前图顶点    int numEdges;//当前图边数    int getPosition(Type el);//返回el在顶点表中的位置public:    DirectedGraph();//创建图    ~DirectedGraph();//构造函数    void print();//打印邻接表};//返回el在顶点表中的位置template<class Type>int DirectedGraph<Type>::getPosition(Type el){    for (int i = 0; i < this->numVertexes; i++)    {        if (this->AdjList[i].data == el)            return i;    }    return -1;}//创建表template<class Type>DirectedGraph<Type>::DirectedGraph(){    Type c1, c2;    int p1, p2;    EdgeNode<Type> *enode;    cout << "input vertex number: ";    cin >> this->numVertexes;    cout << "input edge number: ";    cin >> this->numEdges;    if (this->numVertexes < 1 || this->numEdges<1 || this->numEdges>this->numVertexes*(this->numVertexes - 1))    {        cout << "input error!" << endl;        return;    }    //初始化顶点    for (int i = 0; i < this->numVertexes; i++)    {        cout << "input a data: ";        cin >> this->AdjList[i].data;        this->AdjList[i].firstedge = nullptr;    }    //初始化弧    for (int i = 0; i < this->numEdges; i++)    {        cout << "input arcHead: ";//弧头        cin >> c1;        cout << "input arcTail:";//弧尾        cin >> c2;        p1 = getPosition(c1);        p2 = getPosition(c2);        if (p1 == -1 || p2 == -1)        {            cout << "input invalid!" << endl;            return;        }        enode = new EdgeNode<Type>(p2, nullptr);        if (this->AdjList[p1].firstedge == nullptr)            this->AdjList[p1].firstedge = enode;        else        {            EdgeNode<Type> *move = this->AdjList[p1].firstedge;            while (move->next != nullptr)                move = move->next;            move->next = enode;        }    }}//析构函数template<class Type>DirectedGraph<Type>::~DirectedGraph(){    cout << "析构函数" << endl;}//打印邻接表template<class Type>void DirectedGraph<Type>::print(){    for (int i = 0; i < this->numVertexes; i++)    {        cout << "AdjList[" << i + 1 << "].data = " << this->AdjList[i].data << ": ";        EdgeNode<Type> *move = this->AdjList[i].firstedge;        while (move != nullptr)        {            cout << move->adjvex << " ";            move = move->next;        }        cout << endl;    }}//测试程序int main(){    DirectedGraph<char> graph;    graph.print();    return 0;}
1 0