From f2590db299bbc3d615fb6ba16ecc7decf21c38bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=A1=E5=9D=82=E6=98=B4?= Date: Thu, 5 Dec 2024 18:28:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A5=BDbug=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- homework5/CMakeLists.txt | 4 +- homework5/ListGraph.h | 22 +--------- homework5/{test1.cpp => test2.cpp} | 64 ++++++++++++++---------------- 3 files changed, 32 insertions(+), 58 deletions(-) rename homework5/{test1.cpp => test2.cpp} (83%) diff --git a/homework5/CMakeLists.txt b/homework5/CMakeLists.txt index f30ea50..97678b4 100644 --- a/homework5/CMakeLists.txt +++ b/homework5/CMakeLists.txt @@ -3,6 +3,4 @@ project(homework5) set(CMAKE_CXX_STANDARD 20) -add_executable(test1 test1.cpp - MatrixGraph.h - GraphExceptions/InsertExistedConnectException.h) +add_executable(test2 test2.cpp) diff --git a/homework5/ListGraph.h b/homework5/ListGraph.h index 8a72e78..5b81805 100644 --- a/homework5/ListGraph.h +++ b/homework5/ListGraph.h @@ -37,18 +37,10 @@ private: bool isWeighted; //定义是否有权 public: - ListGraph() { + ListGraph(bool flag=false,bool isWeighted=false) { 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; } @@ -70,7 +62,7 @@ public: } node *current = nodes.at(id1); //跳过id节点和值节点 - for(int i=0;i<2;i++) { + for(int i=0;i<1;i++) { current = current->next; } while(current->next!=NULL) { @@ -89,17 +81,7 @@ public: if (!flag) { node *current2 = nodes.at(id2); - //跳过id节点和值节点 - for(int i=0;i<2;i++) { - current = current->next; - } while(current2->next!=NULL) { - if((current2->next->data==id2)) { - if (current2->next->weight==weight) { - throw InsertExistedConnectException("禁止插入已存在的连接",0x003); - } - current2->next->weight=weight; - } current2 = current2->next; } current2->next=new node(); diff --git a/homework5/test1.cpp b/homework5/test2.cpp similarity index 83% rename from homework5/test1.cpp rename to homework5/test2.cpp index b91e18e..23a4cfe 100644 --- a/homework5/test1.cpp +++ b/homework5/test2.cpp @@ -1,35 +1,29 @@ -// 利用邻接矩阵存储无向图,并从0号顶点开始进行深度优先遍历。 -// 输入: -// 输入第一行是两个整数n e,其中n表示顶点数(则顶点编号为0至n-1),e表示图中的边数。之后有e行信息输入,每行输入表示一条边,格式是“顶点1 顶点2”,把边插入图中。如: -// 4 4 -// 0 1 -// 1 3 -// 0 3 -// 0 2 -// 输出: -// 先输出存储图的邻接矩阵,同一行元素之间空1格,最后一个元素之后不要有空格。 -// 之后空一行后输出从0号顶点开始的深度优先遍历序列,顶点编号之间空1格。 -// 例如,对于上面的示例输入,输出为: -// 0 1 1 1 -// 1 0 0 1 -// 1 0 0 0 -// 1 1 0 0 -// 从顶点0开始的深度优先遍历序列: -// 0 1 3 2 -// 说明 -// 输入第1个4表示有4个顶点,第2个4表示有4条边。之后的4行输入代表4条边的顶点。 -// 输出的前4行为邻接矩阵,之后空一行,然后输出的“0 1 3 2”是深度优先遍历序列。 - -#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; -} +// 利用邻接矩阵存储无向图,并从0号顶点开始进行深度优先遍历。 +// 输入: +// 输入第一行是两个整数n e,其中n表示顶点数(则顶点编号为0至n-1),e表示图中的边数。之后有e行信息输入,每行输入表示一条边,格式是“顶点1 顶点2”,把边插入图中。如: +// 4 4 +// 0 1 +// 1 3 +// 0 3 +// 0 2 +// 输出: +// 先输出存储图的邻接矩阵,同一行元素之间空1格,最后一个元素之后不要有空格。 +// 之后空一行后输出从0号顶点开始的深度优先遍历序列,顶点编号之间空1格。 +// 例如,对于上面的示例输入,输出为: +// 0 1 1 1 +// 1 0 0 1 +// 1 0 0 0 +// 1 1 0 0 +// 从顶点0开始的深度优先遍历序列: +// 0 1 3 2 +// 说明 +// 输入第1个4表示有4个顶点,第2个4表示有4条边。之后的4行输入代表4条边的顶点。 +// 输出的前4行为邻接矩阵,之后空一行,然后输出的“0 1 3 2”是深度优先遍历序列。 + +#include"ListGraph.h" +#include +using namespace std; + +int main() { + return 0; +}