添加了请求控制类
添加了注释
This commit is contained in:
parent
6380265faa
commit
35fa35de3f
56
include/queryModel.h
Normal file
56
include/queryModel.h
Normal file
|
@ -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
|
|
@ -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();
|
||||
|
||||
|
|
92
src/queryModel.cpp
Normal file
92
src/queryModel.cpp
Normal file
|
@ -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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user