Search code examples
c++priority-queue

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 << '\t' << g.top();
        g.pop();
    }
    cout << '\n';
}

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

    while(1)
    {

    int choice;

    cout<<"what do you want to do?\n"
                "\n"
                "1. Insert\n"
                "2. Find\n"
                "3. Delete\n"
                "4. Show Queue\n \nchoice 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<<"\nYou are terminated!!! \nYou entered wrong input.\n"<<endl;
        }

    }
    return 0;
}

Solution

  • 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 << '\t' << g.top();
            g.pop();
        }
        cout << '\n';
    }
    
    int main()
    {
        priority_queue<int, vector<int>,greater<int> > gquiz;
        while(1)
        {
    
        int choice;
        cout<<"\nwhat do you want to do?\n"
                    "\n"
                    "1. Insert\n"
                    "2. Find\n"
                    "3. Delete\n"
                    "4. Show Queue\n \nchoice 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<<"\n"<<gquiz.top()<<" is the minimum number"<<endl; // Find the minimum number.
                    }else{
                        cout<<"\nEmpty Priority Queue"<<endl;
                    }
                    break;
                case 3:
                    if(!gquiz.empty()){
                        gquiz.pop(); //Delete the minimum number from the queue
                        cout<<"\nSuccessfully Deleted"<<endl;
                    }else{
                        cout<<"\nThere is no element to delete"<<endl;
                    }
                    break;
                case 4:
                    if(!gquiz.empty()){
                        showpq(gquiz); // Show full queue
                    }else{
                        cout<<"\nEmpty Priority Queue"<<endl;
                    }
                    break;
                default:
                    cout<<"\nYou are terminated!!! \nYou entered wrong input.\n"<<endl;
            }
    
        }
        return 0;
    }