基于有序链表的优先级队例

Posted ssdut_yrp

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于有序链表的优先级队例相关的知识,希望对你有一定的参考价值。

public class Link 
    public long lData;
    public Link next;//reference to next Link

    public Link(long lData)
        this.lData=lData;
    
    public void displayLink()
        System.out.print(lData+" ");
    
public class SortedList 
    private Link first;

    public SortedList()
        first =null;
    

    public boolean isEmpty()
        return (first==null);
    

    public void insert(long key)
        Link newLink = new Link(key);
        Link previous=null;
        Link current=first;
        while(current!=null&&key>current.lData)
            previous=current;
            current=current.next;
        
        if(previous==null)
            first=newLink;
        
        else
            previous.next=newLink;
        
        newLink.next=current;
    

    public Link remove()
        Link temp =first;
        first=first.next;
        return temp;
    

    public void displayList()
        Link current =first;
        while(current!=null)
            current.displayLink();
            current=current.next;
        
        System.out.println("");
    
/**
     * 优先级队列
     */
public class PriorityQ 
    private SortedList theSortedList;

    public PriorityQ()
        theSortedList=new SortedList();
    

    public void insert(long item)
        theSortedList.insert(item);
    

    public long remove()//remove minimum item
        return theSortedList.remove().lData;
    


    public boolean isEmpty()
        return theSortedList.isEmpty();
    

    public void displayQueue()
        theSortedList.displayList();
    
public class PriorityQApp 

    /**
     * @param args
     */
    public static void main(String[] args) 
        // TODO Auto-generated method stub
        PriorityQ thePQ = new PriorityQ();
        thePQ.insert(30);
        thePQ.insert(50);
        thePQ.insert(10);
        thePQ.insert(40);
        thePQ.insert(20);
        while(!thePQ.isEmpty())
            long item = thePQ.remove();
            System.out.print(item+" ");
        
    

以上是关于基于有序链表的优先级队例的主要内容,如果未能解决你的问题,请参考以下文章

经典算法——合并K个有序链表

基于链表的优先队列

队例queue

两个有序链表的合并

多个有序链表的合并

合并两个有序链表【递归、迭代】