zoukankan      html  css  js  c++  java
  • Crack Interview 3.3

    Imagine a (literal) stack of plates   If the stack gets too high, it might topple   There-
    fore, in real life, we would likely start a new stack when the previous stack exceeds
    some threshold   Implement a data structure SetOfStacks that mimics this   SetOf-
    Stacks should be composed of several stacks, and should create a new stack once
    the previous one exceeds capacity   SetOfStacks push() and SetOfStacks pop() should
    behave identically to a single stack (that is, pop() should return the same values as it
    would if there were just a single stack)
    FOLLOW UP
    Implement a function popAt(int index) which performs a pop operation on a specific
    sub-stack 

    #include <iostream>
    #include <stack>
    #include <string>
    using namespace std;
    
    class MyException{
    public:
        MyException(string str) : message(str) {}
        void PrintMessage(){cout<<message<<endl;}
    private:
        string message;
    };
    
    class SetOfStacks{
    public:
        SetOfStacks(int t) : threshold(t), currElemNum(0), currStack(0){}
        ~SetOfStacks(){};
        void push(int v);
        void pop();
        int top();
    private:
        stack<int> stacks[10];
        int currStack;            //当前工作于第几个stack
        int currElemNum;        //当前stack中的元素数量
    
        const int threshold;    //阈值
    };
    
    void SetOfStacks::push(int v){
        if (currElemNum < threshold){//当前stack未满
            stacks[currStack].push(v);
            currElemNum++;
        }
        else{                        //当前stack已满
            if (currStack+1 < 10){    //当前stack已满,但所有stack未满
                currStack++;
                stacks[currStack].push(v);
                currElemNum = 1;
            }
            else{                    //当前stack已满,且所有stack已满
                throw MyException("No Space left in all stacks!");
            }
        }
    }
    
    void SetOfStacks::pop(){
        if (currElemNum > 0){        //当前stack未空
            stacks[currStack].pop();
            currElemNum--;
        }
        else{                        //当前stack已空
            if (currStack > 0){        //当前stack已空,但所有stack未空
                currStack--;
                stacks[currStack].pop();
                currElemNum = threshold - 1;
            }
            else{                    //当前stack已空,且所有stack已空
                throw MyException("all stacks already empty!");
            }
        }
    }
    
    int SetOfStacks::top(){
        if (currElemNum > 0)        //当前stack未空
            return stacks[currStack].top();
        else{                        //当前stack已空
            if (currStack > 0)        //当前stack已空,但所有stack未空
                return stacks[currStack-1].top();
            else                    //当前stack已空,且所有stack已空
                throw MyException("all stacks already empty!");
        }
    }
    
    int main()
    {
        try{
            SetOfStacks ss(10);
            for (int i = 0; i < 100; i++){    //先push 100个元素进去
                ss.push(i);
            }
            cout<<"Push over!"<<endl;
    
            for (int i = 0; i < 100; i++){    //再pop 100个出来
                cout<<ss.top()<<endl;
                ss.pop();
            }
        }
        catch(MyException ex)
        {
            ex.PrintMessage();
        }
        return 0;
    }

    EOF

  • 相关阅读:
    STL 清除模板容器 clear.h
    建立ORACLE10G DATA GUARD---&gt;Physical Standby
    WWDC 2014 Session 205/217 Extension 注意事项
    Android 布局管理器
    软件测试的基本方法(五岁以下儿童)单元测试
    HDU 4896 Minimal Spanning Tree(矩阵高速功率)
    泛泰A860(高通公司8064 cpu 1080p) 拂4.4中国民营recovery TWRP2.7.1.2文本(通过刷第三版)
    Android L SDK -- 一些有趣的新功能
    Jquery在线咨询地址
    项目开发录制两个
  • 原文地址:https://www.cnblogs.com/lihaozy/p/2818242.html
Copyright © 2011-2022 走看看