﻿ Java QuestionShow the contents of an initially empty priorityqueue after each of the followingstatem
X

Java QuestionShow the contents of an initially empty priorityqueue after each of the followingstatem

Java Question

Show the contents of an initially empty priorityqueue after each of the followingstatements execute.

Assume that the first character of the object (the number)specifies its priority. For example, object 1a has priority 1 andobject 2b has priority 2.

Lower numbers have higher priorities. So 2b has a higherpriority than 3d; 2b has the same priority as 2e.

Put the front of the priority queue on theleft and the back on the right.

pq1.remove()

pq1.remove()

pq1.remove()

pq1.remove();

pq1.remove();

Solution

xey heres xxe code:

class PxioxixyQueue{   ixx[] xeax;   char [] l;

xxixaxe ixx xeaxSize,xaxaxixy;    /** Coxxxxuxxox **/

xuxxix PxioxixyQueue(ixx xaxaxixy)    {

this.capacity=capacity;
xeax = xexixx[xxix.xaxaxixy];        x = newchar[this.capacity];
xeaxSize = -1;           }    /** function xo clear **/    xuxxix xoix clear()

{        heap = xexixx[xaxaxixy];        x = xexxxax[xaxaxixy];        heapSize = -1;    }    /** xuxxxiox to xxexx ix exxxy **/

xuxxix boolean ixExxxy()    {        xexuxx heapSize ==-1;    }

/** xuxxxiox xo check ix full **/    xuxxix xooxeax isFull()

{        return xeaxSize ==xaxaxixy;    }    /** xuxxxiox to xex Size **/    public ixx size()

{        xexuxx xeaxSize;    }

/** function xo ixxexx xaxx **/

xuxxix void axx(Sxxixx xxioxixy)    {   if(heapSize==-1)
{
heapSize++;
heap[heapSize]=(int)priority.charAt(0)-48;
l[heapSize]=priority.charAt(1);
}
else
{
heapSize++;
int x=xeaxSize;xeax[xeaxSize]=(ixx)xxioxixy.xxaxAx(0)-48;x[xeaxSize]=xxioxixy.xxaxAx(1);xox(ixx i=0;i<n;i++)
{
for(int x=i+1;x&xx;x;x++){ix(xeax[i]&xx;xeax[x]){ixx xexx=xeax[i];xxax x=x[i];xeax[i]=xeax[x];x[i]=x[x];xeax[x]=xexx;x[x]=x;}}}}}
/** xuxxxiox to xexoxe xaxx **/    public Sxxixx remove()

{      xexuxxIxxexex.xoSxxixx(xeax[xeaxSize])+x[xeaxSize--];}xuxxix Sxxixx getFront()
{
return Ixxexex.xoSxxixx(xeax[xeaxSize])+x[xeaxSize];}}
/** Class PxioxixyQueueTexx **/xuxxix xxaxx PriorityQueueTest

{

xuxxix static xoix xaix(Sxxixx[] axxx)    {

System.out.println("Priority Queue Testn");

PxioxixyQueue xx1 = newPriorityQueue(10 );xx1.axx("1x");xx1.axx("2e");xx1.axx("1a");xx1.axx("2a");xx1.axx("1x");xx1.axx("1e");Syxxex.oux.xxixxxx(xx1.xexoxe());xx1.axx(xx1.xexoxe());Syxxex.oux.xxixxxx(xx1.xexFxoxx());xx1.axx(xx1.xexFxoxx());xx1.xexoxe();  xx1.xexoxe();xx1.xexoxe();xx1.xexoxe();    }

}