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. BZOJ 1018 [SHOI2008]堵塞的交通traffic

    1018: [SHOI2008]堵塞的交通traffic Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 2247  Solved: 706[Submit ...

  2. JavaScript方法undefined/null原因探究及闭包简单实现

    昨天一个刚写前端不久的同学发消息问这个问题(如下图): HTML代码为(省略部分代码): <head> <script src="test.js">< ...

  3. JS浏览器对象-Location对象

    1.返回web主机的域名 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

  4. 解决Qt5.7.0 cannot find -lGL

    很久没用Qt了,这次要做一个协议编辑器,在ubuntu 14.04上安装了最新版本的Qt 5.7.0.界面改用扁平化风格,第一感觉还不错.按默认步骤创建了一个gui程序,编译运行,报了一个错:cann ...

  5. (转)How to renew your Apple Push Notification Push SSL Certificate

    转自:https://blog.serverdensity.com/how-to-renew-your-apple-push-notification-push-ssl-certificate/ It ...

  6. acd LCM Challenge(求1~n的随意三个数的最大公倍数)

    Problem Description Some days ago, I learned the concept of LCM (least common multiple). I've played ...

  7. Android的Recovery中font_10x10.h字库文件制作

    任务是要汉化Android中的Recovery,就了解了bootable/recovery/minui/font_10x18.h这个英文字库的来历,最终汉化的时候并没有自己汉字字库,用的github上 ...

  8. Socket tips: UDP Echo service - Server code

    #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/soc ...

  9. Eclipse中如何安装和使用GrepCode插件

    GrepCode(GC)Eclipse插件允许Eclipse用户在Eclipse IDE中搜索由GrepCode提供的工厂类.本教程介绍如何安装和使用插件.使用Eclipse3.5(Galileo)的 ...

  10. getClass 与getSimpleName

    //首先定义一个借口 package com.test; public interface Fruit { } //定义一个实现类 package com.test; public class App ...