Question

CODE MUST NOT REFERENCE java.util.PriorityQueue

CODE MUST NOT REFERENCE java.util.PriorityQueue

CODE MUST BE COPY PASTE FRIENDLY AND SCREENSHOTS MUST BE INCLUDED SHOWING THAT IT WORKS.

6. Implement a priority queue using java.util.Stack as the base data type. Your code must not reference java.util.PriorityQue

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

Entry.java

package testpriorityqueue;

public class Entry
{
int k;
String v;
  
//constructor for Entry.
public Entry(int k, String v)
{
this.k = k;
this.v = v;
}
  
//toString() - to print the values in the desired format.
@Override
public String toString()
{
return "("+k+", \""+v+"\")";
}
}

Screenshots:

1 package testpriorityqueue; 2 3 public class Entry 4 5 int ki String vi 6 7 8 9 //constructor for Entry. public Entry (int k
PriorityQueue.java

package testpriorityqueue;

import java.util.Stack;

public class PriorityQueue {
//s1 - holds the priority queue.
private Stack<Entry> s1;
//s2 - supports s1 for achieving priority queue using stack.
private Stack<Entry> s2;
  
//constructor for PriorityQueue.
public PriorityQueue()
{
s1 = new Stack();
s2 = new Stack();
}
  
//while s1 is not empty,
//top of s1(stack)'s key is compared with the E's key.
//if the top value is smaller than E,
//top of the stack s1 is popped and added to stock s2.
//this repeats till either top of s1's key
//is greater than E's key or till s1 becomes empty.
//the E is added to s1.
//then all the entries in the stack s2 is popped and added to the stack s1.
public void insert(Entry E)
{
//while s1 is not empty,
while(!s1.empty())
{
//top of s1(stack)'s key is compared with the E's key.
if(s1.peek().k < E.k)
{
//if the top value is smaller than E,
//top of the stack s1 is popped and added to stock s2.
s2.add(s1.pop());
}
else
{
break;
}
}
//then E is added to s1.
s1.add(E);
//then all the entries in the stack s2 is popped and added to the stack s1.
while(!s2.empty())
{
s1.add(s2.pop());
}
}
  
public Entry removeMin()
{
//returns null is stack is empty, else returns s1's top entry.
return s1.empty()?null:s1.pop();
}

}
Screenshots:

1 package testpriorityqueue; 2 3 import java.util.Stack; 4 5 public class PriorityQueue { //31 - holds the priority queue. prwhile (!s1.empty()) 29 30 31 32 33 34 35 //top of 31 (stack) s key is compared with the Es key. if(31.peek().k < E.k) //if

TestPriorityQueue.java

package testpriorityqueue;
public class TestPriorityQueue {
public static void main(String[] args) {
//creating object for PriorityQueue.
PriorityQueue pq = new PriorityQueue();
//inserting entries.
pq.insert(new Entry(3, "test"));
pq.insert(new Entry(4, "every"));
pq.insert(new Entry(5, "edge"));
//removing entries.
System.out.println(pq.removeMin());
System.out.println(pq.removeMin());
System.out.println(pq.removeMin());
System.out.println("");
//inserting entries.
pq.insert(new Entry(10, "smoked"));
pq.insert(new Entry(5, "quality"));
pq.insert(new Entry(12, "beef"));
pq.insert(new Entry(1, "good"));
//removing entries.
System.out.println(pq.removeMin());
System.out.println(pq.removeMin());
System.out.println(pq.removeMin());
System.out.println(pq.removeMin());   
System.out.println("");
//inserting entries.
pq.insert(new Entry(9, "or"));
pq.insert(new Entry(3, "to"));
pq.insert(new Entry(5, "be"));
pq.insert(new Entry(10, "not"));
//removing entries.
System.out.println(pq.removeMin());
System.out.println(pq.removeMin());
System.out.println(pq.removeMin());
System.out.println(pq.removeMin());
}
}
Screenshots:

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 package testpriorityqueue; public class TestPriorityQueue { public static void

Sample I/O:

run: (3, test) (4, every) (5, edge) (1, good) (5, quality) (10, smoked) (12, beef) (3, to) (5, be) (9,or)

//Hope this Helps!

//Please consider giving this answer a thumbs up.

//If this answer didn't satisfies your requirements or needs modifications, please let me know in the comment section before giving a thumbs down.

//Thank you! Stay Safe!!!

Add a comment
Know the answer?
Add Answer to:
CODE MUST NOT REFERENCE java.util.PriorityQueue CODE MUST NOT REFERENCE java.util.PriorityQueue CODE MUST BE COPY PASTE FRIENDLY...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this assignment you will be implementing two Abstract Data Types (ADT) the Queue ADT and...

    In this assignment you will be implementing two Abstract Data Types (ADT) the Queue ADT and the Stack ADT. In addition, you will be using two different implementations for each ADT: Array Based Linked You will also be writing a driver to test your Queue and Stack implementations and you will be measuring the run times and memory use of each test case. You will also be adding some functionality to the TestTimes class that you created for Homework 1....

  • Write a C++ code based this question n this assignment you will create three additional functions...

    Write a C++ code based this question n this assignment you will create three additional functions for each one of the data structures created in class. You will be provided with a header file which you will modify and a demo program for each data structure. If your functions are implemented correctly the demo programs should compile and execute correctly. Part 0 (Comments) 1. Over the course of the semester we have discussed comments for each data structure. Please add...

  • I'm having trouble writing this code, can some help me? Step 1: Capturing the input The...

    I'm having trouble writing this code, can some help me? Step 1: Capturing the input The first step is to write a functionGetInput()that inputs the expression from the keyboard and returns the tokens---the operands and operators---in a queue. You must write this function. To make the input easier to process, the operands and operators will be separated by one or more spaces, and the expression will be followed by #. For example, here’s a valid input to your program: 6...

  • Programming Concepts cin/cout variables/types Functions Reference structs Task Summary: For this first Quest you I have...

    Programming Concepts cin/cout variables/types Functions Reference structs Task Summary: For this first Quest you I have set up a program in Visual Studio that will use operation between fractions. I have written a basic code example that does all the resources that you need to follow. Your job is to complete the exercises presented below in quest2.cpp. For this assignment you will learn and demonstrate how to: Work with functions use Past-by-Value and Past-by-Reference. use structs. use cin to get...

  • For Python 3.8! Copy and paste the following bolded code into IDLE: movies = [[1939, 'Gone...

    For Python 3.8! Copy and paste the following bolded code into IDLE: movies = [[1939, 'Gone with the Wind', 'drama'],           [1943, 'Casablanca', 'drama'],           [1965, 'The Sound of Music', 'musical'],           [1969, 'Midnight Cowboy', 'drama'],           [1972, 'The Godfather', 'drama'],           [1973, 'The Sting', 'comedy'],           [1977, 'Annie Hall', 'comedy'],           [1982, 'Ghandi', 'historical'],           [1986, 'Platoon', 'action'],           [1990, 'Dances with Wolves', 'western'],           [1992, 'Unforgiven', 'western'],           [1994, 'Forrest Gump', 'comedy'],           [1995, 'Braveheart', 'historical'],           [1997,...

  • Hi all, I'm studying for a test for my Java class that coming up on Tuesday,...

    Hi all, I'm studying for a test for my Java class that coming up on Tuesday, and I would love it if someone could look over my answer. Can someone give me some feedback on the wrong one I get wrong? I really need to get it right for the upcoming test. Here my prep test: Which one statement is true" Question1 All objects that are eligible for garbage collection will be removed from the heap by the garbage collector....

  • Need assistance with this problem. This is for a java class and using eclipse. For this...

    Need assistance with this problem. This is for a java class and using eclipse. For this assignment we’ll be doing problems 21.3 and 21.4 from your textbook. Problem 21.3 asks you to write a generic method that removes duplicate items from an ArrayList. This should be fairly straightforward. Problem 21.4 asks you to implement insertion sort within a generic method. Insertion sort is described in detail on pages 250-252 of your textbook. You can write your two methods in a...

  • ------------------------------------------------------------------------------------------------------------ CODE ALREADY HAVE BELOW--------------------------------------------------------------------------------------- public class LinkedQueue<T>

    ------------------------------------------------------------------------------------------------------------ CODE ALREADY HAVE BELOW--------------------------------------------------------------------------------------- public class LinkedQueue<T> implements QueueADT<T> {    private int count;    private LinearNode<T> head;    private LinearNode<T> tail;               public LinkedQueue()    {        count = 0;        head = null;        tail = null;    }    @Override    public void enqueue(T element)    {        LinearNode<T> node = new LinearNode<T> (element);        if(isEmpty())            head = node;        else           ...

  • Could somebody please help.! Please DON't copy paste from other places and please read the question...

    Could somebody please help.! Please DON't copy paste from other places and please read the question and answer exactly what's asked. Thank you I much appreciate it. You are to write a program name MyArrayStack.java that create/build the ArrayStack data structure. The class must be written to accept any type of Objects. The following must be implemented i.e. YOU must write the code (do not import any stack from the Java Library): 1. One default constructor that will create an...

  • JAVA LANG PLEASE: I have follwed these below guidelines but when i run my queue test...

    JAVA LANG PLEASE: I have follwed these below guidelines but when i run my queue test it is not executing but my stack is working fine, can you fix it please! MyQueue.java Implement a queue using the MyStack.java implementation as your data structure.  In other words, your instance variable to hold the queue items will be a MyStack class. enqueue(String item): inserts item into the queue dequeue(): returns and deletes the first element in the queue isEmpty(): returns true or false...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT