package Examples; import java.util.*; import java.util.logging.Logger; import Sequenic.T3.SuiteUtils.Inference.Comment; public class PriorityQueue { private static class Node { Comparable value ; Node next = null ; Node(Comparable x) { value = x ; } Node linkto(Node n) { next = n ; return this ; } } private Node stack = null ; private int size = 0 ; private final static Comment comment = new Comment(Logger.getLogger(PriorityQueue.class.getName())) ; // public int size() { return stack.size() ; } private Node insert(Node n, Comparable x) { Node x_ = new Node(x) ; x_.linkto(n.next) ; n.next = x_ ; size++ ; return n ; } public Comparable put(Comparable x) { if (x == null) throw new IllegalArgumentException() ; comment.with("input ok").end() ; if (size == 0) { comment.with("found an empty stack").end(); ; stack = new Node(x) ; size++ ; return x ; } comment.with("singleton").If(size==1); if (stack.value.compareTo(x)>0) { comment.with("inserting at the head").end(); ; stack = new Node(x) . linkto(stack) ; size++ ; return x ; } Node p = stack ; while (true) { if (p.value.compareTo(x) == 0) { comment.with("x is already in the queue").end(); return null ; } if (p.next == null || p.next.value.compareTo(x) > 0) { comment.with("inserting x...").end(); insert(p,x) ; return x ; } p = p.next ; } } public Integer indexOf(Comparable x) { int toGo = size ; Node p = stack ; while (toGo>0) { if (p.value.compareTo(x) == 0) return size - toGo ; toGo-- ; p = p.next ; } return null ; } public static void main(String[] args) { PriorityQueue q = new PriorityQueue() ; q.put(1) ; q.put(10) ; q.put(3) ; q.put(1) ; q.put(3) ; q.put(10) ; q.put(-1) ; q.put(11) ; Node p = q.stack ; while (p != null) { System.out.println(">> " + p.value) ; p = p.next ; } System.out.println(">> size " + q.size) ; } }