From 6d97591b1d4568b0d139a8518794c13bd9c9aebc Mon Sep 17 00:00:00 2001 From: jackfiled Date: Wed, 18 May 2022 17:31:34 +0800 Subject: [PATCH] =?UTF-8?q?FindNode=E5=87=BD=E6=95=B0=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E9=80=9A=E8=BF=87=20=E9=87=8D=E5=91=BD=E5=90=8D=E5=BD=A2?= =?UTF-8?q?=E5=8F=82=EF=BC=8C=E9=81=BF=E5=85=8D=E5=92=8C=E5=85=A8=E5=B1=80?= =?UTF-8?q?=E5=8F=98=E9=87=8F=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/rail.h | 4 ++-- src/rail.c | 12 ++++++------ test/rail_test.cpp | 12 ++++++++++++ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/include/rail.h b/include/rail.h index 8016985..9aa5df0 100644 --- a/include/rail.h +++ b/include/rail.h @@ -37,11 +37,11 @@ extern rail_node_t *rails; /** * 查找指定编号的站点指针 - * @param rails 轨道的头节点地址 + * @param head 轨道的头节点地址 * @param id 需要查找的站点编号 * @return 需要查找站点指针 */ -rail_node_t *FindNode(rail_node_t *rails, int id); +rail_node_t *FindNode(rail_node_t *head, int id); /** * 创建轨道列表 diff --git a/src/rail.c b/src/rail.c index bd8d8a1..3c12e72 100644 --- a/src/rail.c +++ b/src/rail.c @@ -41,18 +41,18 @@ rail_node_t *CreateRails(int length, int node_num) return head; } -rail_node_t *FindNode(rail_node_t *rails, int id) +rail_node_t *FindNode(rail_node_t *head, int id) { rail_node_t *p = NULL; - if(rails) + if(head) { - if(rails->id == id) + if(head->id == id) { - return rails; + return head; } - if(rails->next_node != NULL) + if(head->next_node != NULL) { - p = FindNode(rails->next_node, id); + p = FindNode(head->next_node, id); if(p) { return p; diff --git a/test/rail_test.cpp b/test/rail_test.cpp index d30b3f1..b3170f8 100644 --- a/test/rail_test.cpp +++ b/test/rail_test.cpp @@ -38,6 +38,18 @@ TEST(rail, CreateRails) } } +TEST(rail, FindNode) +{ + rail_node_t *head = CreateRails(10, 10); + rail_node_t *p = nullptr; + + p = FindNode(head, 3); + EXPECT_EQ(p->id, 3); + + p = FindNode(head, 9); + EXPECT_EQ(p->id, 9); +} +