CCI_chapter 3 Stacks and Queues
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的更多相关文章
- 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 ...
- 612.1.003 ALGS4 | Stacks and Queues
Algorithm | Coursera - by Robert Sedgewick Type the code one by one! 不要拜读--只写最有感触的!而不是仅仅做一个笔记摘录员,那样毫 ...
- Stacks And Queues
栈和队列 大型填坑现场,第一部分的还没写,以上. 栈和队列是很基础的数据结构,前者后进先出,后者先进先出,如下图: 下面开始将客户端和具体实现分开,这样有两个好处:一是客户端不知道实现的细节,但同时也 ...
- uva 120 stacks of flapjacks ——yhx
Stacks of Flapjacks Background Stacks and Queues are often considered the bread and butter of data ...
- UVa120 - Stacks of Flapjacks
Time limit: 3.000 seconds限时:3.000秒 Background背景 Stacks and Queues are often considered the bread and ...
- Uva 120 - Stacks of Flapjacks(构造法)
UVA - 120 Stacks of Flapjacks Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld &a ...
- stacks and queues--codility
lesson 7: stacks and queues 1. Nesting 2. StoneWall 3. Brackets 4. Finsh lesson 7: stacks and queues ...
- Stacks of Flapjacks(栈)
Stacks of Flapjacks Background Stacks and Queues are often considered the bread and butter of data ...
- Stacks of Flapjacks
Stacks of Flapjacks Background Stacks and Queues are often considered the bread and butter of data s ...
随机推荐
- 2015第29周六Spring
搜了一下Spring相关的经典书籍: <Spring实战(第3版)>从核心的Spring.Spring应用程序的核心组件.Spring集成3个方面,由浅入深.由易到难地对Spring展开了 ...
- 判断UserAgent是否为手机
, ))) { return true; } return false; }
- Android UI 之TextView控件属性列表
在网上收集到了TextView 的属性,在开发过程中还是挺有用的. android:autoLink 设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接.可选值(non ...
- 简要介绍EF(实体框架)
原文地址:http://wenku.baidu.com/link?url=eutYH1QWA9y7fnxsxT9pZfJTPfa36nCI4R3Ub8Y4ybAVSgmXzEnXHwUj-GPFinn ...
- AOJ 0033 深度优先搜索
题意:按给定顺序从A口放标号位1-10的10个球,利用挡板可以使球落到B或C,问能否使B和C里的球标号从下往上递增. 分析:对于第 i 个球,若a[i]大于B口上方的球,则可放入B口:若a[i]大于C ...
- 浏览器开发 IE webkit chrome浏览器定制
FAQ:制作自己的浏览器有何意义?1.浏览器按照您的需求命名,可自定义LOGO,对您的产品或者公司都有推广作用.2.在帮助一栏的主页以及软件安装的网页链接都可以设为您网站的链接. 3.可将首页设置为您 ...
- Storm概念介绍
Storm核心概念如下: 1.Tuple:元组 Tuple即元组,是一个拓扑Topology中的Spout和Bolt组件之间数据传递的基本单元.元组中的字段可以是任何类 ...
- Mac OS X Shell 脚本和终端命令
系统 重启 Mac OS X: 1 shutdown - r now 关闭 Mac OS X: 1 shutdown now 电源管理/省电 获取当前电源管理设置的信息 1 pmset -g 设置显示 ...
- 博客换了,比较少用这个博客(http://loid.cf)
新博客地址: http://loid.cf/
- TTTAttributedLabel使用介绍(转)
TTTAttributedLabel 库地址 https://github.com/TTTAttributedLabel/TTTAttributedLabel 可以实现电话 地址 链接自动查找显示 ...