add: data-structure-lab & compiler-lab

This commit is contained in:
2024-10-30 17:23:52 +08:00
commit eb8c4fa451
35 changed files with 4266 additions and 0 deletions

71
maze/include/maze_node.h Normal file
View File

@@ -0,0 +1,71 @@
//
// Created by ricardo on 2022/10/25.
//
#ifndef MAZE_MAZE_NODE_H
#define MAZE_MAZE_NODE_H
// 全局的节点总数变量
// 虽然全局变量有点不好
extern int node_number;
struct maze_connect_node
{
// 连接的节点数组索引
int index;
struct maze_connect_node* next;
};
typedef struct maze_connect_node maze_connect_node_t;
typedef struct maze_connect_node* maze_connect_node_p;
struct maze_node
{
// 迷宫中结点的编号
int id;
// 与这个节点相连的节点链表
maze_connect_node_p connect_maze_nodes;
// 当前正在遍历的节点
maze_connect_node_p now_node;
};
// 迷宫节点结构体
typedef struct maze_node maze_node_t;
// 迷宫节点结构体指针
typedef struct maze_node* maze_node_p;
/**
* 创建迷宫节点数组
* @param num 迷宫中节点的数量
* @return 数组地址
*/
maze_node_p create_maze_node_array(int num);
/**
* 在指定的节点中添加与之相连的节点
* @param node 指定的节点引用
* @param target 与之相连的节点
* @return 是否添加成功
*/
bool maze_node_add_connect_node(maze_node_t& node, int target);
/**
* 读取迷宫配置文件
* @param filename 配置文件文件名
* @return 迷宫节点数组地址
*/
maze_node_p read_maze_file(char* filename);
/**
* 打印整个迷宫
* @param node_array 迷宫数组
*/
void print_maze(maze_node_p node_array);
/**
* 释放迷宫节点数组占据的空间
* @param node_array 迷宫节点数组引用
*/
void maze_node_free(maze_node_p& node_array);
#endif //MAZE_MAZE_NODE_H

83
maze/include/stack.h Normal file
View File

@@ -0,0 +1,83 @@
//
// Created by ricardo on 2022/10/25.
//
#ifndef MAZE_STACK_H
#define MAZE_STACK_H
#include "cstdlib"
#include "maze_node.h"
// 默认的栈大小
#define DEFAULT_STACK_LENGTH 200
// 由于将迷宫中的所有节点都储存在数组中
// 存储路径的栈就不用存储节点的指针
// 而是直接存储节点在数组中的索引
struct stack {
int* top;
int* base;
int stack_size;
};
typedef struct stack stack_t;
typedef struct stack* stack_p;
/**
* 初始化栈空间
* @param s 栈结构体的引用
* @return 栈是否创建成功
*/
bool init_stack(stack_t& s);
/**
* 判断栈是否为空
* @param s 对于栈结构体的引用
* @return
*/
bool stack_is_empty(const stack_t& s);
/**
* 判断栈是否已满
* @param s 对于结构体的引用
* @return
*/
bool stack_is_full(const stack_t& s);
/**
* 在栈中压入元素
* @param s 栈引用
* @param value 被压入栈的值
* @return 压入栈是否成功
*/
bool stack_push(stack_t& s, int index);
/**
* 在栈中弹出元素
* @param s 栈引用
* @param value 被弹出栈的值
* @return 弹出栈是否成功
*/
bool stack_pop(stack_t& s, int* index);
/**
* 获得栈底的元素
* @param s 栈引用
* @return
*/
int stack_get_bottom(const stack_t& s);
/**
* 获得栈顶的元素
* @param s 栈引用
* @return
*/
int stack_get_top(const stack_t& s);
/**
* 释放栈所占用的空间
* @param s 栈引用
*/
void stack_free(stack_t& s);
#endif //MAZE_STACK_H