问题描述:
    假设停在铁路调度站入口处的车厢序列的编号依次为1,2,3,...n。设计一个程序,求  出所有可能由此输出的长度为n的车厢序列
基本要求:
用栈的顺序存储结构实现基本操作。
该问题要用递归的思想。
我做了程序但不懂 望高手指点迷津
大家可以提出自己的想法 ,互相交流
#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);
}
[此贴子已经被作者于2006-5-30 18:22:57编辑过]

 
											





