# 2.1数据结构—栈

• 数据结构

2016-03-04

## 栈与STL

C++的STL里面已经内置了栈，使用时调用stack头文件。使用时像是操作基本类型一样，具体方法参考C++API，下面简单介绍一下基本的使用方法。

#include <iostream>
#include <stack>
using namespace std;
int main ()
{
stack<int> mystack;
for (int i=0; i<5; ++i)
mystack.push(i);
cout << "Popping out elements...";

while (!mystack.empty())
{
cout << " " << mystack.top();
mystack.pop();
}
cout << endl;
return 0;
}
//Popping out elements... 4 3 2 1 0

### 例题1：Train Problem I

Problem Description

As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes a problem, there is only one railway where all the trains stop. So all the trains come in from one side and get out from the other side. For this problem, if train A gets into the railway first, and then train B gets into the railway before train A leaves, train A can't leave until train B leaves. The pictures below figure out the problem. Now the problem for you is, there are at most 9 trains in the station, all the trains has an ID(numbered from 1 to n), the trains get into the railway in an order O1, your task is to determine whether the trains can get out in an order O2.

Input

The input contains several test cases. Each test case consists of an integer, the number of trains, and two strings, the order of the trains come in:O1, and the order of the trains leave:O2. The input is terminated by the end of file. More details in the Sample Input.

Output

The output contains a string "No." if you can't exchange O2 to O1, or you should output a line contains "Yes.", and then output your way in exchanging the order(you should output "in" for a train getting into the railway, and "out" for a train getting out of the railway). Print a line contains "FINISH" after each test case. More details in the Sample Output.

Sample Input

3 123 321

3 123 312

Sample Output

Yes.

in

in

in

out

out

out

FINISH

No.

FINISH

AC代码：

#include<iostream>
#include<cstdlib>
#include<string>
#include<cstring>

using namespace std;
const int Stacksize=100;
const int INCREMENT=50;

typedef struct
{
char *top;
char *base;
int stacksize;
}Stack;

int main()
{
int a[1000];
int i,k,count;
Stack S;
int n;
string s1,s2;
while(cin>>n>>s1>>s2)
{
memset(a,0,sizeof(a));
S.base=(char*)malloc(Stacksize*sizeof(char));
if(!S.base)
return 0;
S.top=S.base;
S.stacksize=Stacksize;
i=k=count=0;
while(k<n)
{
if(S.top-S.base>=S.stacksize)
{
S.base=(char *)malloc((S.stacksize+INCREMENT)*sizeof(char));
if(!S.base)
return 0;
S.top=S.base+S.stacksize;
S.stacksize+=INCREMENT;
}

if(S.base==S.top)//如栈为空，进栈
{
*S.top=s1[i];
a[count++]=1;
i++;
S.top++;
}
else
{
if(*(S.top-1)==s2[k])//出栈
{
S.top--;
k++;
count++;
}
else
{
if(i==n)//如果已经i已经等于n了表示结束
break;
*S.top=s1[i];//else继续进栈
S.top++;
i++;
a[count++]=1;
}
}
}

if(S.top==S.base)
{
cout<<"Yes.\n";
for(i=0;i<count;i++)
{
if(a[i])
cout<<"in\n";
else
cout<<"out\n";
}
cout<<"FINISH\n";
}
else
cout<<"No.\nFINISH\n";

free(S.base);
}
return 0;
}

### 例题2：简单计算器

Problem Description

Input

Output

Sample Input

1 + 2

4 + 2 * 5 - 7 / 11

0

Sample Output

3.00

13.36

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<stack>using namespace std;
int main()
{
double num[300],t,sum;
int i;
char op;
while(cin>>t)
{
memset(num,0,sizeof(num));
num[0]=t;
i=0;
op=getchar();
if(op=='\n'&&t==0)break;
while(1)
{
cin>>op>>t;
if(op=='*')num[i]*=t;
else if(op=='/')num[i]/=t;
else if(op=='+')num[++i]=t;
else num[++i]=-t;
if(getchar()=='\n')break;
}
for(sum=0;i>=0;i--)
sum+=num[i];
printf("%.2lf\n",sum);
}
return 0;
}

## 相关习题

Hdu 1022、1702、3228

Poj  1028、1363

written by 金磊~~，转载注明出处。

0