Answers for "priority queue with comparator c++"

C++
1

priority queue comparator java

Problem Statement

A shopping mall giving gift to highest purchased customer 
as Independence day offer.
Data stored is order_id, amount and name of customer.
Implement this program using PriorityQueue 
to display Details of customer who won the gift.

Example
Enter customer id, amount and name who purchased in mall(0 to stop):
1 275 ram
2 500 sham
3 350 monika
0
Details of customer who won gift on highest purchase:
orderId:2, orderAmount:500.0, customerName:sham

//write code here
import java.util.*;
class Priority_Queue{
    int id;
    int amount;
    String name;
    Priority_Queue(int i,int a,String n){
        id=i;
        amount=a;
        name=n;
    }
    public String toString(){
        return "orderId:"+id+", orderAmount:"+amount+".0, customerName:"+name;
    }
}

class sort_amount implements Comparator<Priority_Queue>{
    @Override
    public int compare(Priority_Queue o1, Priority_Queue o2){
        return o1.amount-o2.amount;
    }
}
public class Test{
    public static void main(String[] args){
        Scanner scan=new Scanner(System.in);
        ArrayList<Priority_Queue> pq=new ArrayList<>();
        System.out.println("Enter customer id, amount and name who purchased in mall(0 to stop):");
        for(int i=0;i<10;i++){
            int id=scan.nextInt();
            if(id==0){
                break;
            }
            int amount=scan.nextInt();
            String name=scan.next();
            pq.add(new Priority_Queue(id, amount, name));
        }
        Collections.sort(pq, new sort_amount());
        System.out.println("Details of customer who won gift on highest purchase:");
        System.out.println(pq.get(pq.size()-1));
    }
}
Posted by: Guest on August-23-2021
0

how to use priority queue comparator stl c++

void SamplePriorityQueueWithLamda()
{
    // using lambda to compare elements.
    auto compare = [](int lhs, int rhs)
                {
                    return lhs < rhs;
                };

    std::priority_queue<int, std::vector<int>, decltype(compare)> q(compare);

    for(int n : {1,8,5,6,3,4,0,9,7,2})
        q.push(n);


    printQueue(q);
}
Posted by: Guest on April-11-2021

Code answers related to "priority queue with comparator c++"

Browse Popular Code Answers by Language