图的存储结构实践C++

这是无向图的邻接矩阵表示

#include 
#include 
#define MAXSIZE 100
typedef int vertexType;
typedef int edgeType;
typedef struct {
    vertexType vertexList[MAXSIZE];
    edgeType edgeMatrix[MAXSIZE][MAXSIZE];
    int vertexNumber, edgeNumber;
}AdjacencyGraph;

class UndirectedAGraph {
public:
    AdjacencyGraph *G;

    UndirectedAGraph() {
        G = (AdjacencyGraph* )malloc(sizeof(AdjacencyGraph));
        memset(G->vertexList, 0, sizeof(G->vertexList));
        memset(G->edgeMatrix, 0, sizeof(G->edgeMatrix));
    }

    int locateVertex(vertexType v);
    void createVertexList();
    void createEdgeMatrix();
    void createAdjacencyGraph();
};

int UndirectedAGraph::locateVertex(vertexType v) {
    for (int i = 0; i < G->vertexNumber; i++) {
        if (G->vertexList[i] == v) return i;
    }
    return -1;
}

void UndirectedAGraph::createVertexList() {
    std::cout << "Input Vertex Number of the undirected graph:" << std::endl;
    int vertexNumber;
    std::cin >> vertexNumber;
    G->vertexNumber = vertexNumber;

    std::cout << "Input All vertices:" << std::endl;
    int i = 0;
    while (vertexNumber--) {
        int vertex;
        std::cin>>vertex;
        G->vertexList[i++] = vertex;
    }
}

void UndirectedAGraph::createEdgeMatrix() {
    std::cout << "Input Edge number of the undirected graph:" << std::endl;
    int edgeNumber;
    std::cin >> edgeNumber;
    G->edgeNumber = edgeNumber;

    while (edgeNumber--) {
        std::cout << "Input edge:" << std::endl;
        int i, j, a, b;
        std::cin >> a >> b;
        i = locateVertex(a);
        j = locateVertex(b);
        if (i && j) {
            G->edgeMatrix[i][j] = 1;
            G->edgeMatrix[j][i] = 1;
        }
    }
}

void UndirectedAGraph::createAdjacencyGraph() {
    createVertexList();
    createEdgeMatrix();
}

int main() {
    UndirectedAGraph g;
    g.createAdjacencyGraph();
    std::cout << "Vertex List:" << std::endl;
    for (int i = 0; i < g.G->vertexNumber; i++) {
        std::cout << g.G->vertexList[i] << " ";
    }
    std::cout << std::endl;

    std::cout << "Edge Matrix:" << std::endl;
    for (int i = 0; i < g.G->vertexNumber; i++) {
        for (int j = 0; j < g.G->vertexNumber; j++) {
            std::cout << g.G->edgeMatrix[i][j] << " ";
        }
        std::cout << std::endl;
    }
}

有限带权图的邻接矩阵

#include 

template 
class DirectAGraph {
private:
    V *vertexList;
    E **edgeMatrix;
    int vertexNumber;
    int edgeNumber;
public:
    DirectAGraph (int vertexNumber, int edgeNumber) {
        this->vertexNumber = vertexNumber;
        this->edgeNumber = edgeNumber;
        this->vertexList = new V[vertexNumber]{0};
        this->edgeMatrix = new E*[vertexNumber];
        for (int i = 0; i < vertexNumber; i++) {
            this->edgeMatrix[i] = new E[vertexNumber]{0};
        }

        std::cout << "Input " << vertexNumber << " vertices." << std::endl;
        int index = 0;
        while (vertexNumber--) {
            std::cin >> this->vertexList[index++];
        }

        std::cout << "Input " << edgeNumber << " edges with edge value." << std::endl;
        while(edgeNumber--) {
            int i, j;
            E w;
            V a, b;
            std::cin >> a >> b;
            std::cin >> w;
            i = locateVertex(a);
            j = locateVertex(b);
            if (i!=-1&&j!=-1) {
                edgeMatrix[i][j] = w;
            } else {
                std::cout << "Error!";
                exit(0);
            }
        }
    }

