2.1数据结构—栈

  • 数据结构
万子德·中国科学院大学
2016-03-04
阅读数907

引言

我有一个自己的小书箱,六面体形状,顶部开口,书箱很小,每次平放一本书,然后第二本需要叠放在第一本书上,第三本书叠放在第二本书上面,以此类推,里面可以放10本书,当我需要用到第10本书时,我可以直接从上面拿出来,如果我要用到第八本书,我需要先拿出第十本,再取出第九本书,然后才能拿到我需要的第八本书。

这个书箱具有什么特点呢?一端开口,最后放进去的书,可以最先取出来,而最先放进去的书,只能最后才可以取出来,因此还具有后进先出的特点,像这样的书箱,就是我们今天要学习的栈,栈具有一端开口、元素后进先出的特点。

栈的解释

栈(stack)又名堆栈,它是一种运算受限的线性表,是一种特殊的数据结构。其限制是仅允许在表的一端进行插入和删除运算。这一端被称为栈顶,相对地,把另一端称为栈底。向一个栈插入新元素又称作进栈、入栈或压栈,它是把新元素放到栈顶元素的上面,使之成为新的栈顶元素;从一个栈删除元素又称作出栈或退栈,它是把栈顶元素删除掉,使其相邻的元素成为新的栈顶元素。

栈的操作

栈支持两种操作,push和pop,push就是入栈,pop则是出栈,push每次只能将元素放到栈顶,而pop每次也只能抛出栈顶的元素。其次还有top(栈顶元素)、size等方法。

栈与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

测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。

Output

对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。

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 金磊~~,转载注明出处。

本文由 万子德 授权 赛氪网 发表,并经赛氪网编辑。转载此文章须经作者同意,并请附上出处(赛氪网)及本页链接。原文链接https://www.saikr.com/a/2577
收藏
分享
别默默的看了,快来和大家聊聊吧,登录后发表评论~ 登录 立即注册
打赏
万子德
打赏金额(金额:¥0)
给Ta留言
赏金已入袋,多谢!(*^__^*)
温馨提示

非常抱歉!本站不支持旧版本IE浏览器~~建议使用IE10/IE11/Chrome/Firefox/Safari等高级浏览器浏览。

温馨提示
温馨提示
帮助与反馈

热门问题

0
在线咨询