I need this to be in C Write the implementation file, priority queue.c, for the
ID: 3732716 • Letter: I
Question
I need this to be in C
Write the implementation file, priority queue.c, for the interface in the given header file, priority queue.h. Turn in your priority queue.c file and a suitable main program, main.c, that tests the opaque object. priority queue.h is attached as a file to this assignment but is also listed here for your convenience. Your implementation file should implement the priority queue using a heap data structure. Submissions that implement the priority queue without using a heap will not receive any credit #ifndef #define PRIORITY-QUEUE, H PRIORITY-QUEUE-H enum status FAILURE, SUCCESS}; typedef enum status Status; enum boolean FALSE, TRUE ; typedef enum boolean Boolean typedef void* PRIORITY QUEUE; //Precondition: Creates an empty priority queue that can store integer data items /I with different integer priority. Higher // integer values indicate higher priority in the queue. For example, consider the // priority and the data value to be key-value pairs where the priority is the key /I and the data is the value. The queue could hold 21,10 and 35, 5 so that the /I first item to be removed from the queue would be the data value 5 because // it has higher priority (35) than the data value 10 which only has (21) //Postcondition: Returns the handle to an empty priority queue PRIORITY QUEUE priority_queue_init_default(void); //Precondition: hQueue is a handle to a valid priority queue opaque object. // Higher priority_level values indicate higher priority in the queue. /I data_item is simply a value we are storing in the queue //Postcondition: returns SUCCESS if the item was successfully added to the queue /I and FAILURE otherwise Status priority_queue_insert (PRIORITY QUEUE hQueue, int priority_level, int data item); //Precondition: hQueue is a handle to a valid priority queue opaque object. //Postcondition: returns SUCCESS if the highest priority item was removed from the queue /I and FAILURE if the queue was empty. Status priority_queue_service(PRIORITY QUEUE hQueue); //Precondition: hQueue is a handle to a valid priority queue opaque object. //Postcondition: returns a copy of the data value for the // highest priority item in the queue. Sets status to SUCCESS if there is // at least one item in the queue and FAILURE otherwise. If status is // passed in as NULL then the status value is ignored for this run of the function int priority_queue_front (PRIORITY QUEUE hQueue, Status& status); //Precondition: hQueue is a handle to a valid priority queue opaque object //Postcondition: returns TRUE if the priority_queue is empty and FALSE otherwise Boolean priority_queue_is_empty (PRIORITY QUEUE hQueue);Explanation / Answer
Priority Queue with heap
//Priority Queue with heap:
#include<stdio.h>
#include<malloc.h>
void insert();
void del();
void display();
struct node
{
int priority;
int info;
struct node *next;
}*start=NULL,*q,*temp,*new;
typedef struct node N;
int main()
{
int ch;
clrscr();
do
{
printf(" [1] INSERTION [2] DELETION [3] DISPLAY [4] EXIT :");
scanf("%d",&ch);
switch(ch)
{
case 1:insert();
break;
case 2:del();
break;
case 3:display();
break;
case 4:
break;
}
}
while(ch<4);
}
void insert()
{
int item,itprio;
new=(N*)malloc(sizeof(N));
printf("ENTER THE ELT.TO BE INSERTED : ");
scanf("%d",&item);
printf("ENTER ITS PRIORITY : ");
scanf("%d",&itprio);
new->info=item;
new->priority=itprio;
new->next=NULL;
if(start==NULL )
{
//new->next=start;
start=new;
}
else if(start!=NULL&&itprio<=start->priority)
{ new->next=start;
start=new;
}
else
{
q=start;
while(q->next != NULL && q->next->priority<=itprio)
{q=q->next;}
new->next=q->next;
q->next=new;
}
}
void del()
{
if(start==NULL)
{
printf(" QUEUE UNDERFLOW ");
}
else
{
new=start;
printf(" DELETED ITEM IS %d ",new->info);
start=start->next;
//free(start);
}
}
void display()
{
temp=start;
if(start==NULL)
printf("QUEUE IS EMPTY ");
else
{
printf("QUEUE IS: ");
if(temp!=NULL)
for(temp=start;temp!=NULL;temp=temp->next)
{
printf(" %d priority =%d ",temp->info,temp->priority);
//temp=temp->next;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.