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 ...
随机推荐
- MSSQL 标准PROC 写法
MSSQL 标准PROC 写法 ALTER PROC [dbo].[usp_ADM_InsertFlowSortInfo]@FlowSortName NVARCHAR(50),AS/*PAGE: 分类 ...
- guestfish 修改 image file
Example guestfish sessionSometimes, you must modify a virtual machine image to remove any traces of ...
- Java框架学习之Hibernate入门
Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库. Hibernate可以应用在任何使用JDB ...
- css样式-表格优化
1.表格的初步优化 index.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8&qu ...
- for 的多重循环--java
for的多重循环--java 利用for的多重循环打印出四种不同的三角形的图案. 图案如下: 4种不同三角形图案打印如下------------------******---------------- ...
- JavaScript基础知识(学习笔记)
1. 在JavaScript中所有事物都是对象:字符串.数字.数组.日期等等2. 在JavaScript中,对象是拥有属性和方法的数据3. 属性是与对象相关的值,方法是能够在对象上执 ...
- Thinking In Web [原创作品]
(转载请注明:http://zhutty.cnblogs.com, 交流请加群:164858883) 可能在大部分人来讲,前端就是可见的页面数据呈现正确就行.然而这样是不正确的,页面呈现是一部分,更多 ...
- java.util.Map.Entry接口
java.util.Map.Entry接口主要就是在遍历map的时候用到,给你个例子:package test;import java.util.*;import java.util.Map.Entr ...
- android与javascript相互调用
下面这一节来介绍android和javascript是怎么相互调用的,这样我们的UI界面设计起来就简单多了,而且UI设计起来也可以跨平台.现在有好多web app前台框架了,比如sencha和jque ...
- hdu 4605-Magic Ball Game(树状数组)
题目大意: 给你一棵二叉树,每个节点有一个w值,现在有一颗小球,值为x,从根节点往下掉,如果w==x,那么它就会停止:如果w>x,那么它往左.右儿子的概率都是1.2:如果w<x,那么它往左 ...