地铁出站顺序问题(栈的输出顺序)

来源:互联网 发布:飞思卡尔的双核单片机 编辑:程序博客网 时间:2024/04/28 07:40

    有这样一个具体问题,假设地铁只能在一条轨道上行驶,一条轨道上有多个地铁,地铁夜晚停车时需要停放在一条轨道上,早上发车时就存在一个出来的顺序问题?那么怎么解决发车时他们的顺序是怎么样呢?

    假设有3个地铁,编号分别是1,2,3.在地铁上的顺序是1,2,3。在停车时他们可以顺序进入停车,停车时也是1,2,3,但是早上发车时必然是3,2,1.显然这是一个典型的先进后出,如何罗列出他们的所有情况呢?

   我的基本思想是这样的,如果有3个,那么他们的停车出车必然会进行2*3=6次,每一个车必然会进去,出来,如果我们把进去标记为1出来标记为0,那么用一个长度为6的整形数组便可以存储他们的情况,但是要注意这个数组第一个元素必然是1进车,而最后一个必然是0出车,并且必然不会是100110这种情况,因为一个车进去,不可能有两个车出来,解决了以上问题,我们得到了所有情况下数组的值,便可以使用堆栈数据结构模拟所有地铁的进车出车过程,以及最后的顺序。

下面给出代码:

#include<stdio.h>
#include<stdlib.h>
struct train{
 int n;
 int p;
};
struct stack{
 train *base;
 train *top;
 int num;
};
struct hh{
 int *base;
 int *top;
 int num;
};
int n;
void moni(hh *h);
void Inith(hh *s){
 s->base=(int*)malloc(20*sizeof(int));
 if(s->base==NULL)exit(0);
 s->top=s->base;
 s->num=20;
}
void pushh(hh *s,int e)
{
 if((s->top-s->base)>=s->num)
 {
  s->base=(int*)realloc(s->base,(s->num+10)*sizeof(int));
  if(s->base==NULL)exit(0);
  s->num=s->num+10;
 }
 *s->top=e;
 s->top++;
}
void poph(hh *s,int *e)
{
 if(s->top==s->base)exit(0);
 *e=*--s->top;
}
void Init(stack *s){
 s->base=(train*)malloc(20*sizeof(train));
 if(s->base==NULL)exit(0);
 s->top=s->base;
 s->num=20;
}
void push(stack *s,train e)
{
 if(s->top-s->base>=s->num)
 {
  s->base=(train*)realloc(s->base,(s->num+10)*sizeof(train));
  if(s->base==NULL)exit(0);
  s->num=s->num+10;
 }
 *s->top=e;
 s->top++;
}
void pop(stack *s,train *e)
{
 if(s->top==s->base)exit(0);
 *e=*--s->top;
}
void print(hh *h)
{
 hh *t;
 t=(hh*)malloc(sizeof(hh));
 Inith(t); 
 while(h->base!=h->top)
 {
  int nn;
  poph(h,&nn);
  printf("%d",nn);
  pushh(t,nn);
 }
 printf("\n");
 while(t->base!=t->top)
 {
  int nn;
  poph(t,&nn);
  pushh(h,nn);
 }
}
int biaozhun(hh *h)
{
 hh *t;
 t=(hh*)malloc(sizeof(hh));
 Inith(t);
 int bj=1,i;
 int shu=0;
 shu=h->top-h->base;
 //当堆栈数没有数量装够时,使用堆栈数的个数判断
 if(n>shu)
 {
  for(i=1;i<=shu;i++)
  {
   
   int nn;
   poph(h,&nn);
   if(nn==1){bj=0;}
   pushh(t,nn);
  }
  for(i=1;i<=shu;i++)
  {
   int nn;
   poph(t,&nn);
   pushh(h,nn);
  }
 }
 else
 {
  for(i=1;i<=n;i++)
  {
   
   int nn;
   poph(h,&nn);
   if(nn==1){bj=0;}
   pushh(t,nn);
  }
  for(i=1;i<=n;i++)
  {
   int nn;
   poph(t,&nn);
   pushh(h,nn);
  }

 }
 //返回1代表满足条件,0代表不满足
 if(bj==1)
 {
  return 1;
 }
 else
 {
  return 0;
 }
}
void pailei(int n){
 
 int i;
 hh *h;
 h=(hh*)malloc(sizeof(hh));
 Inith(h);
 
 int wz=2;
 int zon=0;
 pushh(h,1);
 //zon判断是否小于0,小于0不能出车
 zon=1;
 do
 {
  for(i=wz;i<=2*n-1;i++)
  {
  
   if(zon>0)
   {
    pushh(h,-1);
    zon--;
   }
   else
   {
    pushh(h,1);
    zon++;
   }

  }
  pushh(h,-1);
  moni(h);
  printf("-------------------------------------------\n");
  //显示标记
  //print(h);
  zon--;
  //bj==1时说明出来了火车,js为计数器
  int bj=0,js=0;
  //这里有问题
  if(biaozhun(h)==0)
  {
   while(1)
   {
    js++;
    int n;
    poph(h,&n);
    if(n==1)
    {
     zon--;
     bj=1;
    }
    if(n==-1)
    {
     zon++;
    }
    if(bj==1&&n==-1)
     break;
   }
   //把最后一个能变成-1变成1;
   pushh(h,1);
   zon++;
   //位置赋值wz;
   wz=2*n-js+2;
  }
  
 }while(biaozhun(h)==0);  
}
void moni(hh *h){
 hh *temp=(hh*)malloc(sizeof(hh));
 int i;
 stack *s1,*s2,*s3;
 s1=(stack*)malloc(sizeof(stack));
 s2=(stack*)malloc(sizeof(stack));
 s3=(stack*)malloc(sizeof(stack));
 Init(s1);
 Init(s2);
 Init(s3);
 for(i=n;i>0;i--)
 {
  train t1;
  t1.n=i;
  t1.p=0;
  push(s1,t1);
 }
 //while(!quan(s3,n))
 
 Inith(temp);
 //提取数据
 while(h->base!=h->top)
 {
  int nu;
  poph(h,&nu);
  pushh(temp,nu);
 }
 //压回数据
 while(temp->base!=temp->top)
 {
  int nu;
  poph(temp,&nu);
  //当是1则进车,-1则出车
  if(nu==1){
   train t;
   pop(s1,&t);
   printf("第%d号车进入停车位置\n",t.n);
   push(s2,t);
  }
  else
  {
   train t;
   pop(s2,&t);
   printf("第%d号出车并进入轨道\n",t.n);
   push(s3,t);
  }
  //把进出车标记压回
  pushh(h,nu);
 }
 //显示运行顺序
 stack *st;
 st=(stack*)malloc(sizeof(stack));
 Init(st);
 while(s3->base!=s3->top)
 {
  train trr;
  pop(s3,&trr);
  push(st,trr);   
 }
 printf("最后轨道运行时顺序为:");
 
 while(st->base!=st->top)
 {
  train trr;
  pop(st,&trr);
  printf("%d",trr.n);
 }
 printf("\n");
 //free(st);
 free(s1);
 free(s3);
 free(s2);
 free(temp);
}
void main(){
 printf("输入几辆车\n");
 scanf("%d",&n);
 pailei(n);
}