C++实现十字链表来表示有向图(附源代码)

文件结构 : 

 

文件名字用途
CmakeList.txtcmake文件
how.md简述思路以及其他说明
main.cpp主测试程序
CrossList.cpp核心实现文件
CrossList..h

核心头文件

要实现图结构的例图:

 

CrossList.cpp

//
// Created by A Luck Boy on 2023/2/10.
//
#include "CrossList.h"

CrossList createInit(const char *names){
    CrossList list;
    list.len = 0;
    while (names[list.len] != '\0') list.len ++ ;
    list.names = new char [list.len];
    for (int i=0;i<list.len;i++) {
        list.names[i] = names[i];
        list.intake[i] = nullptr;
        list.output[i] = nullptr;
    }

    return list;
};

static int getIndex(CrossList _t, char name){
    for (int j=0;j<_t.len;j++) {
        if (_t.names[j] == name) return j;
    }return -1;
};

int getDrgee(CrossList _t, char name, char direction){
    int _count = 0;
    int index = getIndex(_t, name);
    if (direction == 'o'){
        auto cur_node = _t.output[index];
        while (cur_node != nullptr) {
            _count +=  1;
            cur_node = cur_node->next;
        }return _count;
    }elif (direction == 'i'){
        auto cur_node = _t.intake[index];
        while (cur_node != nullptr) {
            _count += 1;
            cur_node = cur_node->next;
        }return _count;
    } else{
        return -1;
    }
};

CrossList..h

//
// Created by A Luck Boy on 2023/2/10.
//
#include <iostream>
using namespace std;

#define Waring cout<<"Wrong Code!"<<endl;
#define SUC cout<< "Correct Procedure!" << endl;
#define elif else if

// 最大允许点数
#if defined(MAXSIZE)
#else
    #define MAXSIZE 10
#endif

// 边点
typedef struct _Node{
    // 点名字
    char name;
    // 边长或者说边的权重
    int weight;
    // 出度指针
    _Node * next;
    // 入度指针
    _Node * next2;
} Node ;

// 点集合组成的图,这里是十字链表表示
typedef struct _CrossList {
    // 点名字组成的字符串
    char * names;
    // 点数
    int len;
    // 指向出度方向的指针
    _Node * output[MAXSIZE];
    // 指向入度方向的指针
    _Node * intake[MAXSIZE];
} CrossList;

// methods——不考虑bug以及错误处理
// names参数是点名字组成的字符串
CrossList createInit(const char *names);

// 计算某点的出/入度--传入o计算出度,i传入入度
int getDrgee(CrossList _t, char name, char direction);

// 获取某个点名字在名字字符串中的索引
static int getIndex(CrossList _t, char name);

main.cpp

//
// Created by A Luck Boy on 2023/2/10.
//
#include "CrossList.h"

int main()
{
    auto list = createInit("ABCD");
    for (int i=0;i<list.len;i++) cout << "Point " << list.names[i] << endl;

    // 出度
    Node a_b = {'B', 1, nullptr, nullptr};
    Node a_d = {'D', 5, nullptr, nullptr};

    Node c_a = {'A', 3, nullptr, nullptr};
    Node c_b = {'B', 2, nullptr, nullptr};

    Node d_a = {'A', 5, nullptr, nullptr};
    Node d_c = {'C', 4, nullptr, nullptr};

    // A的出度
    list.output[0] = &a_b;
    a_b.next = &a_d;

    // B
    list.output[1] = nullptr;

    // C
    list.output[2] = &c_a;
    c_a.next = &c_b;

    // D
    list.output[3] = &d_a;
    d_a.next = &d_c;

    // 入度
    // A
    list.intake[0] = &c_a;
    c_a.next2 = &d_a;

    // B
    list.intake[1] = &a_b;
    a_b.next2 = &c_b;

    // C
    list.intake[2] = &d_c;

    // D
    list.intake[3] = &a_d;

    // 获取每个点的出度和入度
    for (int i=0;i<list.len;i++) {
        cout << "Point " << list.names[i] << " Output is " << getDrgee(list, list.names[i], 'o')<<endl;
        cout << "Point " << list.names[i] << " Intake is " << getDrgee(list, list.names[i], 'i')<<endl;
    }

    return 0;
}

终端运行结果

Point A
Point B
Point C
Point D
Point A Output is 2
Point A Intake is 2
Point B Output is 0
Point B Intake is 2
Point C Output is 2
Point C Intake is 1
Point D Output is 2
Point D Intake is 1

源代码:

自己实现的数据结构与算法合集: 自己实现的数据结构与算法合集,使用C或者C++ - Gitee.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

「已注销」

若您有别的建议,请在评论区留言

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值