Question

In: Computer Science

Language C++ Implement a Priority Queue with a Binary HEAP. Use a Max Heap. Create a...

Language C++

Implement a Priority Queue with a Binary HEAP. Use a Max Heap.

Create a class called Node: Have a Name and Priority.Data set - 10 is the highest priority, 1 is lowest priority.

Enqueue and dequeue in the following order.

Function  Name, Priority

Enqueue  Joe, 3

Enqueue  Fred,1

Enqueue Tuyet,9

Enqueue  Jose, 6

Dequeue

Enqueue  Jing, 2

Enqueue  Xi, 5

Enqueue  Moe, 3

DequeueEnqueue  Miko, 7

Enqueue Vlady, 8

Enqueue Frank, 9

Enqueue  Anny, 3

DequeueEnqueue  Xi, 2

Enqueue  Wali, 2

Enqueue  xChe, 6

Enqueue  xVerra, 8

Dequeue

Dequeue

Dequeue

Dequeue

Dequeue

Dequeue

Dequeue

Dequeue

Dequeue

Dequeue

Dequeue

Solutions

Expert Solution

Answer:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
struct node
{
   int priority;
   int content;
   struct node *connect;
};

class Priority_Queue
{
    private:
        node *top;
    public:
        Priority_Queue()
        {
            top = NULL;
        }
        void insert(int value, int priority)
        {
            node *read, *q;
            read = new node;
            read->content = value;
            read->priority = priority;
            if (top == NULL || priority < top->priority)
            {
                read->connect = top;
                top = read;
            }
            else
            {
                q = top;
                while (q->connect != NULL && q->connect->priority <= priority)
                    q=q->connect;
                read->connect = q->connect;
                q->connect = read;
            }
        }
        void del()
        {
            node *read;
            if(top == NULL)
                cout<<"Queue Underflow\n";
            else
            {
                read = top;
                cout<<"Deleted value is: "<<read->content<<endl;
                top = top->connect;
                free(read);
            }
        }
      
        void display()
        {
            node *input;
            input = top;
            if (top == NULL)
                cout<<"Queue is empty\n";
            else
            {   cout<<"Queue is :\n";
                cout<<"Priority       value\n";
                while(input != NULL)
                {
                    cout<<input->priority<<"                 "<<input->content<<endl;
                    input = input->connect;
                }
            }
        }
};

int main()
{
    int option, value, priority;
    Priority_Queue pq;
    do
    {
        cout<<"1.Insert\n";
        cout<<"2.Delete\n";
        cout<<"3.Display\n";
        cout<<"4.Quit\n";
        cout<<"Enter your option : ";
        cin>>option;
        switch(option)
        {
        case 1:
            cout<<"Input the value value to be added in the queue : ";
            cin>>value;
            cout<<"Enter its priority : ";
            cin>>priority;
            pq.insert(value, priority);
            break;
        case 2:
            pq.del();
            break;
        case 3:
            pq.display();
            break;
        case 4:
            break;
        default :
            cout<<"Wrong option\n";
        }
    }
    while(option != 4);
    return 0;
}


Related Solutions

A max-heap can be used as a max-priority queue, which is an abstract data type having...
A max-heap can be used as a max-priority queue, which is an abstract data type having the operations Insert, Maximum, Extract-Max, and Increase-Key. a. Describe how to implement a FIFO queue with a priority queue, and analyze the time complexity of the implementation (Insert and Delete operations), assuming that a heap is used for the priority queue. b. Describe how to implement a LIFO queue (i.e. a stack) with a priority queue, and analyze the time complexity of the implementation...
How to write a max-heap implementation of a templated priority queue class using the STL std::vector...
How to write a max-heap implementation of a templated priority queue class using the STL std::vector class to hold the data array
write C program to implement the priority queue with the operation insert
write C program to implement the priority queue with the operation insert
Building a Priority Queue in C++ using a min-heap. Not using C++ Standard Template Library. Trying...
Building a Priority Queue in C++ using a min-heap. Not using C++ Standard Template Library. Trying to understand how this works. Thank you
C++ Write the code to implement a complete binary heap using an array ( Not a...
C++ Write the code to implement a complete binary heap using an array ( Not a vector ). Code for Max heap. Implement: AddElement, GetMax, HeapSort, ShuffleUp, ShuffleDown, etc Set array size to 31 possible integers. Add 15 elements 1,3,27,22,18,4,11,26,42,19,6,2,15,16,13 Have a default constructor that initializes the array to zeros.. The data in the heap will be double datatype. PART 2 Convert to the program to a template, test with integers, double and char please provide screenshots thank you so...
Write a C program to implement the priority queue with the operations insert and extractmax. Sample...
Write a C program to implement the priority queue with the operations insert and extractmax. Sample : ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 2 enter any key to go to main menu ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 4 enter any key to go to main menu ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 6 enter any key to go to main menu...
Priority Queue Application: Use your Java's Priority Queue. Make a priority queue to represent customers being...
Priority Queue Application: Use your Java's Priority Queue. Make a priority queue to represent customers being served at the Department of Motor Vehicles. Start with 100 customers in a List. In a loop, generate a priority for each customer (1-5) In a second loop, add the users to a priority queue Print the List and the Priority Queue
C++ Program 2–Implement a Priority queue using a SORTED list. Use Quicksort after adding a new...
C++ Program 2–Implement a Priority queue using a SORTED list. Use Quicksort after adding a new node.Example of quick sort below. Adopt to your program. #include <iostream> voidquickSort(inta[], intfirst, intlast); intpivot(inta[], intfirst, intlast); voidswap(int& a, int& b); voidswapNoTemp(int& a, int& b); voidprint(intarray[], constint& N); usingnamespacestd; intmain() { inttest[] = { 7, -13, 1, 3, 10, 5, 2, 4 }; intN = sizeof(test)/sizeof(int); cout << "Size of test array :"<< N << endl; cout << "Before sorting : "<< endl; print(test,...
C++ Program 1–Implement a Priority Queue(PQ) using an UNSORTED LIST. Use an array size of 10...
C++ Program 1–Implement a Priority Queue(PQ) using an UNSORTED LIST. Use an array size of 10 elements. Use a circular array: Next index after last index is 0. Add the new node to next available index in the array. When you add an element, add 1 to index (hit max index, go to index 0). Test if array in full before you add. When you remove an element, from the list, move the following elements to the left to fill...
Implement a priority queue using a DoublyLinkedList where the node with the highest priority (key) is...
Implement a priority queue using a DoublyLinkedList where the node with the highest priority (key) is the right-most node. The remove (de-queue) operation returns the node with the highest priority (key). If displayForward() displays List (first-->last) : 10 30 40 55 remove() would return the node with key 55. Demonstrate by inserting keys at random, displayForward(), call remove then displayForward() again. You will then attach a modified DoublyLinkedList.java (to contain the new priorityInsert(long key) and priorityRemove() methods). Use the provided...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT