/* related posts with thumb nails */

Program to implement insert and delete operations on Priority Queue?:

class PriorityQ

{

private int a[]=new int[10];

private int n;

PriorityQ()

{

n = 0;

}

public void insert(int v)

{

int i;

if (n==0)

a[n++]=v;

else

{

for (i=n-1;i>=0;i--)

{

if (v>a[i]) // if new v is larger,

a[i+1] = a[i]; // shift upward

else

break;

}

a[i+1] = v;

n++;

}

}

public int delete()

{

return a[--n];

}

public void show()

{

System.out.println("\n Priority Queue Elements:");

for(int i=0;i

System.out.print("\t"+a[i]);

}

}

class PQueueTest

{

public static void main(String[] args)

{

PriorityQ p1 = new PriorityQ();

p1.insert(30);

p1.insert(20);

p1.insert(50);

p1.insert(10);

p1.show();

p1.delete();

System.out.println("\n After Deleting:");

p1.show();

}

}

/* Output */

Priority Queue Elements:

50 30 20 10

After Deleting:

Priority Queue Elements:

50 30 20

Related Topics:

0 comments:

Post a Comment