zoukankan      html  css  js  c++  java
  • 栈的压入和弹出序列

    /*
     * 栈的压入和弹出序列.cpp
     *
     *  Created on: 2018年4月17日
     *      Author: soyo
     */
    #include<iostream>
    #include<stack>
    using namespace std;
    int judgeStack(int a[],int b[])
    {
        stack<int> s;
        int i=0;
        int k=0;  //用来计数
        for(int j=0;j<5;j++)
        {
            if(s.empty()||s.top()!=b[i])
            {
                s.push(a[j]);
                k++;
            }
            while(s.top()==b[i])
            {
                s.pop();
                i++;
                k--;
                if(k==0)
                    return 1;
            }
        }
        if(k!=0)
          return 0;
    
    
    }
    int main()
    {
        int InStack[5]={1,2,3,4,5};
        int outStack[5]={4,5,3,2,1};
        //int outStack[5]={4,3,5,1,2};
       int find=judgeStack(InStack,outStack);
       cout<<find<<endl;
       if(find==1)
       {
            cout<<"第二个序列是第一个序列的弹出顺序"<<endl;
       }
       else{
           cout<<"不是弹出顺序"<<endl;
       }
    }

    结果:

    1
    第二个序列是第一个序列的弹出顺序

  • 相关阅读:
    os.remove some jpgs
    shutil.rmtree, os.path, delete sub-folders, format
    How to create folder
    valgrind
    gstream
    TP TN FP FN
    tensor flow
    接口中静态方法和默认方法
    JAVA基础09
    JAVA基础08
  • 原文地址:https://www.cnblogs.com/soyo/p/8869905.html
Copyright © 2011-2022 走看看