Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

c++ - I want to implement priority queue (insert,delete,find) project with switch case for my assignment please can anyone help me?

here is the code/ also here [pastebin code list][1]: https://pastebin.ubuntu.com/p/h6gM8yP8Q2/

I want to implement a priority queue (insert, delete, find) project with a switch case for my assignment please can anyone help me?

Actually, I am trying to do using queue into switch case

  1. add new entry by gquiz.push()
  2. delte gquiz.pop()
  3. find gquiz.top() //minumum value will show

i already added the code option but maybe gquiz.pop() not work in the switch case. if not then how I will do that?

Please, help to solve the project.

Thank you.

#include<bits/stdc++.h>
#include <iostream>
#include <queue>

using namespace std;

void showpq(
    priority_queue<int, vector<int>, greater<int> > gq)
{
    priority_queue<int, vector<int>,greater<int> > g = gq;
    while (!g.empty()) {
        cout << '' << g.top();
        g.pop();
    }
    cout << '
';
}

int main()
{
    priority_queue<int, vector<int>,greater<int> > gquiz;

    while(1)
    {

    int choice;

    cout<<"what do you want to do?
"
                "
"
                "1. Insert
"
                "2. Find
"
                "3. Delete
"
                "4. Show Queue
 
choice your option from above: ";
    cin>>choice;

    switch(choice)
        {
            case 1:
                int n;
                cout<<"Enter the value: " ;
                cin>>n;// Option 2 => Insert
                gquiz.push(n);
                break;
            case 2:
                gquiz.top();
                if(!gquiz.empty()){
                    gquiz.top(); // Find the minimum number.
                }else{
                    cout<<"Empty Priority Queue"<<endl;
                }
                break;
            case 3:
                if(!gquiz.empty()){
                    gquiz.pop(); //Delete the minimum number from the queue
                    cout<<"Successfully Deleted"<<endl;
                }else{
                    cout<<"There is not element to delete"<<endl;
                }
                break;
            case 4:
                if(!gquiz.empty()){
                    showpq(gquiz); // Show full queue
                }else{
                    cout<<"There is not element to delete"<<endl;
                }
                break;
            default:
                cout<<"
You are terminated!!! 
You entered wrong input.
"<<endl;
        }

    }
    return 0;
}
question from:https://stackoverflow.com/questions/65649058/i-want-to-implement-priority-queue-insert-delete-find-project-with-switch-case

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I solved the program just sharing for otheres. Thank you – Mr. MikeCAT

#include<bits/stdc++.h>
#include <queue>
using namespace std;
void showpq(
    priority_queue<int, vector<int>, greater<int> > gq)
{
    priority_queue<int, vector<int>,greater<int> > g = gq;
    while (!g.empty()) {
        cout << '' << g.top();
        g.pop();
    }
    cout << '
';
}

int main()
{
    priority_queue<int, vector<int>,greater<int> > gquiz;
    while(1)
    {

    int choice;
    cout<<"
what do you want to do?
"
                "
"
                "1. Insert
"
                "2. Find
"
                "3. Delete
"
                "4. Show Queue
 
choice your option from above: ";
    cin>>choice;

    switch(choice)
        {
            case 1:
                int n;
                cout<<"Enter the value: " ;
                cin>>n;// Option 2 => Insert
                gquiz.push(n);
                break;
            case 2:
                if(!gquiz.empty()){
                    cout<<"
"<<gquiz.top()<<" is the minimum number"<<endl; // Find the minimum number.
                }else{
                    cout<<"
Empty Priority Queue"<<endl;
                }
                break;
            case 3:
                if(!gquiz.empty()){
                    gquiz.pop(); //Delete the minimum number from the queue
                    cout<<"
Successfully Deleted"<<endl;
                }else{
                    cout<<"
There is no element to delete"<<endl;
                }
                break;
            case 4:
                if(!gquiz.empty()){
                    showpq(gquiz); // Show full queue
                }else{
                    cout<<"
Empty Priority Queue"<<endl;
                }
                break;
            default:
                cout<<"
You are terminated!!! 
You entered wrong input.
"<<endl;
        }

    }
    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...