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

posted on 2012-12-14 16:14  kkmm  阅读(716)  评论(0编辑  收藏  举报