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 * , );
_stackCur.resize(, -);
} void push(int stackIdx, int val) {
if (_stackCur[stackIdx] + >= _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]] = ;
--_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] == -;
} private:
int _stackSize;
vector<int> _buffer;
vector<int> _stackCur;
};

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

[CareerCup] 3.1 Implement Three Stacks using Array 使用数组来实现三个栈的更多相关文章

  1. golang之 Array(数组)

    目录 一.Array(数组) 二.数组的定义 1. 基本语法 三.数组的初始化 1. 方式一 2. 方式二 3. 方式三 四.数组的遍历 1. 方式一:for循环遍历 2. 方式二:for range ...

  2. vector以及array和数组

    //比较数组.vector.array #include <iostream> #include <vector> #include <array> #includ ...

  3. Array(数组)对象-->概念和创建

    1.什么是数组? 数组对象是使用单独的变量名来存储一系列的值. 2.数组创建的三种方法: 方法1:常规方式 var arr=new Array(); arr[0]="lisa"; ...

  4. java中Array(数组)的用法

    8.Array(数组)    数组是作为对象来实现的.(really occupy the memopry,真实的占用内存 ) An array is a data structure that st ...

  5. [CareerCup] 3.5 Implement Queue using Two Stacks 使用两个栈来实现队列

    3.5 Implement a MyQueue class which implements a queue using two stacks. LeetCode上的原题,请参见我之前的博客Imple ...

  6. [CareerCup] 17.6 Sort Array 排列数组

    17.6 Given an array of integers, write a method to find indices m and n such that if you sorted elem ...

  7. [CareerCup] 3.3 Set of Stacks 多个栈

    3.3 Imagine a (literal) stack of plates. If the stack gets too high, it might topple. Therefore, in ...

  8. [CareerCup] 7.4 Implement Multiply Subtract and Divide 实现乘法减法和除法

    7.4 Write methods to implement the multiply, subtract, and divide operations for integers. Use only ...

  9. [CareerCup] 8.1 Implement Blackjack 实现21点纸牌

    8.1 Design the data structures for a generic deck of cards. Explain how you would subclass the data ...

随机推荐

  1. abs()函数的返回值问题

    转载原文地址:http://www.cnblogs.com/webary/p/4967868.html 在牛客网看到一道关于abs()函数返回值的题目,见下图,当时还没反应过来,第一反应是:自从我开始 ...

  2. Invisible or disabled control cannot be activated

    在WPF 应用程序下出现:Invisible or disabled control cannot be activated(不见的或禁用的控件不能被激活)错误. System.ArgumentExc ...

  3. 最小安装centos 7 无GUI静默安装 oracle 12c,打造轻量linux化服务器

    CentOS 7 下载地址:http://mirrors.opencas.cn/centos/7/isos/x86_64/CentOS-7-x86_64-Everything-1511.iso 一.安 ...

  4. Web Service中的几个重要术语

    WSDL:web service definition language 直译:WebService定义语言 1.对应一种该类型的文件.WSDL 2.定义了Web Service的服务器与客户端应用交 ...

  5. cocos2d-x之文件读写

    bool HelloWorld::init() { if ( !Layer::init() ) { return false; } auto fu=FileUtils::getInstance(); ...

  6. Linux find/grep命令

    一.Find 1)批量删除文件 find . -name "*.h~" -exec rm '{}' \; 2)定位文件某一行 find / -name "demo.con ...

  7. linux alarm函数解除read write等函数的阻塞

    看到apue的第十章,说到alarm,pause可以实现sleep,可以让某些一直阻塞的函数超时,例如read,write.代码如下: static void sig_alrm(int signo) ...

  8. Centos7 搭建hadoop2.6 HA

    用户配置: User :root Password:toor 2.创建新用户 student Pwd: student 3.安装virtualbox的增强工具软件 4.系统默认安装的是openjdk ...

  9. 【MVC 4】8.SportsSore:管理

    作者:[美]Adam Freeman      来源:<精通ASP.NET MVC 4> 本文将继续构建 SportsStore 应用程序,为网站管理员提供一个管理产品分类的方法.本文将添 ...

  10. AC日记——搞笑世界杯 codevs 1060(dp)

    题目描述 Description 随着世界杯小组赛的结束,法国,阿根廷等世界强队都纷纷被淘汰,让人心痛不已. 于是有 人组织了一场搞笑世界杯,将这些被淘汰的强队重新组织起来和世界杯一同比赛.你和你的朋 ...