Java examples for Data Structure:queue
Implements a Priority Queue unordered that iterates over the smallest key.
import static java.lang.System.out; import java.util.NoSuchElementException; public class UnorderedMinPQ<Key extends Comparable<Key>> { private Key[] pq; private int N; /*from www. j a va2 s .c o m*/ public UnorderedMinPQ() { this(1); } @SuppressWarnings("unchecked") public UnorderedMinPQ(int capacity) { pq = (Key[]) new Comparable[capacity]; } public boolean isEmpty() { return N == 0; } public void insert(Key k) { if (N == pq.length - 1) resize(2 * pq.length); pq[N++] = k; } public Key delMin() { if (isEmpty()) throw new NoSuchElementException(); int min = 0; for (int i = 1; i < N; i++) if (less(i, min)) min = i; exch(min, N-1); Key k = pq[--N]; pq[N] = null; if ((N > 0) && (N == (pq.length - 1) / 4)) resize(pq.length / 2); return k; } private boolean less(int i, int j) { return pq[i].compareTo(pq[j]) < 0; } private void exch(int i, int j) { Key swap = pq[i]; pq[i] = pq[j]; pq[j] = swap; } private void resize(int capacity) { @SuppressWarnings("unchecked") Key[] copy = (Key[]) new Comparable[capacity]; for (int i = 0; i <= N; i++) copy[i] = pq[i]; pq = copy; } public static void main(String[] args) { UnorderedMinPQ<String> queue = new UnorderedMinPQ<String>(); queue.insert("P"); queue.insert("R"); queue.insert("I"); queue.insert("O"); queue.insert("R"); queue.insert("I"); queue.insert("T"); queue.insert("Y"); queue.insert("Q"); queue.insert("U"); queue.insert("E"); queue.insert("U"); queue.insert("E"); while (!queue.isEmpty()) { out.printf("%s ", queue.delMin()); } } }