queue for max elem, pop, push

个人信息:就读于燕大本科软件project专业 眼下大三;

本人博客:google搜索“cqs_2012”就可以;

个人爱好:酷爱数据结构和算法,希望将来从事算法工作为人民作出自己的贡献;

博客内容:queue for max elem, pop, push;

博客时间:2014-4-28;

编程语言:C++ ;

编程坏境:Windows 7 专业版 x64;

编程工具:vs2008 32位编译器;

制图工具:office 2010 ppt;

硬件信息:7G-3 笔记本;

my words

Don't let shorts beat you, because it doesn't worth.

problem

make a queue for max elem, pop and push.(problem from beauty from programming)

require fast for getting max elem

my solution

two stacks can make true a queue.

sub-problem

getting max elem for the stack is so easy with dp.

my solution for my stack with max elem follows

name of type for elem is int

class Stack_mine
{
// assume: the data length is not so long, its length <= the max number of int
int * data ;
int length;
int * table; public: // constructor function
Stack_mine()
{
length = 0;
data = new int[LENGTH];
table = new int[LENGTH];
} // function: pop
int _pop()
{
if( ! _empty() )
{
length--;
return data[length];
}
else
{
cout<<"pop error"<<endl;
return -1;
}
} // function: return length
int _length( )
{
return length;
} // function: push
void _push(int a)
{
int * p1,*p2;
if(length >= LENGTH)
{
p1 = (int *)realloc(data,LONGLENGTH * sizeof(int));
p2 = (int *)realloc(table,LONGLENGTH * sizeof(int));
data = p1;
table = p2;
}
data[length] = a;
if( length == 0 || data[ table[length-1] ] < a )
table[length] = length;
else table[length] = table[length-1];
length ++;
} // function: empty
bool _empty()
{
if(length>0)
return false;
else return true;
} // function: max
int _max()
{
if(! _empty())
return data[ table[ length-1 ] ];
cout<<"error: empty stack for _max"<<endl;
return -1;
}
};

ok, my solution for queue max elem follows

class Queue_mine
{
Stack_mine s1;
Stack_mine s2;
public:
Queue_mine(){}; // function: push
void _push(int a)
{
s1._push(a);
}; // function: pop
int _pop()
{
if(s2._empty())
{
while(!s1._empty())
{
s2._push(s1._pop());
}
}
return s2._pop();
} // function: length
int _length()
{
return s1._length() + s2._length();
} bool _empty()
{
if( s1._empty() && s2._empty() )
{
return true ;
}
else return false ;
} int _max()
{
if(! s1._empty() && ! s2._empty())
return ( s1._max() > s2._max() ? s1._max() : s2._max() );
else if( ! s1._empty() && s2._empty())
return s1._max();
else if( s1._empty() && ! s2._empty() )
return s2._max();
else {
cout<<"empty for queue"<<endl;
return -1;
}
} };

