⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 priorityqueuestack.java

📁 BOOK:Beginning Algorithms Code Examples
💻 JAVA
字号:
package com.wrox.algorithms.stacks;import com.wrox.algorithms.queues.EmptyQueueException;import com.wrox.algorithms.queues.HeapOrderedListPriorityQueue;import com.wrox.algorithms.sorting.Comparator;/** * A {@link Stack} that uses a priority queue. * */public class PriorityQueueStack extends HeapOrderedListPriorityQueue implements Stack {    /** The comparator to use. */    private final static Comparator COMPARATOR = new StackItemComparator();    /** the item counter */    private long _count = 0;    public PriorityQueueStack() {        super(COMPARATOR);    }    public void enqueue(Object value) {        super.enqueue(new StackItem(++_count, value));    }    public Object dequeue() throws EmptyQueueException {        return ((StackItem) super.dequeue()).getValue();    }    public void push(Object value) {        enqueue(value);    }    public Object pop() throws EmptyStackException {        try {            return dequeue();        } catch (EmptyQueueException e) {            throw new EmptyStackException();        }    }    public Object peek() throws EmptyStackException {        Object result = pop();        push(result);        return result;    }    private static final class StackItem {        private final long _key;        private final Object _value;        public StackItem(long key, Object value) {            _key = key;            _value = value;        }        public long getKey() {            return _key;        }        public Object getValue() {            return _value;        }    }    private static final class StackItemComparator implements Comparator {        public int compare(Object left, Object right) throws ClassCastException {            StackItem si1 = (StackItem) left;            StackItem si2 = (StackItem) right;            return (int) (si1.getKey() - si2.getKey());        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -