图:邻接矩阵、邻接表(c++)
本文最后更新于93 天前,其中的信息可能已经过时,如有错误请发送邮件到big_fw@foxmail.com

matrix.hpp

#include<iostream>

using namespace std;

#define inf 0

class Graph
{
private:
    int pointsAccount;
    int **matrix;
public:
    Graph(int num);
    ~Graph();
    void addEdge(int p1,int p2,int value);//在p1与p2之间添加一条权值为value的边
    void removeEdge(int p1,int p2);
    void printMatrix();
};

Graph::Graph(int num)
{
    pointsAccount = num;
    matrix = new int*[num];
    for(int i = 0;i<num;++i){
        matrix[i] = new int[num];
    }
    for(int i=0; i<num; ++i){
        for(int j=0; j<num;++j){
            matrix[i][j] = inf;
        }
    }
}

Graph::~Graph()
{
    for(int i = 0;i<pointsAccount;++i){
        delete[] matrix[i];
    }
    delete[] matrix;
}

void Graph::addEdge(int p1,int p2,int value){
    matrix[p1][p2] = value;
}

void Graph::removeEdge(int p1,int p2){
    matrix[p1][p2] = inf;
}

void Graph::printMatrix(){
    cout << " "<<"|";
    for(int i = 0; i<pointsAccount; ++i){
        cout << i << "|";
    }
    cout << endl;
    for(int i=0; i<pointsAccount; ++i){
        cout << i << "|";
        for(int j=0; j<pointsAccount;++j){
            cout << matrix[i][j] << "|";
        }
        cout << endl;
    }
}

void test(){
    Graph g(4);
    g.addEdge(0,1,1);
    g.addEdge(1,2,1);
    g.addEdge(2,3,1);
    g.addEdge(3,1,1);
    g.printMatrix();

}

table.hpp

#pragma once

#include<iostream>

using namespace std;

//鄰接矩陣 定義頂點、邊節點、頂點由順序表存儲、邊節點有鏈表存儲,添加邊,打印
class table
{
private:
    /* data */
    struct EdgeNode
    {
        int vertex;
        int weight;
        EdgeNode* next;
        EdgeNode(int v,int w) : vertex(v),weight(w){}
    };

    struct vertex
    {
        int value;
        EdgeNode* firstNode;
    };
    
    int vertex_amount;
    vertex *nodes;
    
public:
    table(int n);
    ~table();
    void addEdge(int p1,int p2,int w);
    void print();
};

table::table(int n)
{
    vertex_amount = n;
    nodes = new vertex[n];
    for(int i=0;i<n;++i){
        nodes[i].value = i;
        nodes[i].firstNode = nullptr;
    }
}

table::~table()
{
    for(int i = 0; i < vertex_amount; i++){
        EdgeNode* cur = nodes[i].firstNode;
        while(cur != nullptr){
            EdgeNode* temp = cur;
            cur = cur->next;
            delete temp;
        }
    }
    delete[] nodes;
}

void table::addEdge(int p1,int p2,int w){
    EdgeNode* new_edge = new EdgeNode(p2,w);
    new_edge->next = nodes[p1].firstNode;
    nodes[p1].firstNode = new_edge;
}

void table::print(){
    for(int i = 0;i<vertex_amount;++i){
        cout << "頂點" << i <<": "<<"[";
        EdgeNode* cur = nodes[i].firstNode;
        while(cur){
            if(cur->next != 0){
            cout << cur->vertex << ",";}
            else cout << cur->vertex;
            cur = cur->next;
        }
        cout << "]";
    }
}

void test(){
    table g(4);
    g.addEdge(0,1,1);
    g.addEdge(1,2,1);
    g.addEdge(2,3,1);
    g.addEdge(3,1,1);
    g.print();

}

main.cpp

#include”table.hpp”

int main(){

    test();

    return 0;

}

文末附加内容
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