//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. HeadFirst Ruby 第十四章总结 Web apps: Serving HTML

    前言 这一章节主要讲了如何利用 Ruby 中的 Sinatra 这个 gem 来创建一个 Web app 的具体流程,其中的要点包括了: Sinatra, a third party library ...

  2. navicat和 plsql 连接oracle数据库 总结

    打开 navicat  -->工具-->选项-->oci   右侧选择oci.dll 的路径 默认 在 navicat的安装目录下有一个 instantclient 的文件夹 直接选 ...

  3. apicloud 聊天输入框模块UIChatBox

    点击链接查看详情 https://docs.apicloud.com/Client-API/UI-Layout/UIChatBox 模板中包括,聊天输入框,表情,发送图片,还有拍照,录音,其中也可以放 ...

  4. 4.1.6 Grundy数-硬币游戏2

    Problem Description: Alice 和 Bob 在玩一个游戏.给定 k 个数字 a1,a2,……,ak.一开始,有n堆硬币,每堆各有 Xi 枚硬币.Alice 和 Bob 轮流选出一 ...

  5. 网络基础之 tcp/ip五层协议 socket

    1 网络通信协议(互联网协议) 1.1 互联网的本质就是一系列的网络协议 1.2 osi七层协议 1.3 tcp/ip五层模型讲解 1.3.1 物理层 1.3.2 数据链路层 1.3.3 网络层 1. ...

  6. function_exists

    在已经定义的函数列表(包括系统自带的函数和用户自定义的函数)中查找 function_name. 如果 function_name 存在且的确是一个函数就返回 TRUE ,反之则返回 FALSE .

  7. mysql并行执行--缩短主从同步时延

    https://www.w3cschool.cn/architectroad/architectroad-mysql-parallel-copy.html 三.结尾 从mysql并行复制缩短主从同步时 ...

  8. 4月18 数据库的CRUD操作

    php主要是实现B/S Brower Server;此外还有C/S:Client Server暂时不考虑: LAMP: Linux系统 A阿帕奇服务器 Mysql数据库 Php语言,而现在学的是在wi ...

  9. json字符串转Map、json数组

    json数组转map public static void main(String[] args){ String strArr = "[{\"0\":\"zh ...

  10. 数据结构与算法之PHP查找算法(哈希查找)

    一.哈希查找的定义 提起哈希,我第一印象就是PHP里的关联数组,它是由一组key/value的键值对组成的集合,应用了散列技术. 哈希表的定义如下: 哈希表(Hash table,也叫散列表),是根据 ...