0

Serious help needed. I need to make a stack the base data type and use that to implement the priority queue. It must also include an insert method and a removeMin method. Thanks, any help is appreciated

'

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);
        }
    }
}
  • Can you please clarify you required priority queue min/max so it will return min/max value.Stack FIFO.so why do you need to mix these ? – gati sahu May 20 '17 at 17:58
  • When you want to add another element to the stack use `Collections.binarySearch` to determine where to insert it. That way you can be sure that the elements in the stack are always sorted. – Tesseract May 20 '17 at 18:20
  • Im not sure why im required to mix these, Its just part of a project. the fact that queue is LIFO is why im confused and at a lost on how to do this. – Jason Herron May 20 '17 at 18:21
  • Can you use all the methods in `java.util.Stack` or are you limited to `push` and `pop`? – Tesseract May 20 '17 at 18:24
  • Yes I can use java.util.Stack – Jason Herron May 20 '17 at 18:33
  • The answer to https://stackoverflow.com/questions/685060/design-a-stack-such-that-getminimum-should-be-o1 will be useful. – Jim Mischel May 22 '17 at 13:43

0 Answers0