//Imagine a (literal) stack of plates. If the stack gets too high, it might topple. Therefore, 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. SetOfStacks 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 "mStack.h"
#include "MyStack.h"
struct stackPoint
{
MyStack *currentStack;
stackPoint *nextStackPoint;
}; class SetOfStacks
{
public:
SetOfStacks()
{
sp = new stackPoint;
sp->currentStack = new MyStack;
sp->nextStackPoint = NULL;
numOfStack = 1;
} bool push(int e)
{
if (sp->currentStack->isFull())
{ stackPoint *t = new stackPoint;
t->currentStack = new MyStack;
t->currentStack->push(e); t->nextStackPoint = sp;
sp = t;
numOfStack ++;
}
else
{
sp->currentStack->push(e);
}
return true;
} int pop()
{
if (sp->currentStack->isEmpty())
{
stackPoint *t =sp;
sp = sp->nextStackPoint;
delete t;
numOfStack --;
return sp->currentStack->pop();
}
else
{
return sp->currentStack->pop();
}
} int popAt(int index)
{
int ind = numOfStack - index;
if (ind <0)
{
return -9999;
} stackPoint *t = sp;
while(ind>0)
{
ind--;
t=t->nextStackPoint;
}
return t->currentStack->pop();
} int getNumOfStack()
{
return numOfStack;
} private:
stackPoint *sp;
int numOfStack;
}; int main()
{
SetOfStacks s; for (int i = 0;i<100;i++)
{
s.push(i+1);
}
cout<<"numOfStack "<<s.getNumOfStack();
cout<<endl;
for (int i=0;i<50; i++)
{
cout<<s.pop()<<endl;
}
cout<<"numOfStack "<<s.getNumOfStack()<<endl;
cout<<"PoPat "<<s.popAt(1)<<endl;
return 0;
}

上例子中,popAt()没有把pop位置后面的数据前移。用到的MyStack对stack简单封装了一下,如下,

#include <iostream>
#include <stack>
#define MAXSIZE 20
using namespace std;
class MyStack
{
public:
stack<int> mstack;
void push(int e)
{
mstack.push(e);
}
int pop()
{
if (!mstack.empty())
{
int k=mstack.top();
mstack.pop();
return k;
}
return -1; }
bool isFull()
{
if (mstack.size()>MAXSIZE-1)
{
return true;
}
else
return false;
}
bool isEmpty()
{
return mstack.empty();
}
};

Cracking The Coding Interview3.3的更多相关文章

  1. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  2. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  3. Cracking the coding interview--问题与解答

    http://www.hawstein.com/posts/ctci-solutions-contents.html 作者:Hawstein出处:http://hawstein.com/posts/c ...

  4. 《cracking the coding intreview》——链表

    前言 最近准备暑假回家回家修整一下,所以时间大部分用来完成项目上的工作,同时为了9月份的校招,晚上的时间我还在学习<cracking the coding intreview>,第二章链表 ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  6. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  7. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  8. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  9. 《Cracking the Coding Interview》——第13章:C和C++——题目6

    2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...

随机推荐

  1. pycharm设置连接github

    pycharm与guthub连起来,推送代码会方便一些 教程很多,转发一个:https://www.cnblogs.com/feixuelove1009/p/5955332.html

  2. C# winform 窗体怎么隐藏标题栏,不显示标题栏

    //没有标题            this.FormBorderStyle = FormBorderStyle.None;            //任务栏不显示            this.S ...

  3. Node版本管理nvm, npm

    nvm(node version manger) Node版本管理 nvm是一个简单的bash script来管理多个活动的node.js版本. 安装nvm: 具体看git:https://githu ...

  4. 蓝鲸DevOps深度解析系列(1):蓝盾平台总览

    ​​关注嘉为科技,获取运维新知 2018年10月,嘉为科技与腾讯云.蓝鲸智云携手,在北京.上海.广州.深圳举办 “研运一体,数据驱动,让运维走向运营”为主题的分享会,来自金融.电力.能源.制造等行业的 ...

  5. php字符串的拆分截取

    /* * strstr区分大小写 * stristr不区分大小写 * */ $str="test/abc.jpg"; echo stristr($str,'.'); echo '& ...

  6. 20165309 Linux安装及学习

    Linux安装及学习 安装虚拟机 结合娄老师的博客<基于VirtualBox虚拟机安装Ubuntu图文教程>和对一些小问题的百度,我也算是很顺利地完成了安装. 然后,按照步骤安装了虚拟机增 ...

  7. python记录_day16 类的成员

    一.变量 1.实例变量(又叫字段.属性) 创建对象时给对象赋值 形式: self.xxx = xxx 访问: 对象名.xxx     只能由对象访问 class Person: def __init_ ...

  8. xShell的简单操作记录

    查看java进程: ps ux | grep java 显示当前所有java进程pid的命令:jps 将压缩文件从本地上传到服务器上:rz 打包成jar包:mvn clean package -Dma ...

  9. 『CUDA C编程权威指南』第二章编程题选做

    第一题 设置线程块中线程数为1024效果优于设置为1023,且提升明显,不过原因未知,以后章节看看能不能回答. 第二题 参考文件sumArraysOnGPUtimer.cu,设置block=256,新 ...

  10. Oracle 数据库中查看表空间的2种方法

    在Oracle数据库中查看表空间使用状况是我们在实际应用中经常涉及到的,以下的内容就就是对Oracle 数据库中查看表空间使用状况时所要用到的SQL的描述,希望你能从中获得自己想要的东西. Oracl ...