queue for max elem, pop, push的更多相关文章

  1. 小tip:关于typeof,instanceof,toString(),valueOf(),toLocaleString(),join(),reverse(),sort(),pop(),push(),shift(),unshift()

    typeof:用于检测一个变量是否是基本数据类型.instanceof用于检测某引用对象是什么类型的对象. var s = "Nicho"; var b = true; var n ...

  2. js 数组的pop(),push(),shift(),unshift()方法小结

    关于数组的一些操作方法小结: pop(),push(),shift(),unshift()四个方法都可改变数组的内容以及长度: 1.pop() :删除数组的最后一个元素,并返回被删除的这个元素的值: ...

  3. JS pop push unshift shift的作用与区别

    白话JS中数组方法pop push unshift shift的作用与区别,通过本文,你大概能知道这四种数组方法的基本使用与大致区别. 首先,这四种方法会直接修改数组,请先记住这一点. 我们先把pop ...

  4. 自定义栈类型,具有找到站内最小元素的min函数 ,且min(),pop(),push()函数的时间复杂度为O(1)

    基本思想: // 借助一个辅助栈,入栈时,若新元素比辅助栈栈顶元素小,则直接放入辅助站 // 反之,辅助站中放入次小元素(即辅助栈栈顶元素)====保证最小元素出栈时,次小元素被保存 static c ...

  5. JS中some()和every()和join()和concat()和pop(),push(),shift(),unshfit()和map()和filter()

    一.Array 1.some()和every() some()是对数组中每一项运行指定函数,如果该函数对任一项返回true,则返回true. every()是对数组中的每一项运行给定函数,如果该函数对 ...

  6. mongo 修改器 $inc/$set/$unset/$pop/$push/$pull/$addToSet

    mongo $inc 可以对集合里面的某些值是数字的增减.看代码 $set  可以进行修改,并且不存在的时候默认添加. 同时还能该变数据的类型. 还可以该变内嵌元素的值 用.调用 $unset  删除 ...

  7. js array filter pop push shift unshift方法

    JavaScript Array filter() 方法  JavaScript Array 对象 实例 返回数组 ages 中所有元素都大于 18 的元素: var ages = [32, 33,  ...

  8. <h2>js数组操作大全(pop,push,unshift,splice,shift方法)</h2>

    ---恢复内容开始--- shift:删除原数组第一项,并返回删除元素的值:如果数组为空则返回undefined var a = [1,2,3,4,5]; var b = a.shift(); //a ...

  9. 堆栈 pop push

    1.什么是堆栈 1.1堆栈 堆栈可以看作程序的心脏 所有重要的数据都会在这个里面体现(比如运算一道算术题,虽然还没算出最终答案,但是你在算出最终结果前的一些过程值可以放进堆栈) 堆栈这块内存比较特殊, ...

随机推荐

  1. Junit初级篇

    @Test介绍 @Test是我们在写测试脚本时最常用到的,大部分情况下如果没用这个注解,一个方法就不能成为测试用例.如下代码是一个最普通的测试脚本: import org.junit.Assert; ...

  2. jquery empty()方法

    empty() 方法从被选元素移除所有内容,包括所有文本和子节点. $(selector).empty() <html> <head> <script type=&quo ...

  3. Item 5:那些被C++默默地声明和调用的函数 Effective C++笔记

    Item 5: Know what functions C++ silently writes and calls 在C++中,编译器会自己主动生成一些你没有显式定义的函数,它们包含:构造函数.析构函 ...

  4. TensorFlow进阶(五)---图与会话

    图与会话 图 tf.Graph TensorFlow计算,表示为数据流图.一个图包含一组表示 tf.Operation计算单位的对象和tf.Tensor表示操作之间流动的数据单元的对象.默认Graph ...

  5. 改善你的jQuery的25个步骤

    1. 从Google Code加载jQueryGoogle Code上已经托管了多种JavaScript类库,从Google Code上加载jQuery比直接从你的服务器加载更有优势.它节省了你服务器 ...

  6. 检测任意日期字符串是否属于当天的java实现方案

    有时候我们会遇到很多形式的日期判断,甚至是并不常见的日期形式,比如20161212之类的日期,下面就此来进行代码是否处于当天的日期校验的代码实现来做一个整理. public static boolea ...

  7. python 3 爬取百度图片

    python 3 爬取百度图片 学习了:https://blog.csdn.net/X_JS612/article/details/78149627

  8. (转)AS3 中,Function.apply、call中第一个参数的作用;与什么时候用

    http://blog.csdn.net/linjf520/article/details/8746064 大家在使用Function.apply或是call时,是否发现,第一个参数不知道怎么用,赋值 ...

  9. IOS8 Playground介绍

    一.Playground介绍 Playground是Xcode6中自带的Swift代码开发环境.俗话说"功欲善其事,必先利其器".曾经在Xcode5中编写脚本代码,比如编写JS,其 ...

  10. python urllib2 httplib HTTPConnection

    httplib实现了HTTP和HTTPS的客户端协议,一般不直接使用,在python更高层的封装模块中(urllib,urllib2)使用了它的http实现. import httplib conn  ...