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 ...
随机推荐
- 【HDOJ】1341 Simple Computers
注意PC要与31. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXN 40 ...
- 【转】Android:控件Spinner实现下拉列表
原文网址:http://www.cnblogs.com/tinyphp/p/3858920.html 在Web开发中,HTML提供了下拉列表的实现,就是使用<select>元素实现一个下拉 ...
- SDL2.0教程翻译·目录
原文地址:SDL 2.0 Tutorial Index Welcome! 下面的教程旨在为你提供一个SDL2.0以及c++中游戏设计和相关概念的介绍.在本教程中,我们假定你对C++有一定程度上的知识, ...
- Codeforces - ZeptoLab Code Rush 2015 - D. Om Nom and Necklace:字符串
D. Om Nom and Necklace time limit per test 1 second memory limit per test 256 megabytes input standa ...
- 寻访上海西服定制店_Enjoy·雅趣频道_财新网
寻访上海西服定制店_Enjoy·雅趣频道_财新网 寻访上海西服定制店
- ORACLE_CLASS_ENDING
[JSU]LJDragon's Oracle course notes In the first semester, junior year Oracle考前复习 试题结构分析: 1.选择题2x10, ...
- SQL Server 各任务所维护
SQL Server 正在运行的代码查看 SELECT [Spid] = session_id , ecid , [Database] = DB_NAME(sp.dbid) , [User] = nt ...
- AngularJs学习笔记6——四大特性之依赖注入
压缩工具:YUI-compressor 为了优化网页功能,对一些脚本文件进行压缩,比如:删除所有的注释和空格等,简化形参.但是AngularJs模块中可以声明多种组件,如控制器.指令.过滤器.服务等. ...
- CodeForces 242E - XOR on Segment 二维线段树?
今天练习赛的题....又是线段树的变换..拿到题我就敲了个点更新区间查询的..果断超时...然后想到了可以将每个数与合表示成不进位的二进制数..这样就可以区间进行更新了..比赛的时候写搓了..刚重写了 ...
- Excel01-不同的单元格输入同一数据
第一步:按住Ctrl键,选择不同的单元格 第二步:选择完最后一个单元格后,输入需要的数据“YES”,按Ctrl+Enter键结束. 提示:按Ctrl+; 输入当前日期,再按ctrl+Enter实现全部 ...