Question

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 PRIORITY_QUEUE_H
#define 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 
//   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
//   and the data is the value.  The queue could hold 21,10 and 35, 5 so that the
//   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.
//   data_item is simply a value we are storing in the queue.
//Postcondition: returns SUCCESS if the item was successfully added to the queue
//   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 
//   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 the variable at the address 
//   referred to in pStatus to SUCCESS if there is
//   at least one item in the queue and FAILURE otherwise.  If pStatus 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* pStatus);

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

//Precondition: phQueue is a pointer to the handle of a valid priority queue opaque object.
//Postcondition: The opaque object will be free'd from memory and the handle pointed to
//   by phQueue will be set to NULL.
void priority_queue_destroy(PRIORITY_QUEUE* phQueue);

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

Please let me know if you have any doubts or you want me to modify the answer. And if you find this answer useful then don't forget to rate my answer as thumps up. Thank you! :)

//main.c

#include <stdio.h>
#include <stdlib.h>
#include "priority_queue.h"
#include "priority_queue.c"

int main(int argc, char* argv[])
{
   PRIORITY_QUEUE hQueue = NULL;
   hQueue = priority_queue_init_default();
   if (hQueue == NULL)
   {
       printf("failed to creat Queue\n");
       exit(1);
   }
   priority_queue_insert(hQueue, 21, 10);
   priority_queue_insert(hQueue, 35, 5);
   priority_queue_insert(hQueue, 48, 1);
   priority_queue_insert(hQueue, 50, 3);
   priority_queue_insert(hQueue, 24, 6);

       printf("%d\n", priority_queue_front(hQueue, NULL));

       priority_queue_service(hQueue);
       printf("%d\n", priority_queue_front(hQueue, NULL));
       priority_queue_service(hQueue);
       printf("%d\n", priority_queue_front(hQueue, NULL));
       priority_queue_service(hQueue);
       printf("%d\n", priority_queue_front(hQueue, NULL));
       priority_queue_service(hQueue);
       printf("%d\n", priority_queue_front(hQueue, NULL));
       priority_queue_destroy(&hQueue);
   return 0;
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//prionrity_queue.c
#include <stdio.h>
#include <stdlib.h>
#include "priority_queue.h"

struct pair
{
    int priority;
    int value;
};
typedef struct pair Pair;

struct priority_queue
{
    int size;
    int capacity;
    Pair* data;
    int front;
    int back;
};
typedef struct priority_queue Priority_queue;


PRIORITY_QUEUE priority_queue_init_default(void)
{
    Priority_queue* pPriority_queue;
    pPriority_queue = (Priority_queue*)malloc(sizeof(Priority_queue));
    if (pPriority_queue != NULL)
    {
        pPriority_queue->size = 0;
        pPriority_queue->capacity = 1;
        pPriority_queue->front = 0;
        pPriority_queue->back = 0;
        pPriority_queue->data = (Pair*)malloc(sizeof(Pair)*pPriority_queue->capacity);
        if (pPriority_queue->data == NULL)
        {
            free(pPriority_queue);
            pPriority_queue = NULL;
        }
    }
    return pPriority_queue;
}


Status priority_queue_insert(PRIORITY_QUEUE hQueue, int priority_level, int data_item)
{
    Priority_queue* pPriority_queue = (Priority_queue*)hQueue;
    Pair* temp, tmp;
    //temp = (Pair*)malloc(sizeof());
    int i;
    if (pPriority_queue->size >= pPriority_queue->capacity)
    {
        //not enough space
        temp = (Pair*)malloc(sizeof(Pair) * 2 * pPriority_queue->capacity);
        if (temp == NULL)
        {
            return FAILURE;
        }
        for (i = 0; i < pPriority_queue->size; i++)
        {
            temp[i] = pPriority_queue->data[i];

        }
        pPriority_queue->capacity *= 2;
        free(pPriority_queue->data);
        pPriority_queue->data = temp;
    }
    i = pPriority_queue->size;
    (pPriority_queue->data[i]).priority = priority_level;
    (pPriority_queue->data[i]).value = data_item;
    int index_of_parent;
    index_of_parent = (i - 1) / 2;
    while (i >= 1 && ((pPriority_queue->data[i]).priority > (pPriority_queue->data[index_of_parent]).priority))
    {

        tmp = pPriority_queue->data[index_of_parent];
        pPriority_queue->data[index_of_parent] = pPriority_queue->data[i];
        pPriority_queue->data[i] = tmp;
        i = index_of_parent;
        index_of_parent = (i - 1) / 2;
    }
    pPriority_queue->size++;
    pPriority_queue->front = 0;
    pPriority_queue->back = pPriority_queue->size-1;
    return SUCCESS;
}


Status priority_queue_service(PRIORITY_QUEUE hQueue) {
    Priority_queue* pPriority_queue = (Priority_queue*)hQueue;
    Pair* temp;
    Pair* tmp;
    temp = (Pair*)malloc(sizeof(Pair));
    tmp = (Pair*)malloc(sizeof(Pair));
    if (priority_queue_is_empty(pPriority_queue))
    {
        return FAILURE;
    }


    *temp = pPriority_queue->data[0];
    pPriority_queue->data[0] = pPriority_queue->data[pPriority_queue->back];
    pPriority_queue->data[pPriority_queue->back] = *temp;
    pPriority_queue->size--;
    int i = 0;
    int flag = 1;
    int left_child_index = 2 * i + 1;
    int right_child_index = 2 * i + 2;
    while (left_child_index < pPriority_queue->size && flag==1)
    {
        flag = 0;
        if (right_child_index < pPriority_queue->size && (pPriority_queue->data[right_child_index]).priority>(pPriority_queue->data[left_child_index]).priority)
        {
            if ((pPriority_queue->data[right_child_index]).priority > (pPriority_queue->data[i]).priority)
            {
                *tmp = pPriority_queue->data[i];
                pPriority_queue->data[i] = pPriority_queue->data[right_child_index];
                pPriority_queue->data[right_child_index] = *tmp;
                i = right_child_index;
                left_child_index = 2 * i + 1;
                right_child_index = 2 * i + 2;
                flag = 1;

            }
        }
        else
        {
            if ((pPriority_queue->data[left_child_index]).priority > (pPriority_queue->data[i]).priority)
            {
                *tmp = pPriority_queue->data[i];
                pPriority_queue->data[i] = pPriority_queue->data[left_child_index];
                pPriority_queue->data[left_child_index] = *tmp;
                i = left_child_index;
                left_child_index = 2 * i + 1;
                right_child_index = 2 * i + 2;
                flag = 1;
            }
        }
    }

    pPriority_queue->back = pPriority_queue->size - 1;

    return SUCCESS;
}


int priority_queue_front(PRIORITY_QUEUE hQueue, Status* status)
{
    Priority_queue* pPriority_queue = (Priority_queue*)hQueue;
    if (priority_queue_is_empty(pPriority_queue))
    {
        if (status != NULL)
        {
            *status = FAILURE;
        }
        return 0;
    }
    if (status != NULL)
    {
        *status = SUCCESS;
    }
    return (pPriority_queue->data[pPriority_queue->front]).value;
}


Boolean priority_queue_is_empty(PRIORITY_QUEUE hQueue)
{
    Priority_queue* pPriority_queue = (Priority_queue*)hQueue;
    return (Boolean)(pPriority_queue->size == 0);
}


void priority_queue_destroy(PRIORITY_QUEUE* phQueue)
{
    Priority_queue* pPriority_queue = (Priority_queue*)*phQueue;
    free(pPriority_queue->data);
    free(*phQueue);
    *phQueue = NULL;
    return;
}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//priority_queue.h
#ifndef PRIORITY_QUEUE_H
#define PRIORITY_QUEUE_H

enum status { FAILURE, SUCCESS };
typedef enum status Status;

enum boolean { FALSE, TRUE };
typedef enum boolean Boolean;

typedef void* PRIORITY_QUEUE;


PRIORITY_QUEUE priority_queue_init_default(void);


Status priority_queue_insert(PRIORITY_QUEUE hQueue, int priority_level, int data_item);


Status priority_queue_service(PRIORITY_QUEUE hQueue);

int priority_queue_front(PRIORITY_QUEUE hQueue, Status* status);

Boolean priority_queue_is_empty(PRIORITY_QUEUE hQueue);


void priority_queue_destroy(PRIORITY_QUEUE* phQueue);

#endif
PriorityQueueProgram [~/CLionProjects/PriorityQueueProgram] - .. main. PriorityQueueProgrammain.c PriorityQueueProgram CMakeL

Add a comment
Know the answer?
Add Answer to:
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...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • I need this to be in C Write the implementation file, priority queue.c, for the interface...

    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...

  • 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...

  • 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...

  • 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 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...

  • 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...

  • 1. Here are codes to define a stack class based on dynamic array, please complete the...

    1. Here are codes to define a stack class based on dynamic array, please complete the copy constructor //--- Definition of Stack copy constructor Stack::Stack(const Stack & original) : myCapacity(original.myCapacity), myTop(original.myTop) { //--- Get new array for copy myArray = new(nothrow) StackElement[myCapacity]; if (myArray != 0) // check if memory available                         // copy original's array member into this new array {              // Please complete the function here        } else {          cerr << "*Inadequate memory to allocate...

  • //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...

  • Please write a c++ header file, class implementation file and main file that does all of...

    Please write a c++ header file, class implementation file and main file that does all of the following and meets the requirements listed below. Also include a Output of your code as to show that your program works and functions properly. EXERCISING A DOUBLY-LINKED LIST CLASS This project consists of two parts, the second of which appears below. For the first part, write a class that implements an unordered list abstract data type using a doubly-linked list with pointers to...

  • Can you please write the two C++ modules below is a step by step instuctions to...

    Can you please write the two C++ modules below is a step by step instuctions to follow that will make it very easy thanks. Create a module called pqueue.cpp that implements priority queues, with a header file calledpqueue.hthat describes what pqueue.cpp exports. The interface includes the following, and nothing else. Types ItemType and PriorityType are discussed below under the refinement plan. A type, PriorityQueue. Creating a PriorityQueue object with line PriorityQueue q; makes q be an initially empty priority queue....

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