Question

Question 3: 10pts. Write code that will display the entire contents of the above Priority Queue.

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

package data_structures_java ;
import java.util.Iterator;
import java.util.PriorityQueue ;
import java.util.* ;
public class Queue_implementation {

PriorityQueue<Integer> actual_queue ;

public Queue_implementation(){
actual_queue = new PriorityQueue<Integer>() ;

}

public void add(int num){
actual_queue.add(num) ;
}

public int remove(){
return actual_queue.remove() ;
}

public int peek(){
if( actual_queue.isEmpty()) return -1 ;
else return actual_queue.peek() ;
}

public int element(){
return actual_queue.element() ;
}

public void print_queue(){
PriorityQueue<Integer>copy = new PriorityQueue<Integer>();
copy.addAll(actual_queue) ;
Iterator<Integer> through = actual_queue.iterator() ;
while(through.hasNext() ) {
System.out.print(through.next() + " ") ;
}
System.out.println() ;

actual_queue.addAll(copy) ;

}
public static void main(String[] args) {
Queue_implementation x = new Queue_implementation() ;
x.add(10) ;
x.add(9) ;
x.add(8) ;
x.add(7) ;
x.add(6) ;
x.print_queue() ;
}

}

Add a comment
Know the answer?
Add Answer to:
Write code that will display the entire contents of the above Priority Queue.
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
  • 5. Consider a priority queue of strings with an integer priority. The queue us implemented as...

    5. Consider a priority queue of strings with an integer priority. The queue us implemented as a min heap using a 0-based array of size 8 with methods push(entry, priority) and pop(). Determine the priority queue contents, represented both as an array and as a tree, at the points indicated below during the following operations on the ADT. Write down the queue contents after the operation on the given line is executed. 1. priority_queue<string, int> pq; 2. pq.push("start", 10); 3....

  • in C++ please 1- Write the priority queue code – include insertion and remove operations. Given...

    in C++ please 1- Write the priority queue code – include insertion and remove operations. Given the following values – test your program. Use max-heap. 8, 17, 4, 99, 3, 6, 88, 34, 65 2- Sort the numbers: 8, 17, 4, 99, 3, 6, 88, 34, 65 using heap-sort and show the steps - you may write it on a paper and take a picture and include it here. Show all steps. 3- Write the heap-sort code and test the...

  • A priority queue is a collection of items each having a priority. A priority queue supports three...

    A priority queue is a collection of items each having a priority. A priority queue supports three fundamental operations. You can ask a priority queue whether it is empty. You can insert an item into the priority queue with a given priority. You can remove the item from the priority queue that has the smallest priority. For example, suppose that you start with an empty priority queue and imagine performing the following steps. Insert item "one" with priority 10. Insert...

  • Q2 [ 20 pts]. In computer science, a priority queue is an abstract data type which is like a regu...

    Data Structure Java code Q2 [ 20 pts]. In computer science, a priority queue is an abstract data type which is like a regular queue data structure, but where additionally each element has a "priority" associated with it. In a priority queue, an element with high priority is served before an element with low priority. If two elements have the same priority, they are served according to the order in which they were enqueued A typical priority queue supports following...

  • (C++) I need to write a priority queue(where the processes inside are listed as example: P25:2...

    (C++) I need to write a priority queue(where the processes inside are listed as example: P25:2 , 25 being a randomly assigned ID(1-50) and 2 being a randomly assigned priority number(1-10). Once the priority queue is full(10 max), it will transfer the highest priority process to a waiting queue. Once the waiting queue is full(7 max), one process(highest priority) will transfer to a ready queue where the system would perform the process in a round robin style. Once 25 processes...

  • urgent , and what is the big-O? Write a Priority Queue method, end_of_path(path), that is given...

    urgent , and what is the big-O? Write a Priority Queue method, end_of_path(path), that is given a string, path, as input, which represents the path from the root to the node of interest, and if there is a node at the end of that path, the method returns the value at that node, otherwise returns None. path is a string containing just "L"s and "R"s, representing left child and right child. So, given a priority queue represented by the list...

  • In class, we discussed the priority queue (PQ) ADT implemented using min-heap. In a min-heap, the...

    In class, we discussed the priority queue (PQ) ADT implemented using min-heap. In a min-heap, the element of the heap with the smallest key is the root of the binary tree. On the other hand, a max-heap has as root the element with the biggest key, and the relationship between the keys of a node and its parent is reversed of that of a min-heap. We also discussed an array-based implementation of heaps. In this assignment, your task is to...

  • Using C++ write a function that will test if the contents of an STL queue of...

    Using C++ write a function that will test if the contents of an STL queue of characters form a palindrome. A palindrome is a string that reads the same forward and backward (e.g. kayak, madam, dad).

  • Can anyone help me write a simple code for this problem in C++? Generate N>10 random...

    Can anyone help me write a simple code for this problem in C++? Generate N>10 random values from 1 to 100 using a uniform probability distribution. N is entered by the user. The first 5 values are put into nodes and added to a priority queue based on their value. Each node also contains a list of values. After the first 5 are inserted into the priority queue the rest of the values are added to the lists with the...

  • Imagine that the contents of queue Q1 and queue Q2 are as shown. What would be...

    Imagine that the contents of queue Q1 and queue Q2 are as shown. What would be the contents of Q3 after the following code is executed? The queue contents are shown front (left) to rear (right). Q1: 42 30 41 31 19 20 25 14 10 11 12 15 Q2: 4 5 4 10 13 1 Q3 = createQueue 2 count = 0 3 loop (not empty Q1 and not empty Q2)      1 count = count + 1     ...

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