I wanted to write a PriorityList queue, in which all Elements underlie an order. In my case I wanted to order elements of the Type ElementType (can be anything, for testing I used integers and ordered them according to their value, starting with the highest value and ending with the lowest) 

However I am at a loss at the moment finding the error in my following method. The code runs, but doesn't work as intended. 

Letting the code add 3 3 4 or 3 4 5 works nicely. I get 4 3 3 and 5 4 3, exactly as it should be.
However, if I give the queue the following values: 5 3 3 4 5 4, I get : 5 3 3; 
I would expect 5 5 4 4 3 3; 
I think it is understandable, what is wrong, however I don't understand pointers well enough to fix that issue. 

I would be very happy, if someone could look over the following code lines and try to figure out, what is wrong and how one could fix it. If necessary I can provide more information next morning.

```
public void enqueue(ElementType element) {
        ListNode elem = new ListNode ((ElementType)element);        
        if(head == null) {          
            head = elem;
            head.data = element;
            head.next = null;
            head.prev = null;
            tail = head;

        }
        else {  
            ListNode n = null;
            n = head;

            if(element.compareTo(head.data) > 0) {

                elem.prev = null;
                head.prev = elem; 
                elem.next = head;

                head.next = n.next; 
                head = elem;    return; 
            }
            int k = 0;  
            while(element.compareTo(n.data) <= 0 ) {
                k++;
                if(n.next !=null) {

                n = n.next; 

                 }

                else  {

                    elem.prev = n; 
                    n.next = elem;
                    tail = elem; 
                    elem.next = null;

                    return; }
            }   

            elem.prev = n.prev; 
            n.prev = elem; 
            elem.next = n; 

            } }

```