diff --git a/homework5/CMakeLists.txt b/homework5/CMakeLists.txt index 81426e2..fc40a1b 100644 --- a/homework5/CMakeLists.txt +++ b/homework5/CMakeLists.txt @@ -4,6 +4,5 @@ project(homework5) set(CMAKE_CXX_STANDARD 20) add_executable(test1 test1.cpp - Graph.h - GraphExceptions/SameNodeConnectException.h - GraphExceptions/NodeIdOutOfRangeException.h) + MatrixGraph.h + GraphExceptions/InsertExistedConnectException.h) diff --git a/homework5/Graph.h b/homework5/Graph.h deleted file mode 100644 index 318a829..0000000 --- a/homework5/Graph.h +++ /dev/null @@ -1,81 +0,0 @@ -// -// Created by 31416 on 24-12-4. -// - -#ifndef GRAPH_H -#define GRAPH_H - -#include - -#include "GraphExceptions/NodeIdOutOfRangeException.h" -#include "GraphExceptions/SameNodeConnectException.h" -using namespace std; - -/* - * 简述: - * 节点=数据域+指针域 - * 第一个节点存储id - * 第二个节点存储值 - * 后续节点存储连接关系 - * 数据域: - * data:可存id,可存其他节点的id,可存数据 - * weight:边上的权 - */ - -template -class Graph { -private: - struct node{ - T data; - T weight; - struct node* next; - }; - vector nodes; - int max_size; - -public: - Graph() { - 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; - insert->next->next=NULL; - 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; - } - } -}; - - -#endif //GRAPH_H diff --git a/homework5/GraphExceptions/InsertExistedConnectException.h b/homework5/GraphExceptions/InsertExistedConnectException.h new file mode 100644 index 0000000..9f77ff8 --- /dev/null +++ b/homework5/GraphExceptions/InsertExistedConnectException.h @@ -0,0 +1,26 @@ +// 这是一个异常,当用户尝试插入已存在的连接时抛出 + +#ifndef INSERTEXISTEDCONNECTEXCEPTION_H +#define INSERTEXISTEDCONNECTEXCEPTION_H +#include +#include + + +class InsertExistedConnectException : public std::exception{ +private: + std::string message; +public: + InsertExistedConnectException():message("You input id is out of range"){} + InsertExistedConnectException(const std::string& msg) : message(msg) {} + InsertExistedConnectException(const std::string& msg,int errCode):message(msg+" Err code: "+std::to_string(errCode)){} + + virtual ~InsertExistedConnectException() noexcept {} + virtual const char* what() const noexcept override { + return message.c_str(); + } + +}; + + + +#endif //INSERTEXISTEDCONNECTEXCEPTION_H diff --git a/homework5/ListGraph.h b/homework5/ListGraph.h new file mode 100644 index 0000000..067aefa --- /dev/null +++ b/homework5/ListGraph.h @@ -0,0 +1,136 @@ +// +// Created by 31416 on 24-12-4. +// + +#ifndef LIST_GRAPH_H +#define LIST_GRAPH_H + +#include +#include + +#include "GraphExceptions/InsertExistedConnectException.h" +#include "GraphExceptions/NodeIdOutOfRangeException.h" +#include "GraphExceptions/SameNodeConnectException.h" +using namespace std; + +/* + * 简述: + * 节点=数据域+指针域 + * 第一个节点存储id + * 第二个节点存储值 + * 后续节点存储连接关系 + * 数据域: + * data:可存id,可存其他节点的id,可存数据 + * weight:边上的权 + */ + +template +class ListGraph { +private: + struct node{ + T data; + T weight; + struct node* next; + }; + vector nodes; + bool flag; //定义是否有向 + bool isWeighted; //定义是否有权 + +public: + ListGraph() { + nodes.clear(); + flag = false; + isWeighted = false; + } + ListGraph(bool flag) { + nodes.clear(); + this->flag = flag; + isWeighted = false; + } + ListGraph(bool flag,bool isWeighted) { + nodes.clear(); + this->flag = flag; + this->isWeighted = isWeighted; + } + void insert(T data) { + node* insert = new node(); + insert->data=nodes.size(); + insert->weight = 0; + insert->next=new node(); + insert->next->data=data; + insert->next->next=NULL; + nodes.push_back(insert); + } + void connect(int id1,int id2,T weight=0) { //flag区分是否为有向图 + if ((id1>nodes.size()-1||id2>nodes.size()-1)||(id1<0)||(id2<0)) { + throw NodeIdOutOfRangeException("您提供的id超出范围",0X002); + } + if(id1==id2) { + throw SameNodeConnectException("禁止连接图中的同一个点",0x001); + } + node *current = nodes.at(id1); + int current_id = 0; + while(current->next!=NULL) { + if((current->next->data==id2)&&(current_id>1)) { + if (current->next->weight==weight) { + throw InsertExistedConnectException("禁止插入已存在的连接",0x003); + } + current->next->weight=weight; + } + current = current->next; + current_id++; + } + current->next=new node(); + current->next->data=id2; + current->next->weight = weight; + current->next->next=NULL; + + if (!flag) { + node *current2 = nodes.at(id2); + int current2_id = 0; + while(current2->next!=NULL) { + if((current2->next->data==id2)&&(current2_id>1)) { + if (current2->next->weight==weight) { + throw InsertExistedConnectException("禁止插入已存在的连接",0x003); + } + current2->next->weight=weight; + } + current2 = current2->next; + current2_id++; + } + current2->next=new node(); + current2->next->data=id1; + current2->next->weight = weight; + current2->next->next=NULL; + } + } + string to_string() { + stringstream ss; + for (int i=0;i<=nodes.size()-1;i++) { + node* current = nodes.at(i); + while(current!=NULL) { + if(current->next==NULL) { + if (isWeighted) { + ss<data<<"|"<weight; + } + else { + ss<data; + } + }else { + if (isWeighted){ + ss<data<<"|"<weight<<" "; + } + else { + ss<data<<" "; + } + } + current = current->next; + } + ss< +using namespace std; + +template +class MatrixGraph { +private: + vector data; + vector > matrix; + int nodeCount; +public: + MatrixGraph() { + matrix.clear(); + data.clear(); + nodeCount = 0; + } + void insert(T value) { + data.push_back(value); + nodeCount++; + matrix.push_back(vector(nodeCount, false)); + for(int i = 0; i < nodeCount-1; ++i) { + matrix[i].push_back(false); + } + } +}; + + + +#endif //MATRIXGRAPH_H diff --git a/homework5/test1.cpp b/homework5/test1.cpp index 6b94e4a..c62b025 100644 --- a/homework5/test1.cpp +++ b/homework5/test1.cpp @@ -20,10 +20,16 @@ // 输入第1个4表示有4个顶点,第2个4表示有4条边。之后的4行输入代表4条边的顶点。 // 输出的前4行为邻接矩阵,之后空一行,然后输出的“0 1 3 2”是深度优先遍历序列。 -# include "Graph.h" +#include"ListGraph.h" #include using namespace std; int main() { + ListGraph list_graph; + list_graph.insert(1); + list_graph.insert(2); + list_graph.connect(0,1); + list_graph.connect(1,0); + cout << list_graph.to_string() << endl; return 0; }