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 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...
随机推荐
- Linux 各种软件的安装-mysql篇
作为一个长期混迹在windows圈的小白,当拿到一个新的linux服务器时,有点手足无措的赶脚.但是万事开头难嘛,Just Do It! 下面记录一下自己安装各种软件时遇到的坑.这一篇先讲mysql ...
- java ----> 基础之位运算
package test.ant; import java.util.Arrays; import java.io.UnsupportedEncodingException; public class ...
- C#特性-表达式树
表达式树ExpressionTree 表达式树基础 转载需注明出处:http://www.cnblogs.com/tianfan/ 刚接触LINQ的人往往觉得表达式树很不容易理解.通过这篇文章我希 ...
- 部署的error和排查,注意服务器内存占用!
mysqladmin: connect to server at 'localhost' failed error: 'Can't connect to local MySQL server thro ...
- Being a Good Boy in Spring Festival HDU - 1850
桌子上有M堆扑克牌:每堆牌的数量分别为Ni(i=1…M):两人轮流进行:每走一步可以任意选择一堆并取走其中的任意张牌:桌子上的扑克全部取光,则游戏结束:最后一次取牌的人为胜者. 现在我们不想研究到底先 ...
- 如何开发mis系统--整理
1.含义: 所谓MIS(管理信息系统--Management Information System)系统,主要指的是进行日常事务操作的系统.这种系统主要用于管理需要的记录,并对记录数据进行相关处理,将 ...
- zsh切换bash bash切换zsh
切换bash(需要sudo) chsh -s /bin/bash 切换zsh(不需要sudo) chsh -s /bin/zsh 注意:如果输入命令和密码后提示:no change made. 请加上 ...
- 转-MySQL教程-写的很详细,赞一个
原帖地址:https://www.w3cschool.cn/mysql/,谢谢原帖大人 MySQL是什么? MySQL安装 MySQL示例数据库 MySQL导入示例数据库 MySQL基础教程 MySQ ...
- ALV打印模板(存代码)
*&---------------------------------------------------------------------* *& Report ZMMF013 * ...
- SQL - 数据查询
数据查询是数据库的核心操作.SQL 提供了 select 语句进行数据查询,该语句的一般格式为: select [ ALL | distinct ] <目标列表达式> [ ,<目 ...