queue for max elem, pop, push
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的更多相关文章
- 小tip:关于typeof,instanceof,toString(),valueOf(),toLocaleString(),join(),reverse(),sort(),pop(),push(),shift(),unshift()
typeof:用于检测一个变量是否是基本数据类型.instanceof用于检测某引用对象是什么类型的对象. var s = "Nicho"; var b = true; var n ...
- js 数组的pop(),push(),shift(),unshift()方法小结
关于数组的一些操作方法小结: pop(),push(),shift(),unshift()四个方法都可改变数组的内容以及长度: 1.pop() :删除数组的最后一个元素,并返回被删除的这个元素的值: ...
- JS pop push unshift shift的作用与区别
白话JS中数组方法pop push unshift shift的作用与区别,通过本文,你大概能知道这四种数组方法的基本使用与大致区别. 首先,这四种方法会直接修改数组,请先记住这一点. 我们先把pop ...
- 自定义栈类型,具有找到站内最小元素的min函数 ,且min(),pop(),push()函数的时间复杂度为O(1)
基本思想: // 借助一个辅助栈,入栈时,若新元素比辅助栈栈顶元素小,则直接放入辅助站 // 反之,辅助站中放入次小元素(即辅助栈栈顶元素)====保证最小元素出栈时,次小元素被保存 static c ...
- JS中some()和every()和join()和concat()和pop(),push(),shift(),unshfit()和map()和filter()
一.Array 1.some()和every() some()是对数组中每一项运行指定函数,如果该函数对任一项返回true,则返回true. every()是对数组中的每一项运行给定函数,如果该函数对 ...
- mongo 修改器 $inc/$set/$unset/$pop/$push/$pull/$addToSet
mongo $inc 可以对集合里面的某些值是数字的增减.看代码 $set 可以进行修改,并且不存在的时候默认添加. 同时还能该变数据的类型. 还可以该变内嵌元素的值 用.调用 $unset 删除 ...
- js array filter pop push shift unshift方法
JavaScript Array filter() 方法 JavaScript Array 对象 实例 返回数组 ages 中所有元素都大于 18 的元素: var ages = [32, 33, ...
- <h2>js数组操作大全(pop,push,unshift,splice,shift方法)</h2>
---恢复内容开始--- shift:删除原数组第一项,并返回删除元素的值:如果数组为空则返回undefined var a = [1,2,3,4,5]; var b = a.shift(); //a ...
- 堆栈 pop push
1.什么是堆栈 1.1堆栈 堆栈可以看作程序的心脏 所有重要的数据都会在这个里面体现(比如运算一道算术题,虽然还没算出最终答案,但是你在算出最终结果前的一些过程值可以放进堆栈) 堆栈这块内存比较特殊, ...
随机推荐
- 用C#调用Windows API向指定窗口发送按键消息 z
用C#调用Windows API向指定窗口发送 一.调用Windows API. C#下调用Windows API方法如下: 1.引入命名空间:using System.Runtime.Interop ...
- linux查找超过一定时间的文件,并批量删除
1.find . -maxdepth 4 -name "*-*" -mtime 3 -maxdepth的值决定是否对下面的子目录进行递归查找 -mtime 3表示查找刚好3天的: ...
- java中常量定义在interface中好还是定义在class中
Java中interface中定义变量都是"public static final" 类型的, 也就是常量, 因此很多人在interface定义常用的常量,除此之外单独定义一个cl ...
- 纯CSS(无 JavaScript)实现的响应式图像显示
有许多方法可以实现web页面里图像的应答.然而,我碰到的所有方案都使用了JavaScript.这使我疑惑不用JavaScript实现图像响应是否可行. 我提出了下面纯CSS的方案. 它是如何工作的呢? ...
- js执行eval()抛出异常SyntaxError
try{ eval("("+data+")"); }catch(err) { location.href = window.location.href; }
- 详解vue父组件传递props异步数据到子组件的问题
案例一 父组件parent.vue // asyncData为异步获取的数据,想传递给子组件使用 <template> <div> 父组件 <child :child-d ...
- [Unit Testing for Zombie] 06. Using Factory
FACTORIES Convert the zombies fixture to a Factory Girl Factory called :zombie. test/fixture/zombies ...
- (剑指Offer)面试题8:旋转数组的最小数字
题目: 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转 ...
- STL - 常用顺序容器代码
不多说,看代码 #include <iostream> #include <vector> #include <deque> #include <list&g ...
- Colours
A colour is an object representing a combination of Red, Green, and Blue (RGB) intensity values. Val ...