数据结构——图的灵界矩阵存储

#include <iostream>
using namespace std;
const int DefaultVertices = 100;
const int maxWeight = 1000;
template<class T,class E>
class Graphmtx {
public:
    Graphmtx(int sz = DefaultVertices);
    ~Graphmtx() {
        delete[]VerticesList;
        delete[]Edge;
    }
    T getValue(int i) {
        if (i >= 0 && i < numVertices) {
            return VerticesList[i];
        }
        else {
            cout << "error" << endl;
            exit(1);
        }
    }
    E getWeight(int v1, int v2) {
        return (v1 > -1 && v2 > -1) ? Edge[v1][v2] : 0;
    }
    int getFirstNeighbor(int v);
    int getNectNeighbor(int v, int w);
    bool insertVertex(const T vertex);
    bool removeVertex(int v);
    bool insertEdge(int v1, int v2, E cost);
    bool removeEdge(int v1, int v2);
    friend istream& operator >> <T, E> (istream& in, Graphmtx<T, E>& G);
    friend ostream& operator << <T, E> (ostream& out, Graphmtx<T, E>& G);
    int NumberOfVertices() { return numVertices; }
    int NumberOfEdge() { return numEdge; }
private:
    T* VerticesList;
    E** Edge;
    int numVertices;
    int maxVertices;
    int numEdge;
    int gtVertexPos(T vertex) {
        for (int i = 0; i < numVertices; i++) 
            if (VerticesList[i] == vertex)
                return i;
        return -1;
    }
};
template<class T, class E>
Graphmtx<T, E>::Graphmtx(int sz) {
    maxVertices = sz;
    numVertices = 0;
    numEdge = 0;
    int i, j;
    VerticesList = new T[maxVertices];
    Edge = new E * [maxVertices];
    for (i = 0; i < maxVertices; i++)
        Edge[i] = new E[maxVertices];
    for (i = 0; i < maxVertices; i++)
        for (j = 0; j < maxVertices; j++)
            Edge[i][j] = ((i == j) ? 0 : maxWeight);
}

template<class T, class E>
int Graphmtx<T, E>::getFirstNeighbor(int v) {
    if (v > -1) 
        for (int col = 0; col < numVertices; col++) 
            if (Edge[v][col] && Edge[v][col] < maxWeight)
                return col;
    return -1;
}
template<class T, class E>
int Graphmtx<T, E>::getNectNeighbor(int v, int w) {
    if (v > -1 && w > -1) 
        for (int col = w + 1; col < numVertices; col++)
            if (Edge[v][col] && Edge[v][col] < maxWeight)
                return col;
    return -1;
}
template<class T, class E>
bool Graphmtx<T, E>::insertVertex(const T vertex) {
    if (numVertices == maxVertices)
        return false;
    VerticesList[numVertices++] = vertex;
    return true;
}
template<class T, class E>
bool Graphmtx<T, E>::removeVertex(int v) {
    if (v < 0 || v >= numVertices) {
        cerr << "error" << endl;
        return false;
    }
    if (numVertices == 1)
        return false;
    int i, j;
    VerticesList[v] = VerticesList[numVertices - 1];
    for (i = 0; i < numVertices; i++) 
        Edge[i][v] = Edge[i][numVertices - 1];   
    numVertices--;
    for (j = 0; i < numVertices; j++)
        Edge[v][i] = Edge[numVertices][j];
    return true;
}
template<class T, class E>
bool Graphmtx<T, E>::insertEdge(int v1, int v2, E cost) {
    if (v1 < 0 || v1 >= numVertices || v2 < 0 || v2 >= numVertices) {
        cerr << "error" << endl;
        return false;
    }
    Edge[v1][v2] = cost;
    Edge[v2][v1] = cost;
    return true;
}
template<class T, class E>
bool Graphmtx<T, E>::removeEdge(int v1, int v2) {
    if (v1 < 0 || v1 >= numVertices || v2 < 0 || v2 >= numVertices) {
        cerr << "error" << endl;
        return false;
    }
    Edge[v1][v2] = maxWeight;
    Edge[v2][v1] = maxWeight;
    numEdge--;
    return true;
}

template <class T, class E>
ostream& operator << (ostream& out, Graphmtx<T, E>& G) {
    int i, j, n, m; T e1, e2; E w;
    n = G.NumberOfVertices(); m = G.NumberOfEdges();
    out << n << "," << m << endl;
    for (i = 0; i < n; i++)
        for (j = i + 1; j < n; j++) {
            w = G.getWeight(i, j);
            if (w > 0 && w < maxWeight) {
                e1 = G.getValue(i); e2 = G.getValue(j);
                out << "(" << e1 << "," << e2 << "1" << w << ")" << endl;
            }
        }
    return out;
};
template <class T, class E>
istream& operator >> (istream& in, Graphmtx<T, E>& G) {
    int i, j, k, n, m; T e1, e2; E weight;
    in >> n >> m;
    for (i = 0; i < n; i++)
    in >> e1; G.insertVertex(e1);
    i = 0;
    while (i < m) {
        in >> e1 >> e2 >> weight;
        j = G.getVertexPos(e1); k = G.getVertexPos(e2);
        if (j == -1 || k == -1)
            cerr << "边两端点信息有误,重新输入!" << endl;
        else 
            G.insertEdge(j, k, weight); i++;
    }
    return in;
}


  int main() {
    return 0;
}```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值