您的位置:首页 > 大数据 > 人工智能

poj 1363 Rails

2013-11-01 17:09 447 查看
栈实现火车进站,出站。注意输入输出格式。

#include <iostream>
using namespace std;
#define MAX 1001
#define STACK_ININ_SIZE 100
#define STACK_INCREAMT 10
typedef struct stack
{
int stacksize;
int *base;
int *top;
}stack;

void InitStack(stack &s)
{
s.base = (int *)malloc(STACK_ININ_SIZE*sizeof(int));
s.top = s.base;
s.stacksize = STACK_ININ_SIZE;
}

void Pop(stack &s)
{
if (s.base == s.top)
return;
s.top--;
}

void Push(stack &s, int n)
{
if (s.top-s.base>=s.stacksize)
{
s.base = (int *)realloc(s.base, (s.stacksize+STACK_INCREAMT)*sizeof(int));
s.top = s.base + s.stacksize;
s.stacksize += STACK_INCREAMT;
}
*s.top = n;
s.top++;
}
int IsEmpty(stack &s)
{
if (s.base == s.top)
return 1;
else
return 0;
}
stack s;
int main()
{
int num, i, j, flag;
int ch[MAX];
flag = 0;
while (cin>>num && num)
{
if (flag==1)
cout<<endl;
flag = 1;
while (1)
{

cin>>ch[1];
if (ch[1]==0)
break;
for (i=2; i<=num; i++)
cin>>ch[i];
InitStack(s);
j = 1;
for (i=1; i<=num; i++)
{
Push(s, i);
while (!IsEmpty(s))
{
if (*(s.top-1)==ch[j])
{
Pop(s);
j++;
}
else
break;
}
}
if (IsEmpty(s))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: