zoukankan      html  css  js  c++  java
  • [CareerCup] 3.1 Implement Three Stacks using Array 使用数组来实现三个栈

    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,这种方法较为复杂,这里就不细写了。

  • 相关阅读:
    第五课 按键控制文本
    第四课:怎么去掉Activity的标题和邮件图标-20160705
    第三课:控件的使用及按键响应-20160705
    第二课:Android Studo 各文件作用-20160705
    第一课:如何创建Android Studo 工程-20160705
    JTAG各类接口针脚定义及含义
    c# 定时器的实现
    二进制及十进制文件的读写方法
    c# 检测设备改变
    Vue(四)事件和属性
  • 原文地址:https://www.cnblogs.com/grandyang/p/4675640.html
Copyright © 2011-2022 走看看