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

  1. class Stack_mine
  2. {
  3. // assume: the data length is not so long, its length <= the max number of int
  4. int * data ;
  5. int length;
  6. int * table;
  7.  
  8. public:
  9.  
  10. // constructor function
  11. Stack_mine()
  12. {
  13. length = 0;
  14. data = new int[LENGTH];
  15. table = new int[LENGTH];
  16. }
  17.  
  18. // function: pop
  19. int _pop()
  20. {
  21. if( ! _empty() )
  22. {
  23. length--;
  24. return data[length];
  25. }
  26. else
  27. {
  28. cout<<"pop error"<<endl;
  29. return -1;
  30. }
  31. }
  32.  
  33. // function: return length
  34. int _length( )
  35. {
  36. return length;
  37. }
  38.  
  39. // function: push
  40. void _push(int a)
  41. {
  42. int * p1,*p2;
  43. if(length >= LENGTH)
  44. {
  45. p1 = (int *)realloc(data,LONGLENGTH * sizeof(int));
  46. p2 = (int *)realloc(table,LONGLENGTH * sizeof(int));
  47. data = p1;
  48. table = p2;
  49. }
  50. data[length] = a;
  51. if( length == 0 || data[ table[length-1] ] < a )
  52. table[length] = length;
  53. else table[length] = table[length-1];
  54. length ++;
  55. }
  56.  
  57. // function: empty
  58. bool _empty()
  59. {
  60. if(length>0)
  61. return false;
  62. else return true;
  63. }
  64.  
  65. // function: max
  66. int _max()
  67. {
  68. if(! _empty())
  69. return data[ table[ length-1 ] ];
  70. cout<<"error: empty stack for _max"<<endl;
  71. return -1;
  72. }
  73. };

ok, my solution for queue max elem follows

  1. class Queue_mine
  2. {
  3. Stack_mine s1;
  4. Stack_mine s2;
  5. public:
  6. Queue_mine(){};
  7.  
  8. // function: push
  9. void _push(int a)
  10. {
  11. s1._push(a);
  12. };
  13.  
  14. // function: pop
  15. int _pop()
  16. {
  17. if(s2._empty())
  18. {
  19. while(!s1._empty())
  20. {
  21. s2._push(s1._pop());
  22. }
  23. }
  24. return s2._pop();
  25. }
  26.  
  27. // function: length
  28. int _length()
  29. {
  30. return s1._length() + s2._length();
  31. }
  32.  
  33. bool _empty()
  34. {
  35. if( s1._empty() && s2._empty() )
  36. {
  37. return true ;
  38. }
  39. else return false ;
  40. }
  41.  
  42. int _max()
  43. {
  44. if(! s1._empty() && ! s2._empty())
  45. return ( s1._max() > s2._max() ? s1._max() : s2._max() );
  46. else if( ! s1._empty() && s2._empty())
  47. return s1._max();
  48. else if( s1._empty() && ! s2._empty() )
  49. return s2._max();
  50. else {
  51. cout<<"empty for queue"<<endl;
  52. return -1;
  53. }
  54. }
  55.  
  56. };

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. 用C#调用Windows API向指定窗口发送按键消息 z

    用C#调用Windows API向指定窗口发送 一.调用Windows API. C#下调用Windows API方法如下: 1.引入命名空间:using System.Runtime.Interop ...

  2. linux查找超过一定时间的文件,并批量删除

    1.find . -maxdepth 4  -name "*-*" -mtime 3 -maxdepth的值决定是否对下面的子目录进行递归查找 -mtime 3表示查找刚好3天的: ...

  3. java中常量定义在interface中好还是定义在class中

    Java中interface中定义变量都是"public static final" 类型的, 也就是常量, 因此很多人在interface定义常用的常量,除此之外单独定义一个cl ...

  4. 纯CSS(无 JavaScript)实现的响应式图像显示

    有许多方法可以实现web页面里图像的应答.然而,我碰到的所有方案都使用了JavaScript.这使我疑惑不用JavaScript实现图像响应是否可行. 我提出了下面纯CSS的方案. 它是如何工作的呢? ...

  5. js执行eval()抛出异常SyntaxError

    try{ eval("("+data+")"); }catch(err) { location.href = window.location.href; }

  6. 详解vue父组件传递props异步数据到子组件的问题

    案例一 父组件parent.vue // asyncData为异步获取的数据,想传递给子组件使用 <template> <div> 父组件 <child :child-d ...

  7. [Unit Testing for Zombie] 06. Using Factory

    FACTORIES Convert the zombies fixture to a Factory Girl Factory called :zombie. test/fixture/zombies ...

  8. (剑指Offer)面试题8:旋转数组的最小数字

    题目: 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转 ...

  9. STL - 常用顺序容器代码

    不多说,看代码 #include <iostream> #include <vector> #include <deque> #include <list&g ...

  10. Colours

    A colour is an object representing a combination of Red, Green, and Blue (RGB) intensity values. Val ...