diff --git a/CMakeLists.txt b/CMakeLists.txt index e786e01..2e2a5e2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,9 +18,11 @@ include_directories( ${PROJECT_SOURCE_DIR}/include ${PROJECT_SOURCE_DIR}/src/header ) + aux_source_directory(${PROJECT_SOURCE_DIR}/src SRCS) add_executable(auto_bus_gui main.cpp ${SRCS}) + target_link_libraries(auto_bus_gui Qt::Core Qt::Gui diff --git a/include/railsModel.h b/include/railsModel.h new file mode 100644 index 0000000..e513c3d --- /dev/null +++ b/include/railsModel.h @@ -0,0 +1,51 @@ +// +// Created by ricardo on 2022/6/10. +// + +#ifndef AUTO_BUS_GUI_RAILSMODEL_H +#define AUTO_BUS_GUI_RAILSMODEL_H +#include "cstdlib" + +struct rail_node{ + /** + * 站点的编号 + */ + int id; + /** + * 距离上一个站点的距离 + */ + int last_node_distance; + /** + * 距离下一个站点的距离 + */ + int next_node_distance; + /** + * 指向上一个站点的指针 + */ + struct rail_node* last_node; + /** + * 指向下一个站点的指针 + */ + struct rail_node* next_node; +}; + +typedef struct rail_node rail_node_t; + +class RailsModel +{ + +public: + /** + * 轨道的头节点 + */ + rail_node_t *rails; + + explicit RailsModel(int distance = 2, int node_number = 5); + rail_node_t *FindNode(int node_num) const; + ~RailsModel(); + + + +}; + +#endif //AUTO_BUS_GUI_RAILSMODEL_H diff --git a/src/railsModel.cpp b/src/railsModel.cpp new file mode 100644 index 0000000..4857d56 --- /dev/null +++ b/src/railsModel.cpp @@ -0,0 +1,77 @@ +// +// Created by ricardo on 2022/6/10. +// + +#include "railsModel.h" + +RailsModel::RailsModel(int distance, int node_number) +{ + rail_node_t *head; + rail_node_t *node; + + head = (rail_node_t *) malloc(sizeof(rail_node_t)); + head->id = 1; + head->last_node_distance = distance; + head->next_node_distance = distance; + + node = head; + // 循环创建每一个节点 + for(int i = 2; i <= node_number; i++) + { + auto *p = (rail_node_t*) malloc(sizeof (rail_node_t)); + + p->id = i; + p->last_node_distance = distance; + p->next_node_distance = distance; + p->last_node = node; + node->next_node = p; + node = p; + } + + // 将链表头尾链接起来 + node->next_node = head; + head->last_node = node; + + rails = head; +} + +RailsModel::~RailsModel() +{ + rail_node_t *node = rails; + + // 断开头尾节点 + node->last_node->next_node = nullptr; + + while (node != nullptr) + { + rail_node_t *p = node; + node = node->next_node; + free(p); + } + + // 将指针置为空 + rails = nullptr; +} + +rail_node_t *RailsModel::FindNode(int node_num) const +{ + if (rails == nullptr) + { + return nullptr; + } + + rail_node_t *result = nullptr; + rail_node_t *node = rails; + + do + { + if(node->id == node_num) + { + result = node; + break; + } + node = node->next_node; + } while (node != rails); + + return result; +} \ No newline at end of file