简体   繁体   中英

Priority Queue using Priority List Misses All the Middle Values in Java

I am having an issue building a PList based Priority queue. Essentially, if I add items, all it ever does when I run a main function is spit out the highest and the lowest priority values.

Here is my PQueue insert item method

public class PQueue {
PList pq;
PList top;


public PQueue(){
    this.pq=null;
}
public PQueue(int priority, char x){
this.pq=new PList(x, priority, null);
top=this.pq;
}

public boolean isEmpty(){
    if(this.pq==null){
        return true;
    }else{
        return false;
    }
}

public void insertItem(int priority, char x){
    if(isEmpty()){
        this.pq=new PList(x, priority);
        top=this.pq;            
    }else{
        PList p=top;
        while(p.priority<top.priority && p!=null){
            p=p.next;
        }PList n=new PList(x, priority, p.next);
        p.next=n;
        if(n.priority>top.priority){
            top=n;
        }
    }

}
public void show(PrintStream p){
    PList prnt= top;
    while(prnt!=null){
        p.println(prnt.content);
        prnt=prnt.next;
    }
}

}

Here is my linked list::

package se2s03;

public class PList {
public char content;
public int priority;
public PList next;

PList(final char a, final int b, final PList ll){
    this.content=a;
    this.priority=b;
    this.next=ll;
}
PList(final char a, final int b){
    this.content=a;
    this.priority=b;
}

}

Here's a priority queue implementation using a linked list.

public class PQueue
{
    private Node head;

    public boolean isEmpty()
    {
         return head == null;
    }

    public void insert(int priority, Object obj)
    {
         if (head == null)
             head = new Node(obj, priority);
         else {
             Node curr = head, prev = null;
             while (curr != null && curr.priority > priority) {
                  prev = curr;
                  curr = curr.next;
             }
             if (prev == null)
             head = new Node(obj, priority, head);
             else
             prev.next = new Node(obj, priority, curr);
         }
    }

    public Object remove()
    {
         if (head == null)
             return null;
         Object val = head.val;
         head = head.next;
         return val;
    }

    public Object peek()
    {
         if (head == null)
             return null;
         return head.val;
    }

    public String toString()
    {
         StringBuilder sb = new StringBuilder();
         sb.append("[");
         Node prnt = head;
         while (prnt != null) {
             sb.append(prnt.val.toString() + ", ");
             prnt = prnt.next;
         }
         return sb.substring(0, sb.length() - 2) + "]";
    }

    private class Node
    {
         Object val;
         int priority;
         Node next;

         Node(Object val, int priority)
         {
             this(val, priority, null);
         }

         Node(Object val, int priority, Node next)
         {
             this.val = val;
             this.priority = priority;
             this.next = next;
         }
    }

    public static void main(String[] args)
    {
         PQueue pq = new PQueue();
         for (char ch = 'a'; ch <= 'z'; ch++)
             pq.insert(ch - 'a', ch);
         System.out.println(pq);
         while (!pq.isEmpty())
             System.out.print(pq.remove() + ", ");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM