写好节点插入和连接,支持有向图无向图,支持权

This commit is contained in:
2024-12-05 01:21:44 +08:00
parent a56aa1d048
commit 238975f638
4 changed files with 109 additions and 7 deletions
+3 -1
View File
@@ -4,4 +4,6 @@ project(homework5)
set(CMAKE_CXX_STANDARD 20)
add_executable(test1 test1.cpp
Graph.h)
Graph.h
GraphExceptions/SameNodeConnectException.h
GraphExceptions/NodeIdOutOfRangeException.h)
+59 -6
View File
@@ -6,20 +6,73 @@
#define GRAPH_H
#include<vector>
#include "GraphExceptions/NodeIdOutOfRangeException.h"
#include "GraphExceptions/SameNodeConnectException.h"
using namespace std;
/*
* 简述:
* 节点=数据域+指针域
* 第一个节点存储id
* 第二个节点存储值
* 后续节点存储连接关系
* 数据域:
* data:可存id,可存其他节点的id,可存数据
* weight:边上的权
*/
template<typename T>
class Graph {
private:
vector<int> ids;
Graph *node;
int max_id;
struct node{
T data;
T weight;
struct node* next;
};
vector<node> nodes;
int max_size;
public:
Graph() {
ids.clear();
node = NULL;
max_id = 0;
nodes.clear();
max_size = 0;
}
void insert(T data) {
max_size++;
node insert = new node();
insert->data=max_size;
insert->weight = 0;
insert->next=new node();
insert->next->data=data;
nodes.push_back(insert);
}
void connect(int id1,int id2,bool flag,T weight=0) { //flag区分是否为有向图
if(id1==id2) {
throw SameNodeConnectException("禁止连接图中的同一个点",0x001);
}
if ((id1>max_size||id2>max_size)||(id1<0)||(id2<0)) {
throw NodeIdOutOfRangeException("您提供的id超出范围",0X002);
}
node *current = nodes.at(id1);
while(current->next!=NULL) {
current = current->next;
}
current->next=new node();
current->next->data=id2;
current->next->weight = weight;
current->next->next=NULL;
if (flag==false) {
node *current = nodes.at(id2);
while(current->next!=NULL) {
current = current->next;
}
current->next=new node();
current->next->data=id1;
current->next->weight = weight;
current->next->next=NULL;
}
}
};
@@ -0,0 +1,25 @@
// 这是一个异常,当用户给的节点id超出最大值时抛出
#ifndef NODEIDOUTOFRANGEEXCEPTION_H
#define NODEIDOUTOFRANGEEXCEPTION_H
#include <exception>
#include<string>
class NodeIdOutOfRangeException : public std::exception{
private:
std::string message;
public:
NodeIdOutOfRangeException():message("You input id is out of range"){}
NodeIdOutOfRangeException(const std::string& msg) : message(msg) {}
NodeIdOutOfRangeException(const std::string& msg,int errCode):message(msg+"Err code: "+std::to_string(errCode)){}
virtual ~NodeIdOutOfRangeException() noexcept {}
virtual const char* what() const noexcept override {
return message.c_str();
}
};
#endif //NODEIDOUTOFRANGEEXCEPTION_H
@@ -0,0 +1,22 @@
// 这是一个异常,当用户试图连接图中的两个相同节点时抛出
#ifndef SAMENODECONNECTEXCEPTION_H
#define SAMENODECONNECTEXCEPTION_H
#include <exception>
#include <string>
class SameNodeConnectException : public std::exception{
private:
std::string message;
public:
SameNodeConnectException():message("Don't connect same node"){}
SameNodeConnectException(const std::string& msg) : message(msg) {}
SameNodeConnectException(const std::string& msg,int errCode):message(msg+"Err code: "+std::to_string(errCode)){}
virtual ~SameNodeConnectException() noexcept {}
virtual const char* what() const noexcept override {
return message.c_str();
}
};
#endif //SAMENODECONNECTEXCEPTION_H