কম্পিউটার টিউটোরিয়াল

সি++-এ প্রায়োরিটি কিউ (Priority Queue)


প্রায়োরিটি কিউ হলো কিউর একটি ভেরিয়েশন যেখানে প্রতিটি এলিমেন্টের একটি প্রায়োরিটি থাকে। সর্বোচ্চ প্রায়োরিটি ওয়ালা এলিমেন্ট প্রথমে ডিকিউ হয়। সি++ STL-এ `priority_queue` ক্লাস দিয়ে এটি বাস্তবায়ন করা যায়। ডিফল্টলি বড় মান হাই প্রায়োরিটি হিসেবে বিবেচিত হয়।

এলগরিদম

insert(key, priority): হিপে ইনসার্ট করুন, হিপিফাই করুন
delete(): রুট এলিমেন্ট রিমুভ করুন, লাস্ট এলিমেন্ট রুটে আনুন, হিপিফাই করুন

উদাহরণ কোড (STL ব্যবহার)

#include <iostream> 
#include <queue> 
using namespace std; 

void printQueue(priority_queue q){
    while(!q.empty()){
        cout << q.top() << " ";
        q.pop();
    }
    cout << endl;
}

int main(){
    priority_queue pq;
    pq.push(10); pq.push(20); pq.push(30); pq.push(5); pq.push(1);
    
    cout << "Queue: "; printQueue(pq);
    cout << "Size: " << pq.size() << endl;
    cout << "Top: " << pq.top() << endl;
    
    cout << "After pop: "; pq.pop(); printQueue(pq);
    cout << "After pop: "; pq.pop(); printQueue(pq);
    return 0;
}

আউটপুট

Currently que is holding: 30 20 10 5 1
Size of queue: 5
Element at top: 30
Delete from queue: 20 10 5 1
Delete from queue: 10 5 1

  1. C/C++-তে প্রায়োরিটি কিউ (Priority Queue) পরিচিতি

  2. C++ STL-এ প্রায়োরিটি কিউ (Priority Queue)

  3. ডাবলি লিংকড লিস্ট দিয়ে প্রায়োরিটি কিউ বাস্তবায়ন C++-তে

  4. IPC: মেসেজ কিউ (Message Queues) ব্যবহার করে ইন্টার-প্রসেস কমিউনিকেশন