Question

Language: C++ (Please include simple explanation & Screenshot of the output) Implement a Prio...

Language: C++ (Please include simple explanation & Screenshot of the output)

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

Dequeue

Enqueue Miko, 7

Enqueue Vlady, 8

Enqueue Frank, 9

Enqueue Anny, 3

Dequeue

Enqueue Xi, 2

Enqueue Wali, 2

Enqueue xChe, 6

Enqueue xVerra, 8

Dequeue

Dequeue

Dequeue

Dequeue

Dequeue

Dequeue

Dequeue

Dequeue

Dequeue

Dequeue

Dequeue

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

#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;
}

1 . Insert 2.Delete 3.Display 4.Quit Enter your option 1 Input the value value to be added in the queue : 4 Enter its priorit

Add a comment
Know the answer?
Add Answer to:
Language: C++ (Please include simple explanation & Screenshot of the output) Implement a Prio...
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
  • Language C++ (Please include a short description & Screenshot of output) Implement a Priority...

    Language C++ (Please include a short description & Screenshot of output) Implement a Priority queue using a SORTED list. Use Quick sort after adding a new node. Example of quick sort below. Adopt to your program the code below. #include <iostream> void quickSort(int a[ ], int first, int last); int pivot(int a[], int first, int last); void swap(int& a, int& b); void swapNoTemp(int& a, int& b); void print(int array[], const int& N); using namespace std; int main() { int test[]...

  • (Data Strcture) Tool(s)/Software Java programming language with NetBeans IDE. Description Implementing a Linear Queue using a...

    (Data Strcture) Tool(s)/Software Java programming language with NetBeans IDE. Description Implementing a Linear Queue using a Singly Linked-List and Implementing a Priority Queue Tasks/Assignments(s) ■ Write your own program to implement priority queue using the Linked-List and implement the Enqueue, Dequeue and Display methods. implement the Merge methods in your main program that receive Q1 and Q2 and merge the two queues into one queue. Public static PriorityQueue MergeQueue(PriorityQueue Q1,PriorityQueue Q2) Write main program to test priority queue class and...

  • Using C++, data structures, C++ STL, inputs and expected outputs are shown below. Max Heap Heap...

    Using C++, data structures, C++ STL, inputs and expected outputs are shown below. Max Heap Heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either > (in a max heap) or s (in a min heap) the key of C. The node at the "top" of the heap (with no parents) is called the root node. In binary-tree based heap, it...

  • C language huffman This exercise will familiarize you with linked lists, which you will need for...

    C language huffman This exercise will familiarize you with linked lists, which you will need for a subsequent programming Getting Started assignment Overview Requirements Getting Started Submit Start by getting the files. Type 264get hw13 and then cd hw13 from bash. Pre-tester You will get the following files: Q&A Updates 1. huffman.h: An empty header file, you have to define your own functions in this homework. 2. huffman.c: An empty c file, you have to define your own functions in...

  • I've posted 3 classes after the instruction that were given at start You will implement and...

    I've posted 3 classes after the instruction that were given at start You will implement and test a PriorityQueue class, where the items of the priority queue are stored on a linked list. The material from Ch1 ~ 8 of the textbook can help you tremendously. You can get a lot of good information about implementing this assignment from chapter 8. There are couple notes about this assignment. 1. Using structure Node with a pointer point to Node structure to...

  • ***** running late, mere 3 hours left for due time, please help ** #needed in c++...

    ***** running late, mere 3 hours left for due time, please help ** #needed in c++ #inputfile MinPath2.txt 0   SF 1   LA 2   DALLAS 3   CONCORD 4   PHOENIX 5   CHICAGO 6   ST LOUIS 7   BOSTON 8   NY 9   LONDON 10   PARIS 11   TOKYO 12   BANGKOK 13   MEXICO CITY 14   MONTREAL -1   0   1   40 0   2   100 0   4   130 0   8   200 0   9   800 0   10   900 1   2   50 1   3   80 1   4   70 1   8  ...

  • Use the C programming language to complete #include <stdio.h> #include <stdlib.h> #include <float.h> // Declare Global...

    Use the C programming language to complete #include <stdio.h> #include <stdlib.h> #include <float.h> // Declare Global variables here. void array_stats() { // Insert your solution here. } #include <stdlib.h> #include <time.h> int main() { // Simulate the test setup process. srand( time( NULL ) ); for ( int i = 0; i < 32; i++ ) { val[i] = rand(); } val_count = rand(); val_mean = rand(); val_min = rand(); val_max = rand(); // Call submitted code. array_stats(); // Display...

  • Hi, can you help me with Part E? Please use Java language. So for this Part,...

    Hi, can you help me with Part E? Please use Java language. So for this Part, you will be given 3 files of starter code that is already done for you. All you have to do is to add on to it in order to produce the exact output shown in the pictures below. Please only add on to the code, but not change any of them. Out of the 3 given files, 1 of them is already completed. The...

  • The Code need to be in C# programming language. could you please leave some explanation notes...

    The Code need to be in C# programming language. could you please leave some explanation notes as much as you can? Thank you Create the following classes in the class library with following attributes: 1. Customer a. id number – customer’s id number b. name –the name of the customer c. address – address of the customer (use structs) d. telephone number – 10-digit phone number e. orders – collection (array) of orders Assume that there will be no more...

  • Can you please help with the below? 1)   Which of the following is true about using...

    Can you please help with the below? 1)   Which of the following is true about using a 2-3-4 tree? a.   It is designed to minimize node visits while keeping to an O(log n) search performance b.   It is designed to self-balance as new values are inserted into the tree c.   As soon as a node becomes full, it performs the split routine d.   None of the above 2)   Which of the following is true about a binary search tree? a.  ...

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