    int locateVertex(V v) {
        for (int i = 0; i < this->vertexNumber; i++) {
            if (this->vertexList[i] == v) return i;
        }
        return -1;
    }

    void showGraph() {
        std::cout << "Here show the data structure of adjacency graph." << std::endl;
        std::cout << "Vertex List:" << std::endl;
        for (int i = 0; i < this->vertexNumber; i++) {
            std::cout << this->vertexList[i] << " ";
        }
        std::cout << std::endl << "Edge Matrix:" << std::endl;
        for (int i = 0; i < this->vertexNumber; i++) {
            for (int j = 0; j < this->vertexNumber; j++) {
                std::cout << this->edgeMatrix[i][j] << " ";
            }
            std::cout << std::endl;
        }
    }
};

int main() {
    DirectAGraph g(6, 9);
    g.showGraph();
}

邻接表创建

#include 
#include 
#define MAXSIZE 100

typedef struct edgeNode{
    int adjId;
    int weight;
    edgeNode *next;
};

typedef struct vertexNode{
    int data;
    edgeNode *first;
};

typedef struct undirectedAGraph{
    int vertexNumber;
    int edgeNumber;
    vertexNode vertexList[MAXSIZE];
};

int locateVertex(undirectedAGraph* G, int v) {
    for (int i = 0; i < G->vertexNumber; i++){
        if (v == G->vertexList[i].data) return i;
    }
    return -1;
}

void initAGraph(undirectedAGraph* &G) {
    G = (undirectedAGraph *)malloc(sizeof(undirectedAGraph));
    printf("Input vertex number:");
    scanf("%d", &G->vertexNumber);
    printf("Input edge number:");
    scanf("%d", &G->edgeNumber);
    for (int i = 0; i < G->vertexNumber; i++) {
        G->vertexList[i].first = NULL;
    }

}

void createAGraph(undirectedAGraph* &G) {
    printf("Input %d vertices.\n", G->vertexNumber);
    int vertexNumber = G->vertexNumber;
    int index = 0;
    while (vertexNumber--) {
        scanf("%d", &G->vertexList[index++].data);
    }

    printf("Input %d edges with edge weight.\n", G->edgeNumber);
    int edgeNumber = G->edgeNumber;
    while (edgeNumber--) {
        int i, j, a, b, w;
        scanf("%d %d %d", &a, &b, &w);
        i = locateVertex(G, a);
        j = locateVertex(G, b);
        if (i!=-1&&j!=-1) {
            edgeNode *e1 = (edgeNode*)malloc(sizeof(edgeNode));
            edgeNode *e2 = (edgeNode*)malloc(sizeof(edgeNode));
            e1->weight = w;
            e2->weight = w;
            e1->adjId = j;
            e2->adjId = i;
            e1->next = G->vertexList[i].first;
            G->vertexList[i].first = e1;
            e2->next = G->vertexList[j].first;
            G->vertexList[j].first = e2;
        }
    }
}

void showAGraph(undirectedAGraph *G) {
    printf("Vertex List:\n");
    for (int i = 0; i < G->vertexNumber; i++) {
        printf("%d ",G->vertexList[i].data);
    }
    printf("\nEdge Link Table:\n");
    for (int i = 0; i < G->vertexNumber; i++) {
        printf("No.%d V.%d", i, G->vertexList[i].data);
        edgeNode * temp = G->vertexList[i].first;
        while (temp) {
            printf("-%d->%d", temp->weight, temp->adjId);
            temp = temp->next;
        }
        printf("-->NULL\n");
    }
}

int main() {
    printf("Declaring...\n");
    undirectedAGraph *g;
    printf("Initialing...\n");
    initAGraph(g);
    printf("Creating...\n");
    createAGraph(g);
    printf("Showing...\n");
    showAGraph(g);
}

你可能感兴趣的:(图的存储结构实践C++)