Question

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);

I need this to be in C

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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("\n[1] INSERTION\t[2] DELETION\t[3] DISPLAY [4] EXIT\t:");
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 :\t");
scanf("%d",&item);
printf("ENTER ITS PRIORITY :\t");
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("\nQUEUE UNDERFLOW\n");

}
else
{
new=start;
printf("\nDELETED ITEM IS %d\n",new->info);
start=start->next;
//free(start);
}
}

void display()
{
temp=start;
if(start==NULL)
printf("QUEUE IS EMPTY\n");
else
{
printf("QUEUE IS:\n");
if(temp!=NULL)
for(temp=start;temp!=NULL;temp=temp->next)
{
printf("\n%d priority =%d\n",temp->info,temp->priority);
//temp=temp->next;
}
}
}

Add a comment
Know the answer?
Add Answer to:
I need this to be in C Write the implementation file, priority queue.c, for the interface...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Needed in C please 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...

    Needed in C please 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...

  • Using the below files, Write a program that reads a document containing endnotes indicated in this...

    Using the below files, Write a program that reads a document containing endnotes indicated in this manner, collects them in a queue, and prints them on the screen. For this lab, you will create a text file called sample.txt and put the following paragraph in it. This part is the beginning. {This part is the footnote.} This part is the end. /* Queue.h contains the declaration of class Queue. Basic operations: Constructor: Constructs an empty queue empty: Checks if a...

  • Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation...

    Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation of the Table ADT. Make sure that you apply the concepts of design by contract (DbC) to your implementation. Once you have fully implemented the table, create a main.c file that implements a testing framework for your table. Your table implementation must ensure that values inserted are unique, and internally sorted within a linked list. table.h #ifndef _TABLE_H #define _TABLE_H //----------------------------------------------------------------------------- // CONSTANTS AND...

  • In a file named LLBag.java, write a class called LLBag that implements the Bag interface using...

    In a file named LLBag.java, write a class called LLBag that implements the Bag interface using a linked list instead of an array. You may use a linked list with or without a dummy head node. Bag interface code: /* * Bag.java * * Computer Science 112, Boston University */ /* * An interface for a Bag ADT. */ public interface Bag { /*    * adds the specified item to the Bag. Returns true on success    * and...

  • The purpose of this program is to help reinforce container class concepts and linked list concepts...

    The purpose of this program is to help reinforce container class concepts and linked list concepts in C++. Specifically, the task is to implement the sequence class using a linked list. You need to use the author's file sequence3.h to create your implementation file named sequence3.cpp. Please make your code as efficient and reusable as possible. Please make sure code can pass any tests. sequence3.h // FILE: sequence3.h // CLASS PROVIDED: sequence (part of the namespace main_savitch_5) // This is...

  • The purpose of this lab is to help reinforce container class concepts and linked list concepts...

    The purpose of this lab is to help reinforce container class concepts and linked list concepts in C++. Specifically, the lab to repeat the sequence lab from last week except to use a linked list. You need to use the author's files sequence3.h and sequence_exam3.cpp. Author - Michael Main, Book - Data Structures and other objects using c++, 4th edition // FILE: sequence3.h // CLASS PROVIDED: sequence (part of the namespace main_savitch_5) // This is the header file for the...

  • Write a C++ program to validate computer user-ids and passwords. A list of valid ids and...

    Write a C++ program to validate computer user-ids and passwords. A list of valid ids and passwords (unsorted) is read from a file and stored in a Binary Search Tree (BST) of UserInfo objects. When user-ids and passwords are entered during execution, this BST is searched to determine whether they are legal. Input (file): UserInfo records for valid users Input (keyboard): Ids and passwords of users logging in Output (screen): Messages indicating whether user-ids and passwords are valid, as well...

  • The object manager's interface is given by the file ObjectManager.h and the implementation will be in...

    The object manager's interface is given by the file ObjectManager.h and the implementation will be in the file ObjectManager.c. Your task is to implement the functions required for the object manager. This includes all the functions listed in ObjectManager.h. #ifndef _OBJECT_MANAGER_H #define _OBJECT_MANAGER_H #ifndef MEMORY_SIZE #define MEMORY_SIZE 1024*512 #endif #define NULL_REF 0 typedef unsigned long Ref; // This function trys to allocate a block of given size from our buffer. // It will fire the garbage collector as required. //...

  • //Look for** to complete this program --C+ please --please add comments // using namespace std; #include...

    //Look for** to complete this program --C+ please --please add comments // using namespace std; #include <iostream> #include <stdlib.h> #include < string> #include "queue.h" //Purpose of the program: ** Algorithm: * int main() /** "A", "B", "C" in the queue //** while loop -- indefinitely { try {//** catches } //* end of loop } |/ II II II II II //File type: ** queue.cpp using namespace std; #include <iostream> #include "queue.h" // constructor queue::queue () } //destructor queue::~queue() queue::queue...

  • Complete an Array-Based implementation of the ADT List including a main method and show that the...

    Complete an Array-Based implementation of the ADT List including a main method and show that the ADT List works. Draw a class diagram of the ADT List __________________________________________ public interface IntegerListInterface{ public boolean isEmpty(); //Determines whether a list is empty. //Precondition: None. //Postcondition: Returns true if the list is empty, //otherwise returns false. //Throws: None. public int size(); // Determines the length of a list. // Precondition: None. // Postcondition: Returns the number of items in this IntegerList. //Throws: None....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT