Ustuvor navbat (Priority_queue)




Download 1,83 Mb.
bet33/131
Sana13.05.2024
Hajmi1,83 Mb.
#228405
1   ...   29   30   31   32   33   34   35   36   ...   131
Bog'liq
Tiplarni dinamik tarzda

Ustuvor navbat (Priority_queue). Ustuvor navbat (Priority_queue) bilan navbat (queue) - oddiy navbatdek, lekin ustuvor navbatga yangi element qo‘shilasa, shunda navbat kamayish tartibida saralanadi. Shu tariqa ustuvor navbatda eng katta element birinchi o‘ringa chiqadi. Ustuvor navbat shablonidan quyida sintaktik orqali foydalanish kerak:

priority_queue ;

Birinchi priority_queue so‘zini yozishimiz kerak.
Type ga navbatni to‘ldirish uchun kerakli tipni ko‘rsatishimiz lozim. Navbat obʻyektining nomini ko‘rsatishamiz kerak.
Ustivor navbatda yangi element qo‘shish uchun push() funksiyasi, birinchi o‘rindagi elementiga murojaat qilish uchun top() funksiyasi ishlatiladi. Front() – funksiyasi ishlatilmaydi. Shuningdek, back() – oxirgi elementga murojaat funksiyasi ham ishlatilmaydi.
4.7-dastur. Ustuvor navbatni (Priority_queue) yaratish va usullaridan foydalanish.

// Created by MBBahodir #include "stdafx.h" #include #include
using namespace std;

int main() {





priority_queue priority_q, priority_q_one;

for (int h = 0; h < 7; h++) { priority_q.push(rand()%100);


}

priority_q_one = priority_q;

cout << "priority_q navbatdigi birinchi element " << priority_q.top() << endl; cout << " priority_q_one navbatdigi birinchi element " << priority_q_one.top() <<
endl;
priority_q.pop();

cout << "priority_q navbatdigi birinchi element (oʻchirishdan soʻng): " << priority_q.top() << endl;


cout << "priority_q navbat boʻsh " << (priority_q.empty()?"":"emas") << endl;

while (!priority_q_one.empty())
{
cout << "priority_q_one navbatdigi element : " << priority_q_one.top() <<
endl;
priority_q_one.pop();
}
cout << " priority_q_one navbat boʻsh " << (priority_q_one.empty()?"":"emas") << endl;


system("pause"); return 0;
}

Navbatlarni yaratishni ikki usulini kshrib chiqdik. Queue sinf shabloni va massiv asosida. Aslida ko‘proq queue sinf shablonidan foydalangan maqul. Chunki bu massivga nisbattan tez ishlaydi.



Download 1,83 Mb.
1   ...   29   30   31   32   33   34   35   36   ...   131




Download 1,83 Mb.