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

    面试题 22:栈的压入弹出序列,假设压入栈的所有数字都不相等,第一个序列表示压入顺序,第二个为弹出顺序,判断两个顺序是否相对应。

    下面我根据海涛大神的思路自己写代码实现功能

    关键思路指点:当前要弹出的数字不在栈中,就把当前压入序列的数字至要弹出的数字压入栈中,然后弹出要弹出的数字,最后如果栈为空,就说明序列对应,否则不对应

    #include<iostream>
    #include<stack>
    using namespace std;
    
    bool testStack(int *pushSequece, int pushLength, int *popSequece, int popLength){
        //讨论异常情况,如两个长度不相同,或传入空指针
        int pushBegin,popBegin;
        stack<int> mystack;
        pushBegin = popBegin = 0;
        int i;
        for (; popBegin < popLength; popBegin++){
            if (!mystack.size()){
                //stack 为空
                for (i = pushBegin; i < pushLength; i++){
                    if (pushSequece[i] == popSequece[popBegin])
                        break;
                }
                if (i == pushLength) return false;
                for (int j = pushBegin; j <= i; j++){
                    mystack.push(pushSequece[j]);
                }
                pushBegin = i + 1;
                mystack.pop();
            }else {
                if (mystack.top() != popSequece[popBegin]){
                    //如果top元素不为当前遍历popSequece的元素
                    for (i = pushBegin; i < pushLength; i++){
                        if (pushSequece[i] == popSequece[popBegin])
                            break;
                        
                    }
                    if (i == pushLength) return false;
                    for (int j = pushBegin; j <= i; j++){
                        mystack.push(pushSequece[j]);
                        cout << "测试2" << endl;
                        
                    }
                    pushBegin = i + 1;
                    mystack.pop();
                }else{
                    mystack.pop();
                }
                
            }
            
        }
        if (mystack.size() == 0)
            return true;
    
    
    }
    int main(){
        
        int push[] = { 1, 2, 3, 4, 5 };
        int length = sizeof(push) / sizeof(int);
        int pop1[] = { 4, 5, 3, 2, 1 };
        int pop2[] = { 4, 3, 5, 1, 2 };
        int pop3[] = { 1, 2, 3, 4, 5 };
        int pop4[] = { 3, 2, 5, 4, 1 };
        int pop5[] = { 3, 5, 2, 1, 4 };
        if (testStack(push, length, pop1, length))
            cout << "符合条件"<<endl;
        if (!testStack(push, length, pop2, length))
            cout << "不符合条件"<<endl;
        if (testStack(push, length, pop4, length))
            cout << "符合条件" << endl;
        if (!testStack(push, length, pop5, length))
            cout << "不符合条件" << endl;
    
        system("pause");
    }

    关键思路指点:当前要弹出的数字不在栈中,就把当前压入序列的数字至要弹出的数字压入栈中,然后弹出要弹出的数字,最后如果栈为空,就说明序列对应,否则不对应

  • 相关阅读:
    git常用命令
    Vue路由传参的几种方式
    yum安装之-安装mysql
    Xshell连接不上Ubuntu解决方式 --转
    ubuntu 命令行模式和图形界面切换 --转
    yum安装之-安装底层软件库
    VMware虚拟机 CentOS 6.5系统安装配置详细图文教程 --技术支持TPshop商城
    vmware虚拟机怎么新建虚拟机--技术支持TPshop商城
    TPshop商城使用,TPshop商城使用的安装
    tpshop商城根据用户坐标,向数据库查找附近的商家
  • 原文地址:https://www.cnblogs.com/hzmbbbb/p/3980579.html
Copyright © 2011-2022 走看看