中华纸业员工邮箱:车厢调度 c语言 数据结构

来源:百度文库 编辑:高考问答 时间:2024/04/30 22:52:07
用c语言编程,题目是:
假设停在铁路调度站入口车厢序列编号依次为1,2,3....n,设计一个程序,求出所有的输出可能的序列.
我是说用c语言编的 你们的帖子好像不对啊

#include<iostream.h>
#include<assert.h>

class stack
{
private:
int * element;
int maxsize;
int top;
public:
void friend search(stack &s1,stack &s2,stack &s3);
stack(int n)
{ element=new int [n];
maxsize=n;
assert(element!=NULL);
top=-1;
}
void stack::push(int n)
{
assert(!isfull());
top++;
element[top]=n;
}
int stack::pop()
{
assert(!isempty());
int a=element[top];
top--;
return a;
}
bool isfull()
{
if(top==maxsize-1)
return 1;
else
return 0;
}
bool isempty()
{
if(top==-1)
return 1;
else
return 0;
}
void stack::print()
{
for(int i=0;i<top+1;i++)
cout<<element[i];
cout<<endl;
}
};
void search(stack &s1,stack &s2,stack &s3)
{
if(!s1.isempty())
{
s2.push(s1.pop());
search(s1,s2,s3);
s1.push(s2.pop());
}

if(!s2.isempty())
{
s3.push(s2.pop());
search(s1,s2,s3);
s2.push(s3.pop());
}
if( s3.isfull())
{
s3.print();
}
}
void main()
{
cout<<"请输车厢长度n: ";
int n; cin>>n;
stack s1(n);
for(int i=n;i>=1;i--)
s1.push(i);
stack s2(n),s3(n);
cout<<"输出序列为:"<<endl;
search(s1,s2,s3);
}

void S(Stack &S1, Stack &S2, Stack &S3) {
// 已知三个栈的初始状态为:S2和S3为空栈, 栈S1中从栈顶到栈底依次存放元素1至n,
// 本函数利用三个栈求得元素1至n 经入栈到出栈可能得到的所有排列。
// 递归的终结状态是S1栈和S2栈均为空栈。
if(StackEmpty(S1) &&StackEmpty(S2)
从栈底到栈顶输出栈S3中所有元素;
else{
if (!StackEmpty(S1)) {
Pop(S1, e); Push(S2, e);
S(S1, S2, S3);
Pop(S2, e); Push(S1, e);
}
if (!StackEmpty(S2)) {
Pop(S2, e); Push(S3, e);
S(S1, S2, S3);
Pop(S3, e); Push(S2, e);
}
}
}// S

从网上搜到的,老实说,我也看不懂