zoukankan      html  css  js  c++  java
  • 10.12 说明如何用一个数组A[1..n]来实现两个栈,使得两个栈中的元素总数不到n时,两者都不会发生上溢,注意PUSH和POP操作的时间应为O(1)。

    #include "iostream"
    #include "string"
    using namespace std;
    
    int A[9]={-1,1,2,3,4,5,6,7,8};
    int top_s1=0,top_s2=9;
    
    bool PUSH(string s,int x){
        if(top_s1+1==top_s2)
            return false;
        else{
            if(s=="s1"){
                top_s1++;
                A[top_s1]=x;
            }
            else{
                top_s2--;
                A[top_s2]=x;
            }
            return true;
        }
    }//入栈
    
    int POP(string s){
        if(s=="s1"&&top_s1==0||s=="s1"&&top_s2==9)
            return -1;
        else{
            if(s=="s1"){
                top_s1--;
                return A[top_s1+1];
            }
            else{
                top_s2++;
                return A[top_s2-1];
            }
        }
    }//出栈
    
    void main(){
        PUSH("s1",12);
        PUSH("s2",32);
        PUSH("s1",67);
        PUSH("s2",24);
        PUSH("s1",13);
        PUSH("s1",14);
        PUSH("s1",15);
        PUSH("s1",16);
        if(!PUSH("s1",11))
            cout<<"堆栈已经满了!!!"<<endl;
        for(int i=1;i<9;i++)
            cout<<A[i]<<" ";
        cout<<endl;
        getchar();
        getchar();
    }
  • 相关阅读:
    Linux 常用命令
    Oracle DG 三种模式(转)
    S5PV2210
    Timer wheel etc.
    SCM etc.
    负载均衡 IO etc.
    Remoting,OData Snippet Compiler等
    displaytag 动态列实现
    <display:column>属性解释
    <display:table>属性解释
  • 原文地址:https://www.cnblogs.com/593213556wuyubao/p/2952468.html
Copyright © 2011-2022 走看看