3.1Describe how you could use a single array to implement three stacks

for stack 1, we will use [0, n/3)
for stack 2, we will use [n/3, 2n/3)
for stack 3, we will use [2n/3, n)

const int stackSize = ;
int buffer = new int[stackSize * ];
int stackPointer[] = {,,}; //栈顶指针,指向下一可以放元素的位置 bool isEmpty(int stackNum){
assert(stackNum >= && stackNum <= );
return stackPointer[stackNum-] == ;
}
bool isFull(int stackNum){
assert(stackNum >= && stackNum <= );
return stackPointer[stackNum-] == stackSize ;
}
bool push(int stackNum, int value){
assert(stackNum >= && stackNum <= );
if(isFull(stackNum)) return false;
int index = (stackNum -) * stackSize + stackPointer[stackNum-] ;
buffer[index] = value;
stackPointer[stackNum-]++;
return true;
}
bool pop(int stackNum, int &value){
assert(stackNum >= && stackNum <= );
if(isEmpty(stackNum)) return false;
int index = (stackNum -) * stackSize + stackPointer[stackNum-] ;
value = buffer[index-];
stackPointer[stackNum-]--;
return true;
}
bool peek(int stackNum, int &value){
assert(stackNum >= && stackNum <= );
if(isEmpty(stackNum)) return false;
int index = (stackNum -) * stackSize + stackPointer[stackNum-] ;
value = buffer[index-];
return true;
}

3.2How would you design a stack which, in addition to push and pop, also has a function min which returns the minimum element? Push, pop and min should all operate in O(1) time

struct node{
int value;
int min;
node *up;
node(int data){
value = data;
min = data;
up = NULL;
}
}
struct stack()
{
node *top;
stack(){
top = NULL ;
}
}
void push(stack *, node *);
void pop(stack *);
node *peek(stack *);
int min(stack *);
stack *createS()
{
stack * myS = new stack;
return myS;
}
void push(stack * myS, node * myn){ if(NULL == myS || NULL == myn) return ;
if(NULL == myS->top){
myS->top = myn;
return ;
}else{
myn->up = myS->top;
myn->min = myn->value < myS->top->value ? myn->value : myS->top->value;
myS->top = myn;
}
}
void pop(stack * myS){
if(NULL == myS || myS->next == NULL ) return;
node *tp = myS->top;
myS->top = tp->up;
delete tp;
}
node * peek(stack * myS){
if(myS == NULL|| NULL == myS->top) return NULL;
return myS->top;
}
int min(stack * myS){
if(NULL == myS || myS->top== NULL) return INT_MAX ;
return myS->top->min;
}

CCI的第二种解法感觉有问题,如果进栈的元素有重复,就有可能有bug

3.3 3.4没什么意思

3.5  Implement a MyQueue class which implements a queue using two stacks

stack<int> s1;
stack<int> s2; int size(){
return s1.size();
}
int front(){
s1.top();
}
int push(int value){
while(!s1.empty()){
int temp = s1.top();
s2.push(temp);
s1.pop();
}
s1.push(value);
while( !s2.empty() ){
int temp = s2.top();
s1.push(temp);
s2,pop();
}
}
void pop(){
s1.pop();
}

3.6Write a program to sort a stack in ascending order You should not make any assumptions about how the stack is implemented The following are the only functions that should be used to write this program: push | pop | peek | isEmpty

/*
sorting can be performed with one more stack The idea is to pull an item from the original
stack and push it on the other stack If pushing this item would violate the sort order of the
new stack, we need to remove enough items from it so that it’s possible to push the new
item Since the items we removed are on the original stack, we’re back where we started The
algorithm is O(N^2) and appears below
*/
stack<int> sort(stack<int> mys){ if(mys.size() < ) return mys;
stack<int> tp;
while( !mys.empty()){
int value = mys.top();
mys.pop();
while( !tp.empty() && value < tp.top()){
int temp = tp.top();
mys.push(temp);
tp.pop();
}
tp.push(value);
}
return tp; }

