首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

栈兑现队列(维护最大值)

2012-12-22 
栈实现队列(维护最大值)package alogrithmpublic class MyQueue {private MyStack stackAprivate MyStac

栈实现队列(维护最大值)

package alogrithm;public class MyQueue {private MyStack stackA;private MyStack stackB;public MyQueue(){stackA = new MyStack();stackB = new MyStack();}public int maxValue(int x,int y){return x>y?x:y;}public int max(){return maxValue(stackA.max(), stackB.max());}public void enQueue(int x){stackA.push(x);}public int Dequeue(){if(stackA.isEmpty()){while(!stackB.isEmpty())stackA.push(stackB.pop());}return stackA.pop();}}

热点排行