#P1104. 测-罗德岛源石处理管道系统
测-罗德岛源石处理管道系统
背景
最近,罗德岛急需高效处理战场回收的源石碎片。这些源石蕴含巨大能量,但性质极不稳定,需要经过多道工序才能安全利用或封存。作为罗德岛的博士,你负责设计一套自动化管道系统,模拟源石在管道中经历不同干员能力处理的流程。
每个管道阶段代表一位干员的特殊能力:
处理阶段会改变源石的能量数值。
过滤阶段会剔除不符合条件的源石。
管道中的链表如同传送带,承载着源石数据,依次经过干员们的处理。你的任务是实现这套系统,确保源石最终被安全高效地处理完毕。
题目描述
你正在实现一个管道数据处理系统。该系统包含一个链表用于存储数据,以及一个管道(基于上述链表编写)用于存储一系列处理阶段。每个处理阶段可以是以下两种类型之一:
处理阶段(Process Stage):对链表中的每个元素执行某种操作(如修改数据)
过滤阶段(Filter Stage):根据条件过滤链表中的元素(删除不满足条件的元素)
你需要实现这个系统,并处理一系列管道操作。
关于管道:管道是一种数据处理模式,它将一系列处理阶段连接起来,数据依次通过这些阶段进行处理。
当然,考虑到本题可能较难,我们提供了部分代码,你只需要补充完函数即可,连输入输出都不要考虑。
在提交代码时,你只需要提交那些补全的函数,剩下的部分无需提交
以下是为你写好的部分:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// List
typedef void (*DeleteFunction)(void*);
struct DataNode {
DeleteFunction delete_function;
void* data;
struct DataNode* next;
struct DataNode* prev;
};
typedef struct DataNode* List;
typedef struct DataNode* Node;
List create_list() {
List head = malloc(sizeof(struct DataNode));
head->next = head;
head->prev = head;
head->data = NULL;
head->delete_function = NULL;
return head;
}
int is_empty(List head);
void delete_node(List head, Node node) {
if (node == head || is_empty(head)) {
return;
}
node->prev->next = node->next;
node->next->prev = node->prev;
if (node->delete_function != NULL) {
node->delete_function(node->data);
}
free(node);
}
void pop_front(List head) {
if (is_empty(head)) {
return;
}
Node front = head->next;
delete_node(head, front);
}
void pop_back(List head) {
if (is_empty(head)) {
return;
}
Node back = head->prev;
delete_node(head, back);
}
void clear_list(List head) {
while (!is_empty(head)) {
pop_front(head);
}
}
void free_list(List head) {
clear_list(head);
free(head);
}
void push_back(List head, void* data, DeleteFunction delete_function);
void push_front(List head, void* data, DeleteFunction delete_function) {
Node node = malloc(sizeof(struct DataNode));
node->data = data;
node->delete_function = delete_function;
Node front = head->next;
head->next = node;
node->prev = head;
node->next = front;
front->prev = node;
}
Node find_node(List head, void* data, int (*equal)(const void*, const void*));
void print_list(List head, void (*print_function)(void*)) {
if (is_empty(head)) {
printf("empty list\n");
return;
}
Node current = head->next;
while (current != head) {
print_function(current->data);
current = current->next;
if (current != head) {
printf(" <-> ");
}
}
printf("\n");
}
void push_back_int(List head, int data) {
int* p = malloc(sizeof(int));
*p = data;
push_back(head, p, free);
}
void push_front_int(List head, int data);
int equal_int(const void* a, const void* b) {
return *(int*)a == *(int*)b;
}
Node find_node_int(List head, int data) {
return find_node(head, &data, equal_int);
}
void print_function_int(void* data) {
printf("%d", *(int*)data);
}
void print_list_int(List head) {
print_list(head, print_function_int);
}
// Pipeline
enum PipelineStageType {
PIPELINE_STAGE_PROCESS,
PIPELINE_STAGE_FILTER,
};
typedef void (*ProcessFunction)(void* data, void* config);
typedef int (*FilterFunction)(void* data, void* config);
union PipelineFunction {
ProcessFunction process;
FilterFunction filter;
};
struct PipelineStageData {
union PipelineFunction function;
enum PipelineStageType type;
void* config;
};
typedef List Pipeline;
typedef Node PipelineNode;
typedef struct PipelineStageData* PipelineStage;
void apply_pipeline_stage(List head, PipelineStage stage);
void free_pipeline_stage(void* data);
void add_pipeline_stage(Pipeline pipeline, PipelineStage stage) {
push_back(pipeline, stage, free_pipeline_stage);
}
void pipeline_process(Pipeline pipeline, List head);
void add_func(void* data, void* config) {
*(int*)data += *(int*)config;
}
PipelineStage stage_add(int data) {
PipelineStage stage = malloc(sizeof(struct PipelineStageData));
stage->type = PIPELINE_STAGE_PROCESS;
stage->config = malloc(sizeof(int));
*(int*)stage->config = data;
stage->function.process = add_func;
return stage;
}
PipelineStage stage_mul(int data);
PipelineStage stage_is_even();
int in_range_func(void* data, void* config);
PipelineStage stage_in_range(int lower, int upper) {
PipelineStage stage = malloc(sizeof(struct PipelineStageData));
stage->type = PIPELINE_STAGE_FILTER;
stage->config = malloc(sizeof(int) * 2);
((int*)stage->config)[0] = lower;
((int*)stage->config)[1] = upper;
stage->function.filter = in_range_func;
return stage;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
List data_list = create_list();
for (int i = 0; i < n; i++) {
char op[20];
scanf("%s", op);
if (strcmp(op, "push_back") == 0) {
int k;
scanf("%d", &k);
push_back_int(data_list, k);
}
else if (strcmp(op, "push_front") == 0) {
int k;
scanf("%d", &k);
push_front_int(data_list, k);
}
else if (strcmp(op, "find") == 0) {
int k;
scanf("%d", &k);
if (find_node_int(data_list, k)) {
printf("exist\n");
}
}
}
print_list_int(data_list);
int m;
scanf("%d", &m);
Pipeline pipeline = create_list();
for (int i = 0; i < m; i++) {
char op[20];
scanf("%s", op);
if (strcmp(op, "add") == 0) {
int k;
scanf("%d", &k);
add_pipeline_stage(pipeline, stage_add(k));
}
else if (strcmp(op, "mul") == 0) {
int k;
scanf("%d", &k);
add_pipeline_stage(pipeline, stage_mul(k));
}
else if (strcmp(op, "even") == 0) {
add_pipeline_stage(pipeline, stage_is_even());
}
else if (strcmp(op, "range") == 0) {
int a, b;
scanf("%d %d", &a, &b);
add_pipeline_stage(pipeline, stage_in_range(a, b));
}
}
pipeline_process(pipeline, data_list);
print_list_int(data_list);
printf("\n");
free_list(data_list);
free_list(pipeline);
}
return 0;
}
//提交后你的代码会放在这里
你需要补充这些函数,提交时也只要提交这些函数(当然,你可以创建一些其他的辅助函数),其他写好的代码无需提交:
// 检查链表是否为空
int is_empty(List head);
// 在链表尾部插入节点
void push_back(List head, void* data, DeleteFunction delete_function);
// 在链表中查找包含指定数据的节点
Node find_node(List head, void* data, int (*equal)(const void*, const void*));
// 在链表头部插入整型数据
void push_front_int(List head, int data);
// 对链表应用单个管道阶段
void apply_pipeline_stage(List head, PipelineStage stage);
// 释放管道阶段内存
void free_pipeline_stage(void* data);
// 对整个管道处理链表数据(依次应用所有阶段)
void pipeline_process(Pipeline pipeline, List head);
// 创建乘法处理阶段(配置为乘数)
PipelineStage stage_mul(int data);
// 创建偶数过滤阶段
PipelineStage stage_is_even();
// 范围过滤函数:检查数据是否在指定范围内(区间是左闭右开)
int in_range_func(void* data, void* config);
9
7
push_back 1
push_back -23
push_front 12
push_back 0
push_front -8
push_front 24
push_front 21
4
mul 4
mul 3
range 9 15
range 0 11
6
push_back -7
push_front -9
push_front -7
push_front -6
find -9
find -7
3
mul 4
add 7
add 2
12
push_back -4
push_front 6
push_front 14
push_back -25
push_front -36
push_back -33
push_front 5
push_front -19
push_front 26
push_front -13
find -33
find -19
6
add 9
range -6 6
range 4 18
add 7
range -8 7
mul 6
13
push_back -21
push_back -8
push_front 24
push_back 14
push_front 32
push_back 9
push_front 11
push_front 20
push_front 106
push_front 6
push_back -19
push_front 84
push_back -1
3
add -7
even
add -8
6
push_back 7
push_front -29
push_back 6
push_back -4
push_back -2
find 6
2
mul 3
add -2
11
push_back 26
push_back 2
push_back -15
push_front 4
push_front -7
push_front 4
push_front -11
push_front -21
push_back 23
push_back -8
find -7
4
range -10 2
range -1 10
mul 4
range -4 3
8
push_back 69
push_back 17
push_front 65
push_back 2
push_back -2
push_front -12
push_back 20
find 65
2
even
mul 5
11
push_front 29
push_back -13
push_front 7
push_front -2
push_front 5
push_back -11
push_back 1
push_back -1
push_back -6
push_back 23
find -2
3
range -4 13
range 4 10
mul 2
17
push_front 17
push_front 7
push_front -25
push_back 2
push_front 26
push_back -26
push_front 12
push_back -10
push_front -14
push_back -19
push_back -18
push_front 15
push_front 29
push_back -7
push_back 10
find -26
find 15
3
add 4
range -3 10
range 4 14
21 <-> 24 <-> -8 <-> 12 <-> 1 <-> -23 <-> 0
empty list
exist
exist
-6 <-> -7 <-> -9 <-> -7
-15 <-> -19 <-> -27 <-> -19
exist
exist
-13 <-> 26 <-> -19 <-> 5 <-> -36 <-> 14 <-> 6 <-> -4 <-> -25 <-> -33
empty list
84 <-> 6 <-> 106 <-> 20 <-> 11 <-> 32 <-> 24 <-> -21 <-> -8 <-> 14 <-> 9 <-> -19 <-> -1
-4 <-> -36 <-> -6 <-> -34 <-> -16
exist
-29 <-> 7 <-> 6 <-> -4 <-> -2
-89 <-> 19 <-> 16 <-> -14 <-> -8
exist
-21 <-> -11 <-> 4 <-> -7 <-> 4 <-> 26 <-> 2 <-> -15 <-> 23 <-> -8
empty list
exist
-12 <-> 65 <-> 69 <-> 17 <-> 2 <-> -2 <-> 20
-60 <-> 10 <-> -10 <-> 100
exist
5 <-> -2 <-> 7 <-> 29 <-> -13 <-> -11 <-> 1 <-> -1 <-> -6 <-> 23
10 <-> 14
exist
exist
29 <-> 15 <-> -14 <-> 12 <-> 26 <-> -25 <-> 7 <-> 17 <-> 2 <-> -26 <-> -10 <-> -19 <-> -18 <-> -7 <-> 10
6
相关
在下列比赛中: