3#Show the JAVA contents of an initially empty priority queue after each of the
ID: 3759301 • Letter: 3
Question
3#Show the JAVA contents of an initially empty priority queue after each of the following statements execute.
Assume that the first character of the object specifies its priority.
For example, object 1a has priority 1 and object 2b has priority 2. Lower numbers have higher priorities. So 2b has a higher priority than 3d; 2b has the same priority as 2e. Put the front of the priority queue on the left.
pq1.add(1e)
pq1.add(2g)
pq1.add(1b)
pq1.add(2b)
pq1.add(1a)
pq1.remove()
pq1.add(pq1.remove())
pq1.add(pq1.getFront())
pq1.remove()
pq1.remove()
Explanation / Answer
initially queue will be empty
pq1.add(1e)
pq1 -- 1e
pq1.add(2g)
pq1 -- 1e 2g
pq1.add(1b)
pq1 -- 1e 1b 2g
pq1.add(2b)
pq1 -- 1e 1b 2g 2b
pq1.add(1a)
pq1 -- 1e 1b 1a 2g 2b
pq1.remove() - this will remove the top of the queue
pq1 -- 1b 1a 2g 2b
pq1.add(pq1.remove()) - this will remove the element from the top and will again re-insert it. 1a will be removed and will be inserted but will move again because of priority queue.
pq1 -- 1b 1a 2g 2b
pq1.add(pq1.getFront()) - this will get the front element and will insert it again.
pq1 -- 1b 1a 1b 2g 2b
pq1.remove() - this will remove the top element
pq1 -- 1a 1b 2g 2b
pq1.remove() - this will again remove the top element
pq1 -- 1b 2g 2b
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.