Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A priority queue is a queue where a numeric priority is associated with each ele

ID: 3783832 • Letter: A

Question

A priority queue is a queue where a numeric priority is associated with each element. Access to elements that have been inserted into the queue is limited to inspection and removal of the elements with smallest and largest priority only. A priority queue may have multiple items that are of equal priority.

By “bounded”, it is meant that the priority queue has a maximum capacity specified when it is created, and it can never contain more than that number of items.

Your specification must specify the following operations:

newPriorityQueue: make a new queue
insert: inserts an element with a certain priority
isEmpty: test if the queue is empty
isFull: test if the queue is full
maxItem: obtain the item in the queue with the highest priority
minItem: obtain the item in the queue with the lowest priority
deleteMax: remove from the queue the item with the highest priority
deleteAllMax: remove from the queue all items that are tied for the highest priority deleteMin: remove from the queue the item with the lowest priority
frequency: obtain the number of times a certain item occurs in the queue (with any priority)

Explanation / Answer

#include<stdio.h>
#include<math.h>
#define MAX 100
void swapelements(int*,int*);
main()
{
int yourchoice,number,m,b[MAX],d,t;
void displayvalue(int[],int);
void insertvalue(int[],int,int,int);
int del(int[],int,int);
m=0;
int lowerb=0;
while(1)
{
printf(".....MAIN MENU.....n");
printf("n1.Insertvalue.m");
printf("2.Deletevalue.m");
printf("3.Displayvalue.m");
printf("4.Quit.n");
printf("nEnter your choice : ");
scanf("%d",&yourchoice);
switch(yourchoice)
{
case 1:
printf("Enter data to be inserted : ");
scanf("%d",&d);
insert(b,m,d,lowerb);
m++;
break;
case 2:
t=del(b,m+1,lowerb);
if(t!=0)
printf("nThe deleted value is : %d n",t);
if(m>0)
m--;
break;
case 3:
printf("m");
display(b,m);
break;
case 4:
return;
default:
printf("Invalid choice.m");
}
printf("mm");
}
}
void insert(int b[],int heapsize,int d,int lowerb)
{
int j,q;
int parent(int);
if(heapsize==MAX)
{
printf("Queue Is Full!!m");
return;
}
j=lowerb+heapsize;
b[j]=d;
while(j>lowerb&&b[q=parent(j)]<b[j])
{
swap(&b[q],&b[j]);
j=q;
}
}
int delh(int b[],int heapsize,int lowerb)
{
int d,j,m,o,maxc,u;
int leftone(int);
int rightone(int);
if(heapsize==1)
{
printf("Queue Is Empty!!m");
return 0;
}
u=b[lowerb];
swapelements(&b[lowerb],&b[heapsize-1]);
j=lowerb;
heapsize--;
while(1)
{
if((m=leftone(j))>=heapsize)
break;
if((o=rightone(j))>=heapsize)
maxc=m;
else
maxc=(b[m]>c[o])?n:o;
if(b[j]>=b[maxc])
break;
swapelements(&b[j],&b[maxc]);
j=maxc;
}
return u;
}

int parent(int j)
{
float q;
q=((float)j/2.0)-1.0;
return ceil(q);
}

int leftone(int j)
{
return 2*j+1;
}

int rightone(int j)
{
return 2*j+2;
}

void display(int b[],int m)
{
int j;
if(m==0)
{
printf("Queue Is Empty!!m");
return;
}
for(j=0;j<m;j++)
printf("%d ",b[j]);
printf("m");
}

void swapelements(int*q,int*r)
{
int temp;
temp=*q;
*q=*r;
*r=temp;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote