Cracking The Coding Interview3.3
//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的更多相关文章
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- Cracking the coding interview--问题与解答
http://www.hawstein.com/posts/ctci-solutions-contents.html 作者:Hawstein出处:http://hawstein.com/posts/c ...
- 《cracking the coding intreview》——链表
前言 最近准备暑假回家回家修整一下,所以时间大部分用来完成项目上的工作,同时为了9月份的校招,晚上的时间我还在学习<cracking the coding intreview>,第二章链表 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- 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 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目6
2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...
随机推荐
- 如何修改Sublime Text3 的侧边栏字体大小
如何修改Sublime Text3 的侧边栏字体大小 一.总结 一句话总结:用PackageResourceViewer插件来修改 PackageResourceViewer插件 1.如果Sublim ...
- GEO数据下载分析(SRA、SRR、GEM、SRX、SAMN、SRS、SRP、PRJNA全面解析)
很多时候我们需要从GEO(https://www.ncbi.nlm.nih.gov/geo/)下载RNA-seq数据,一个典型的下载页面是https://www.ncbi.nlm.nih.gov/ge ...
- 3.2 定位shellcode
前言 此帖为 0day_2th 一书第三章实践不完全记录. 流程记录 searchAddr.c 文件: #include <windows.h> #include <stdio.h& ...
- python 线程 进程
1.进程与线程优.缺点的比较总言:使用进程和线程的目的,提高执行效率. 进程: 优点:能利用机器的多核性能,同时进行多个操作. 缺点:需要耗费资源,重新开辟内存空间,耗内存. 线程: 优点:共享内存( ...
- 使用INTERSECT运算符
显示符合以下条件的雇员的雇员ID 和职务ID:这些雇员的当前职务与以前的职务相同,也就是说这些雇员曾担任过别的职务,但现在又重新担任了以前的同一职务. hr@TEST0924> SELECT e ...
- Hadoop---集群之MySQL的安装
Hadoop---集群之MySQL的安装 一: 二: 三: 四: 五: 六: 七: 八:修改数据库字符:解决中文乱码问题 ,mysql默认为latin1,我们要修改为utf-8 1> 2> ...
- 记一次搭建vsftp服务器坑
避免踩坑,特此记录... yum -y install vsftpd useradd -d /www -s /sbin/nologin sui # 修改vsftpd配置文件/etc/vsftpd/vs ...
- C#实体对象序列化成Json并让字段的首字母小写的两种解决方法
引言:最近在工作中遇到与某些API对接的post的数据需要将对象的字段首字母小写.解决办法有两种:第一种:使用对象的字段属性设置JsonProperty来实现(不推荐,因为需要手动的修改每个字段的属性 ...
- 【转】vue项目重构技术要点和总结
vue数据更新, 视图未更新 这个问题我们经常会遇到,一般是vue数据赋值的时候,vue数据变化了,但是视图没有更新.这个不算是项目重构的技术要点,也和大家分享一下vue2.0通常的解决方案吧! 解决 ...
- rsyslog的配置文件使用方法
参考地址: http://www.rsyslog.com/doc/v8-stable/configuration/property_replacer.html rsyslog消息模板的定义规则 &qu ...