编程之美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
原创粉丝点击