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

    何海涛面试题22

    #include<iostream>
    #include<stack>
    using namespace std;
    bool IsPopOrder(int a[],int b[],int n)//压入序列和弹出序列及长度
    {
    	bool flag=false;
    	if(n>0)
    	{
    		int i=0,j=0;
    		stack<int>s;
    		while(j<n)
    		{
    			while(s.empty()||s.top()!=b[j])//压入a[5]={1,2,3,4,5}直到元素等于b[0],b[5]={4,5,3,2,1}(正确)和{4,3,5,2,1}(错误)
    			{
    				if(i==n)break;//没找到
    				s.push(a[i]);
    				i++;
    			}
    			if(s.top()!=b[j])//下一个出栈的元素不等于栈顶元素
    			{
    				break;
    			}
    			s.pop();//出栈
    			j++;
    		}
    		if(s.empty()&&j==n)
    			flag=true;
    	}
    	return flag;
    }
    
  • 相关阅读:
    sed
    UCOSIII(二)
    UCOSIII(一)
    IIC
    SPI
    vii
    find
    grep
    Scrum项目4.0
    Scrum项目3.0
  • 原文地址:https://www.cnblogs.com/tgkx1054/p/2769014.html
Copyright © 2011-2022 走看看