CCI_chapter 3 Stacks and Queues的更多相关文章

  1. 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 ...

  2. 612.1.003 ALGS4 | Stacks and Queues

    Algorithm | Coursera - by Robert Sedgewick Type the code one by one! 不要拜读--只写最有感触的!而不是仅仅做一个笔记摘录员,那样毫 ...

  3. Stacks And Queues

    栈和队列 大型填坑现场,第一部分的还没写,以上. 栈和队列是很基础的数据结构,前者后进先出,后者先进先出,如下图: 下面开始将客户端和具体实现分开,这样有两个好处:一是客户端不知道实现的细节,但同时也 ...

  4. uva 120 stacks of flapjacks ——yhx

     Stacks of Flapjacks  Background Stacks and Queues are often considered the bread and butter of data ...

  5. UVa120 - Stacks of Flapjacks

    Time limit: 3.000 seconds限时:3.000秒 Background背景 Stacks and Queues are often considered the bread and ...

  6. Uva 120 - Stacks of Flapjacks(构造法)

    UVA - 120  Stacks of Flapjacks Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld &a ...

  7. stacks and queues--codility

    lesson 7: stacks and queues 1. Nesting 2. StoneWall 3. Brackets 4. Finsh lesson 7: stacks and queues ...

  8. Stacks of Flapjacks(栈)

     Stacks of Flapjacks  Background Stacks and Queues are often considered the bread and butter of data ...

  9. Stacks of Flapjacks

    Stacks of Flapjacks Background Stacks and Queues are often considered the bread and butter of data s ...

随机推荐

  1. Java多线程中变量的可见性

    之所以写这篇博客, 是因为在csdn上看到一个帖子问的就是这个问题. 废话不多说, 我们先看看他的代码(为了减少代码量, 我将创建线程并启动的部分修改为使用方法引用). 1 2 3 4 5 6 7 8 ...

  2. Java_XML操作

    xml的写: code: import java.io.File; import java.io.StringWriter; import javax.xml.parsers.DocumentBuil ...

  3. office2007序列号/密钥

    绝对可以用的许可证:V9MTG-3GX8P-D3Y4R-68BQ8-4Q8VD

  4. 读《MacTalk&#183;人生元编程》及Mac经常使用软件

    引子 池建强的Blog:http://www.cnblogs.com/chijianqiang/ 用了1年多的黑苹果,是用Windows的思维用UI.用Linux的思维用Shell,折腾的是联想E49 ...

  5. 二、Solr安装(Tomcat)

    安装环境 Windows 7 64bit Apache-tomcat-8.0.9-windows-x64 Solr-4.9.0 JDK 1.8.0_05 64bit 安装步骤 Tomcat和JDk的安 ...

  6. coco2d-html5制作弹弓射鸟第一部分---橡皮筋

    一.写在前面的话 最近在学习cocos2d-html5方面的知识,一直想从事游戏方面的工作,自己也找了好多资料.网上关于cocos2d-html5的教程真的不多,也只有自己摸索,慢慢看示例代码.由于本 ...

  7. Log4net日志组件使用

    一.简介 几乎所有的大型应用都会有自己的用于跟踪调试的API.因为一旦程序被部署以后,就不太可能再利用专门的调试工具了.然而一个管理员可能需要有一套强大的日志系统来诊断和修复配置上的问题.经验表明,日 ...

  8. 在C#、Java中,为什么不能用[返回值]区别重载方法?

    为什么方法签名只包含方法名和参数列表,而没有把返回值考虑进去? 如下有两个方法: void Func(){} string Func() { return string.Empty; } 编辑器可以根 ...

  9. Directory.GetFiles 方法

    Directory.GetFiles 方法 返回指定目录中文件的名称(包括其路径). 命名空间:   System.IO程序集:  mscorlib(mscorlib.dll 中) Enumerate ...

  10. Web ADF 编程步骤.

    从Web Controls 开始(工具来中的 ArcGIS Web Controls). 访问Resource Manager. 找到待访问的 Resource. 决定 Resource支持哪个 Fu ...