3.1 Describe how you could use a single array to implement three stacks.

 

这道题让我们用一个数组来实现三个栈,书上给了两种方法,第一种方法是定长分割 Fixed Division,就是每个栈的长度相同,用一个公用的一位数组buffer来保存三个栈的内容,前三分之一为第一个栈,中间三分之一为第二个栈,后三分之一为第三个栈,然后还要分别记录各个栈当前元素的个数,然后再分别实现栈的基本操作push, pop, top 和 empty,参见代码如下:

 

class ThreeStacks {
public:
    ThreeStacks(int size) : _stackSize(size) {
        _buffer.resize(size * 3, 0);
        _stackCur.resize(3, -1);
    }
    
    void push(int stackIdx, int val) {
        if (_stackCur[stackIdx] + 1 >= _stackSize) {
            cout << "Stack " << stackIdx << " is full!" << endl;
        }
        ++_stackCur[stackIdx];
        _buffer[stackIdx * _stackSize + _stackCur[stackIdx]] = val;
    }
    
    void pop(int stackIdx) {
        if (empty(stackIdx)) {
            cout << "Stack " << stackIdx << " is empty!" << endl;
        }
        _buffer[stackIdx * _stackSize + _stackCur[stackIdx]] = 0;
        --_stackCur[stackIdx];
    }
    
    int top(int stackIdx) {
        if (empty(stackIdx)) {
            cout << "Stack " << stackIdx << " is empty!" << endl;
        }
        return _buffer[stackIdx * _stackSize + _stackCur[stackIdx]];
    }
    
    bool empty(int stackIdx) {
        return _stackCur[stackIdx] == -1;
    }
    
private:
    int _stackSize;
    vector<int> _buffer;
    vector<int> _stackCur;
};

 

第二种方法是灵活分割 Flexible Divisions,这种方法较为复杂,这里就不细写了。

 

相关文章:

  • 2021-09-25
  • 2021-07-31
  • 2021-12-23
  • 2022-02-02
  • 2021-07-29
  • 2021-09-29
  • 2021-06-16
  • 2022-01-25
猜你喜欢
  • 2021-08-19
  • 2021-07-18
  • 2022-12-23
  • 2021-07-24
  • 2021-10-23
  • 2022-12-23
  • 2021-07-06
相关资源
相似解决方案