文件结构 :
文件名字 | 用途 |
CmakeList.txt | cmake文件 |
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
源代码: