数据结构车厢调度_数据库车厢调度
数据结构车厢调度由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“数据库车厢调度”。
#include #include
#define STACK_INIT_SIZE 20 #define STACK_SIZE
typedef struct { int *base;int *top;int stacksize;} SqStack;
bool InitStack(SqStack &S){ // 初始化栈
S.base =(int *)malloc(STACK_INIT_SIZE*sizeof(SqStack));if(!S.base)
exit(0);S.top = S.base;S.stacksize = STACK_INIT_SIZE;return true;} // InitStack
bool GetTop(SqStack S, int &e){ // if(S.top == S.base)
return false;e = *(S.topS.base >= S.stacksize){
S.base =(int *)realloc(S.base,(S.stacksize + STACK_SIZE)*sizeof(SqStack));
if(!S.base)
exit(0);
S.top = S.base + S.stacksize;
S.stacksize += STACK_SIZE;} *S.top++ = e;return true;} // Push
bool Pop(SqStack &S, int &e){ if(S.top == S.base)
return false;e = *--S.top;return true;}
///////////////////////////////////////////////////////////////////////////////////
bool Judement(int *Array, int n){ int Count = 0;for(int i = 1;i
Count++;if(Count == n && Array[2*n] == 0)
一个是出栈
return true;else
return false;}
void operation(SqStack S, int n){ // n代表获得数量,分别标记为1,2,3....n int *Array;int nCount = 0, e;Array =(int *)malloc((2 * n + 1)* sizeof(int));
if(!Array)
exit(0);// 将其初始化为1234..n的形式
for(int i = 1;i
if(i%2 == 1)
Array[i] = 1;
else
Array[i] = 0;
while(1){
if(Judement(Array, n))
{
nCount = 0;
for(int i = 1;i
{
if(Array[i] == 1)
Push(S, ++nCount);
// 必须保证最后
//从1开始
// 代表入栈
// 代表出栈
else
{
Pop(S, e);
printf(“%d ”, e);
}
}
printf(“n”);
}
Array[2*n] += 1;
// 最后一个自增1,等于2时本为为0前一位加1
for(int i = 2*n;i >= 1;i--)
{
if(Array[i] == 2)
{
Array[i-1] += 1;
Array[i] = 0;
}
}
nCount = 0;
for(int i = 1;i
{
if(Array[i] == 1)
nCount++;
}
if(nCount == n && Array[2*n] == 1)
break;
// 退出循环的条件为前n个为1同时最后一位也为1 } }
void main(){ // 这个是在左边有1,2,3...n节车厢的情况,得出在右边所有的情况
SqStack S;InitStack(S);int n;scanf_s(“%d”, &n);if(n
printf(“输入数字小于1”);
return;} operation(S, n);