编程之美3.7队列中取最大值操作问题Java版
来源:互联网 发布:http网络协议 编辑:程序博客网 时间:2024/06/03 05:06
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */package chart_3;/** * * 3.7 队列中取最大值操作问题 */public class QueueMaxOperate_3_7 { public static void main(String[] args) { //定义一个Stack类 Stack s = new Stack(5); s.push(12); s.push(3); s.push(5); s.push(9); s.push(6); s.push(36); System.out.println(s.Max()); s.Pop(); System.out.println(s.Max()); //定义一个Queue_T类,用Stack实现 Queue_T q = new Queue_T(); q.EnQueue(3); q.EnQueue(4); q.EnQueue(2); System.out.println(q.Max()); q.DeQueue(); q.EnQueue(5); System.out.println(q.Max()); } private static class Stack { private static int maxStackItemIndex; private static int stackTop; private static int[] link2NextMaxItenm; private static int[] stackItem; private static int maxn; public Stack(int maxn) { stackTop = -1; maxStackItemIndex = -1; link2NextMaxItenm = new int[maxn]; stackItem = new int[maxn]; this.maxn = maxn; } void push(int x) { if (stackTop >= maxn-1) { } else { stackTop++; stackItem[stackTop] = x; if (x > Max()) { link2NextMaxItenm[stackTop] = maxStackItemIndex; maxStackItemIndex = stackTop; } else { link2NextMaxItenm[stackTop] = -1; } } } int Pop(){ int ret; if(stackTop <0){ return -1; }else{ ret = stackItem[stackTop]; if(stackTop == maxStackItemIndex){ maxStackItemIndex = link2NextMaxItenm[stackTop]; } stackTop--; return ret; } } int Max() { if (maxStackItemIndex >= 0) { return stackItem[maxStackItemIndex]; } else { return -1; } } } }
Queue_T类
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */package chart_3;import java.util.Enumeration;import java.util.Stack;/** * * @author Administrator */class Queue_T { private Stack<Integer> stackA; private Stack<Integer> stackB; private int max_index; public Queue_T() { stackA = new Stack<Integer>(); stackB = new Stack<Integer>(); max_index = -1; } void EnQueue(int v) { stackB.push(v); if (v > max_index) { max_index = v; } } Integer DeQueue() { if (stackA.isEmpty()) { while (!stackB.isEmpty()) { stackA.push(stackB.pop()); } } int peek = stackA.pop(); if (peek == max_index) { max_index = LookMax(); } return peek; } int Max(){ return max_index; } int LookMax() { int maxA = -1; int maxB = -1; Enumeration<Integer> itemsA = stackA.elements(); while (itemsA.hasMoreElements()) { if (itemsA.nextElement() > maxA) { maxA = itemsA.nextElement(); } } if (!stackB.isEmpty()) { Enumeration<Integer> itemsB = stackB.elements(); while (itemsB.hasMoreElements()) { if (itemsB.nextElement() > maxB) { maxB = itemsB.nextElement(); } } } if(maxA > maxB) return maxA; else return maxB; }}
0 0
- 编程之美3.7队列中取最大值操作问题Java版
- 编程之美3.7--队列中取最大值操作问题
- 3.7-编程之美-队列中取最大值操作问题
- 编程之美--3.7 队列中取最大值操作问题
- 编程之美-队列中取最大值操作问题
- 编程之美—队列中取最大值操作问题
- 编程之美-队列中取最大值操作问题
- 编程之美读书笔记-队列中取最大值操作问题
- 编程之美3.7队列中取最大值操作
- 读书笔记之编程之美 - 3.7 队列中取最大值操作问题
- 编程之美:第三章 结构之法 3.7队列中取最大值操作问题
- 编程之美读书笔记3.7队列中取最大值操作的问题 解法2
- 编程之美3.7——队列中取最大值操作问题
- 编程之美3.7——队列中取最大值操作问题
- 编程之美3.7——队列中取最大值操作问题
- 编程之美之队列中取最大值操作
- 编程之美——队列中取最大值操作问题
- [编程之美] PSet3.7 队列中取最大值操作问题
- centos7 设置本地光盘yum源
- poj 2151 Check the difficulty of problems(概率dp)
- 数据存储方式
- 手把手教你提高英语能力
- java编程思想 bruce Eckel chapter10 内部类
- 编程之美3.7队列中取最大值操作问题Java版
- 数据库分库分表
- 【Android联系人】仿小米联系人(MIUI8)
- MySQL预处理 mysql_stmt_execute()
- 剑指Offer: 链表中环的入口结点
- 在Nodemcu开发板上运行MicroPython 1.8.2
- java代码模型6(异常处理格式)
- iOS 基本功能都有的音乐播放器 AVAudioPlayer
- 基于七牛和fresco的一整套安卓图片解决方案