提交 1c972109 编辑于 作者: Martin77's avatar Martin77
浏览文件

remain free memory

上级 3f6120c1
#IncludeRegexLine: ^[ ]*[#%][ ]*(include|import)[ ]*[<"]([^">]+)([">])
#IncludeRegexScan: ^.*$
#IncludeRegexComplain: ^$
#IncludeRegexTransform:
D:/C-PL/2021-C-PL-Solution/10-struct/stackPour.c
stdio.h
-
malloc.h
-
# CMAKE generated file: DO NOT EDIT!
# Generated by "MinGW Makefiles" Generator, CMake Version 3.20
CMakeFiles/stackPour.dir/stackPour.c.obj
D:/C-PL/2021-C-PL-Solution/10-struct/stackPour.c
# Empty dependencies file for stackPour.
# This may be replaced when dependencies are built.
# CMAKE generated file: DO NOT EDIT!
# Generated by "MinGW Makefiles" Generator, CMake Version 3.20
CMakeFiles/stackPour.dir/stackPour.c.obj: \
../stackPour.c
Start testing: Dec 14 10:52 ?D1???
Start testing: Dec 14 17:55 ?D1???
----------------------------------------------------------
End testing: Dec 14 10:52 ?D1???
End testing: Dec 14 17:55 ?D1???
......@@ -4,12 +4,60 @@
#include <stdio.h>
#include <malloc.h>
struct stack {
typedef struct stack {
int pointer;
// 存放栈的值
int* val;
};
int val[1000006];
}STACK;
void pour(STACK* x, STACK* y);
void output(STACK* stacks, int len);
int main() {
int stackAmount, n;
scanf("%d %d", &stackAmount, &n);
// 构建一个stack的数组
STACK* stacks = malloc((stackAmount+1) * sizeof(STACK));
for (int i = 1; i <= stackAmount; i++) {
(stacks + i)->pointer = 0;
(stacks + i)->val[0] = i;
}
for (int i = 0; i < n; i++) {
int x, y;
scanf("%d %d", &x, &y);
pour(&stacks[x], &stacks[y]);
}
output(stacks, stackAmount);
free(stacks);
return 0;
}
void pour(STACK* x, STACK* y) {
// 将x倒入y里面
// 没有元素的话我们将pointer置为-1
int len = x->pointer;
int index = y->pointer;
for (int i = len; i >= 0; i--) {
y->val[++index] = x->val[i];
}
x->pointer = -1;
y->pointer = index;
}
void output(STACK* stacks, int len) {
for (int i = 1; i <= len; i++) {
if (stacks[i].pointer == -1) {
printf("0\n");
} else {
int top = stacks[i].pointer;
for (int j = 0; j <= top; j++) {
printf("%d ", stacks[i].val[j]);
}
stacks[i].pointer = -1;
printf("\n");
}
}
}
\ No newline at end of file
Supports Markdown
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册