diff --git a/include/bus.h b/include/bus.h index 6992575..ac158f0 100644 --- a/include/bus.h +++ b/include/bus.h @@ -21,6 +21,11 @@ struct bus { */ typedef struct bus bus_t; +/** + * 全局的公交车变量 + */ +extern bus_t the_bus; + /** * 每个时刻使公交车前进 * @param rails 轨道链表 @@ -28,6 +33,6 @@ typedef struct bus bus_t; * @param bus 公交车 * @return 公交车是否到达站点 */ -int RunBus(rail_node_t* rails, bus_t bus, int direction); +int RunBus(int direction); #endif //AUTO_PILOT_BUS_BUS_H diff --git a/include/controller.h b/include/controller.h index 569005d..2e9a0d4 100644 --- a/include/controller.h +++ b/include/controller.h @@ -10,66 +10,20 @@ #include "query.h" #include "bus.h" -/** - * 全局的时间计数器 - */ -extern int BUS_TIME; - /** * 当前正在处理的请求 */ extern bus_query_t *target_query; -/** - * 时间增加 - */ -void AddTime(); - /** * 在先来先服务策略下应该前进的方向 - * @param bus 公交车 - * @param queries 请求队列链表 * @return 前进的方向 */ -int FCFSDirection(bus_t bus, bus_query_t queries); +int FCFSDirection(); /** * 在先来先服务策略下给出处理的请求 - * @param bus 公交车 - * @param queries 请求队列链表 * @return 需要处理的请求 */ -bus_query_t FCFSQuery(bus_t bus, bus_query_t queries); - -/** - * 在最短寻找时间策略下应该前进的方向 - * @param bus 公交车 - * @param queries 请求队列链表 - * @return 前进的方向 - */ -int SSTFDirection(bus_t bus, bus_query_t queries); - -/** - * 在最短寻找时间策略给出处理的请求 - * @param bus 公交车 - * @param queries 请求队列链表 - * @return 需要处理的请求 - */ -bus_query_t SSTFQuery(bus_t bus, bus_query_t queries); - -/** - * 顺便服务的前进方向 - * @param bus 公交车 - * @param queries 请求队列链表 - * @return 前进的方向 - */ -int SCANDirection(bus_t bus, bus_query_t queries); - -/** - * 顺便服务的策略处理 - * @param bus 公交车 - * @param queries 请求队列链表 - * @return 需要处理的请求 - */ -bus_query_t SCANQuery(bus_t bus, bus_query_t queries); +bus_query_t FCFSQuery(); #endif //AUTO_PILOT_BUS_CONTROLLER_H diff --git a/include/query.h b/include/query.h index 951d0bd..af2ee14 100644 --- a/include/query.h +++ b/include/query.h @@ -19,30 +19,37 @@ struct bus_query { /** * 请求产生/指向的站点 */ - rail_node_t node; + rail_node_t *node; + /** + * 指向下一个请求的指针 + */ + struct bus_query *next_node; }; typedef struct bus_query bus_query_t; /** - * 创建请求 - * @param query 请求链表队列 + * 全局的请求链表头节点 + */ +extern bus_query_t *queries; + +/** + * 创建请求链表节点 * @param type 请求的类型 * @param node 请求产生/指向的站点 */ -void CreateQuery(bus_query_t query, int type, rail_node_t node); +bus_query_t *CreateQuery(int type, rail_node_t *node); /** * 删除请求 - * @param query 请求链表队列 * @param target_query 需要删除的请求 */ -void DeleteQuery(bus_query_t query, bus_query_t target_query); +void DeleteQuery(bus_query_t *target_query); /** * 释放请求链表占据的空间 - * @param queries 请求链表的头节点 + * @param head 请求链表的头节点 */ -void FreeQueries(bus_query_t queries); +void FreeQueries(bus_query_t *head); #endif //AUTO_PILOT_BUS_QUERY_H diff --git a/include/rail.h b/include/rail.h index 9aa5df0..0ab4964 100644 --- a/include/rail.h +++ b/include/rail.h @@ -35,6 +35,11 @@ typedef struct rail_node rail_node_t; */ extern rail_node_t *rails; +/** + * 全局的计时器 + */ +extern int bus_time; + /** * 查找指定编号的站点指针 * @param head 轨道的头节点地址 @@ -55,8 +60,10 @@ rail_node_t *CreateRails(int length, int node_num); * 释放分配的内存空间 * @param railNode 轨道链表的头节点 */ -void FreeRails(rail_node_t railNode); - -int add(int a, int b); +void FreeRails(rail_node_t *head); +/** + * 时间走过一秒 + */ +void AddTime(); #endif //AUTO_PILOT_BUS_RAIL_H diff --git a/src/bus.c b/src/bus.c index f47b66b..0d08cab 100644 --- a/src/bus.c +++ b/src/bus.c @@ -1 +1,3 @@ -#include "bus.h" \ No newline at end of file +#include "bus.h" + +bus_t *the_bus; \ No newline at end of file diff --git a/src/query.c b/src/query.c index 642474b..893d9c7 100644 --- a/src/query.c +++ b/src/query.c @@ -3,5 +3,70 @@ // #include "query.h" +bus_query_t *queries = NULL; +bus_query_t *CreateQuery(int type, rail_node_t *node) +{ + bus_query_t *p = (bus_query_t *)malloc(sizeof (bus_query_t)); + p->node = node; + p->type = type; + p->time = bus_time; + p->next_node = NULL; + + if(queries == NULL) + { + queries = p; + } + else + { + // 寻找链表的末节点 + bus_query_t *last_node = queries; + + while(last_node->next_node != NULL) + { + last_node = last_node->next_node; + } + + last_node->next_node = p; + } + + return p; +} + +void DeleteQuery(bus_query_t *target) +{ + if(target == queries) + { + queries = target->next_node; + } + else + { + bus_query_t *node = queries; + + // 找到被删除节点的上一个节点 + while(node->next_node != target) + { + node = node->next_node; + } + + node->next_node = target->next_node; + } + + free(target); +} + +void FreeQueries(bus_query_t *head) +{ + bus_query_t *p = head; + + while (p != NULL) + { + bus_query_t *temp = p; + p = p->next_node; + free(temp); + } + + // 将全局的请求列表头指针置为空 + queries = NULL; +} diff --git a/src/rail.c b/src/rail.c index 3c12e72..356d398 100644 --- a/src/rail.c +++ b/src/rail.c @@ -1,11 +1,7 @@ #include "rail.h" rail_node_t *rails = NULL; - -int add(int a, int b) -{ - return a+ b; -} +int bus_time = 0; rail_node_t *CreateRails(int length, int node_num) { @@ -63,3 +59,23 @@ rail_node_t *FindNode(rail_node_t *head, int id) return NULL; } +void FreeRails(rail_node_t *head) +{ + rail_node_t *p = head; + + while (p != NULL) + { + rail_node_t *temp = p; + p = p->next_node; + free(temp); + } + + // 将全局的轨道指针置为空 + rails = NULL; +} + +void AddTime() +{ + bus_time++; +} + diff --git a/test/rail_test.cpp b/test/rail_test.cpp index b3170f8..d05bf4b 100644 --- a/test/rail_test.cpp +++ b/test/rail_test.cpp @@ -41,7 +41,7 @@ TEST(rail, CreateRails) TEST(rail, FindNode) { rail_node_t *head = CreateRails(10, 10); - rail_node_t *p = nullptr; + rail_node_t *p; p = FindNode(head, 3); EXPECT_EQ(p->id, 3); @@ -50,6 +50,12 @@ TEST(rail, FindNode) EXPECT_EQ(p->id, 9); } +TEST(rail, FreeRails) +{ + rail_node_t *head = CreateRails(10, 10); + FreeRails(head); +} +