From 35fa35de3f60dc17c67c869571aee529115d3485 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Fri, 10 Jun 2022 23:33:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E6=8E=A7=E5=88=B6=E7=B1=BB=20=E6=B7=BB=E5=8A=A0=E4=BA=86?= =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/queryModel.h | 56 +++++++++++++++++++++++++++ include/railsModel.h | 11 ++++++ src/queryModel.cpp | 92 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 159 insertions(+) create mode 100644 include/queryModel.h create mode 100644 src/queryModel.cpp diff --git a/include/queryModel.h b/include/queryModel.h new file mode 100644 index 0000000..0f3312c --- /dev/null +++ b/include/queryModel.h @@ -0,0 +1,56 @@ +// +// Created by ricardo on 2022/6/10. +// + +#ifndef AUTO_BUS_GUI_QUERYMODEL_H +#define AUTO_BUS_GUI_QUERYMODEL_H +#include "railsModel.h" + +struct bus_query { + /** + * 请求产生的时间 + */ + int time; + /** + * 请求的类型 + */ + int type; + /** + * 请求产生/指向的站点 + */ + rail_node_t *node; + /** + * 指向下一个请求的指针 + */ + struct bus_query *next_node; +}; + +typedef struct bus_query bus_query_t; + +class QueryModel +{ +public: + bus_query_t *queries = nullptr; + + /** + * 析构函数 + * 负责释放内存 + */ + ~QueryModel(); + + /** + * 创建请求节点 + * @param type 请求的链表 + * @param node 请求的节点 + */ + void CreateQuery(int type, rail_node_t *node); + + /** + * 删除创建的请求节点 + * @param target_query 需要删除的节点 + */ + void DeleteQuery(bus_query_t *target_query); + +}; + +#endif //AUTO_BUS_GUI_QUERYMODEL_H diff --git a/include/railsModel.h b/include/railsModel.h index e513c3d..53ce16f 100644 --- a/include/railsModel.h +++ b/include/railsModel.h @@ -40,7 +40,18 @@ public: */ rail_node_t *rails; + /** + * 生成相关的轨道对象 + * @param distance 站点直接的距离 + * @param node_number 站点的数量 + */ explicit RailsModel(int distance = 2, int node_number = 5); + /** + * 查找指定编号的站点指针 + * @param head 轨道的头节点地址 + * @param id 需要查找的站点编号 + * @return 需要查找站点指针 + */ rail_node_t *FindNode(int node_num) const; ~RailsModel(); diff --git a/src/queryModel.cpp b/src/queryModel.cpp new file mode 100644 index 0000000..9829544 --- /dev/null +++ b/src/queryModel.cpp @@ -0,0 +1,92 @@ +// +// Created by ricardo on 2022/6/10. +// +#include "queryModel.h" + +QueryModel::~QueryModel() +{ + if(queries != nullptr) + { + bus_query_t *p = queries; + + while (p != nullptr) + { + bus_query_t *temp = p; + p = p->next_node; + free(temp); + } + + queries = nullptr; + } +} + +void QueryModel::CreateQuery(int type, rail_node_t *node) +{ + auto *p = (bus_query_t *)malloc(sizeof (bus_query_t)); + p->node = node; + p->type = type; + p->next_node = nullptr; + + if (queries == nullptr) + { + queries = p; + } + else + { + bus_query_t *query = queries; + /** + * 标记是否存在相同的请求 + */ + bool flag = false; + + do + { + if(query->node == p->node and query->type == p->type) + { + flag = true; + break; + } + query = query->next_node; + } while (query != nullptr); + + if (flag) + { + // 存在相同的请求 + free(p); + } + else + { + // 在不存在相同的请求时 + bus_query_t *last_query = queries; + + while (last_query->next_node != nullptr) + { + last_query = last_query->next_node; + } + + last_query->next_node = p; + } + } +} + +void QueryModel::DeleteQuery(bus_query_t *target_query) +{ + if (target_query == queries) + { + queries = target_query->next_node; + } + else + { + bus_query_t *query = queries; + + // 找到被删除节点的上一个节点 + while (query->next_node != target_query) + { + query = query->next_node; + } + + query->next_node = target_query->next_node; + } + + free(target_query); +}