Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Linked List Priority Queue
    text
    copied!<p>The ordered linked list implementation</p> <p>Here is the code I have written for some reason it won't compile and asks for a main function. I had used a main function to test this, but I need the program with out it.</p> <pre><code>package data_structures; import java.util.Iterator; import java.util.NoSuchElementException; public class ListPriorityQueue&lt;E&gt; implements PriorityQueue&lt;E&gt; { private Node head; private int size; private class Node { private E value; private Node next; public Node(E value, Node next) { this.value = value; this.next = next; } } private class NodeIter implements Iterator&lt;E&gt; { private E[] arr; private int n; public NodeIter() { n = size; arr = (E[]) new Object[n]; Node p = head; for (int i = 0; i &lt; n; ++i) { arr[i] = p.value; p = p.next; } } public boolean hasNext() { return n &gt; 0; } public E next() { if (n == 0) throw new NoSuchElementException(); return arr[--n]; } public void remove() { throw new UnsupportedOperationException(); } } public ListPriorityQueue() { this.head = null; this.size = 0; } public boolean insert(E object) { if (isFull()) return false; if (head == null) { head = new Node(object, null); } else if (((Comparable&lt;E&gt;)object).compareTo(head.value) &lt; 0) { head = new Node(object, head); } else { Node p = head; while (p.next != null &amp;&amp; ((Comparable&lt;E&gt;)object).compareTo(p.next.value) &gt;= 0) { p = p.next; //or equal to preserve FIFO on equal items } p.next = new Node(object, p.next); } ++size; return true; } public E remove() { if (isEmpty()) return null; E value = head.value; head = head.next; --size; return value; } public E peek() { if (isEmpty()) return null; return head.value; } public int size() { return size; } public boolean contains(E object) { Node p = head; while (p != null) { if (((Comparable&lt;E&gt;)object).compareTo(p.value) == 0) return true; p = p.next; } return false; } public Iterator&lt;E&gt; iterator() { if (isEmpty()) return null; return new NodeIter(); } public void clear() { head = null; size = 0; } public boolean isEmpty() { return size == 0; } public boolean isFull() { return size == DEFAULT_MAX_CAPACITY; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload