使用Stack作为基本数据类型实现优先级队列

时间:2017-05-20 17:48:57

标签: java stack queue priority-queue

需要认真的帮助。我需要将堆栈作为基本数据类型并使用它来实现优先级队列。它还必须包含insert方法和removeMin方法。谢谢,任何帮助表示赞赏

'

import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Stack

public class Problem2 {
    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);

        PriorityQueue<Entry> pq = new PriorityQueue<Entry>();

        for (int i = 0; i<5; i++)
        {
            int a = scn.nextInt();
            String b = scn.next();
            pq.add(new Entry(a, b));
        }
        System.out.println("The output from the priority queue:");

        for(int i = 0, c = pq.size(); i<c; i++)
        {
            System.out.println(pq.remove());
        }
    }

    static class Entry implements Comparable<Entry> {
        int k;
        String v;

        public Entry() 
        {
        }
        public Entry(int key, String value) {
            k = key;
            v = value;
        }
        public String toString() {
            return ("Key " + k + " value " + v);
        }
        public int compareTo(Entry b) {
            return Integer.compare(this.k, b.k);
        }
    }
}

0 个答案:

没有答案