C++中优先级队列的完整指南

368 阅读3分钟

Priority Queue in C++ Example | C++ Priority Queue Program

队列是一种基于FIFO(先进先出)的数据结构,也就是说,插入放在后面,删除放在前面。优先级队列 是一种特殊类型的队列。优先队列并不像队列那样遵循先进先出的概念。

C++中的优先级队列

C++中的优先级队列是作为容器适配器实现的,它使用特定容器类的封装对象作为其底层容器。优先级队列中的每个元素都与一些优先级相关。一个元素的删除是根据优先级来进行的。

第一个被从队列中删除的项目是具有最高优先级的元素。具有相同优先级的项目根据FIFO进行排序。 这些都包含在C++库的STL库中。这种排序方式是将最高的元素放在前面,其他元素按递减的顺序排序。这些有一组特定的成员函数来访问它们的元素。

优先级队列的基本操作

insert (item, priority)

它 在优先级队列中插入一个具有其优先级的项目。

getHighestPriority()

返回一个 在队列中具有最高优先级的项目。

delete ()

删除 具有最高优先级的项目,并给出删除的项目。

isEmpty()

它检查队列是否为空。

isFull()

它检查队列是否已满。

优先级队列的优点

节点被赋予了权重,这使得它们可以向队列的头部移动,而不是像普通队列中那样处于队列的尾部。

优先级队列的劣势

现在插入需要的时间不像队列那样恒定,因为我们还必须应用插入排序来根据元素的优先级插入。

通过链表实现有助于实现插入的恒定时间。

算法或伪代码

Insert (item, priority)
First element to insert: Insert (10,1)
Second element to insert:Insert (30,3)
Third element to insert: Insert (20,2)
Fourth element to insert:Insert (40,4)
Queue after insertion: 40 30 20 10
Delete ()
First deletion: 40
Queue after deletion: 30 20 10
Insert (50,4)
Insert (60,4)
Queue after insertion :50 60 30 20 10

这里我们可以看到,元素是根据它们的优先级插入的。

最高优先级的元素在队列的前面。因此,在删除时,第一个队列元素被从队列中删除。之后,具有相同优先级的项目会根据先进先出的原则进行排序。

C++中的优先级队列程序

例1: 使用STL方法制作一个队列,并执行基本功能。

#include <iostream>
#include <conio.h>
#include <cstdio>
#include <cstring>
#include <cstdlib>

using namespace std;

struct n // node declaration
{
  int priority;
  int info;
  struct n *next;
};
class Priority_Queue
{
private:
  n *f;

public:
  Priority_Queue()
  {

    f = NULL;
  }
  void insert(int i, int p)
  {
    n *t, *q;
    t = new n;
    t->info = i;
    t->priority = p;
    if (f == NULL || p < f->priority)
    {
      t->next = f;
      f = t;
    }
    else
    {
      q = f;
      while (q->next != NULL && q->next->priority <= p)
        q = q->next;
      t->next = q->next;
      q->next = t;
    }
  }
  void delet()
  {
    n *t;
    if (f == NULL) //if queue is null
      cout << "Queue Underflow\n";
    else
    {
      t = f;
      cout << "Deleted item is: " << t->info << endl;
      f = f->next;
      free(t);
    }
  }
  void show() //print queue {
  {
    n *ptr;
    ptr = f;
    if (f == NULL)
      cout << "Queue is empty\n";
    else
    {
      cout << "Queue is :\n";
      cout << "Priority Item\n";
      while (ptr != NULL)
      {
        cout << ptr->priority << " " << ptr->info << endl;
        ptr = ptr->next;
      }
    }
  }
};
int main()
{
  int c, i, p;
  Priority_Queue pq;
  do
  {
    cout << "1.Insert\n";
    cout << "2.Delete\n";
    cout << "3.Display\n";
    cout << "4.Exit\n";
    cout << "Enter your choice : ";
    cin >> c;
    switch (c)
    {
    case 1:
      cout << "Input the item value to be added in the queue : ";
      cin >> i;
      cout << "Enter its priority : ";
      cin >> p;
      pq.insert(i, p);
      break;
    case 2:
      pq.delet();
      break;
    case 3:
      pq.show();
      break;
    case 4:
      break;
    default:
      cout << "Wrong choice\n";
    }
  } while (c != 4);

  return 0;
}

请看输出。

Priority Queue in C++

Priority Queue in C++ Example

例2: 使用链接列表制作一个队列并执行基本功能。

#include <iostream>
#include <queue>

using namespace std;

void displaypq(priority_queue<int> pq)
{
  priority_queue<int> pqueue = pq;
  while (!pqueue.empty())
  {
    cout << '\t' << pqueue.top();
    pqueue.pop();
  }
  cout << '\n';
}

int main()
{
  priority_queue<int> pq;
  pq.push(1);
  pq.push(3);
  pq.push(5);
  pq.push(7);
  pq.push(9);

  cout << "Size of the queue(pq.size()): " << pq.size();
  cout << "\nTop element of the queue(pq.top()): " << pq.top();
  cout << "\nThe priority queue pq is : ";
  displaypq(pq);

  cout << "\nPriority queue, after pq.pop() operation : ";
  pq.pop();
  displaypq(pq);

  return 0;
}

请看输出。

C++ Priority Queue Program

总结

优先级队列是明确设计的容器适配器,因此队列中的第一个元素是队列中所有项目中最大的,而且元素的顺序是非递增的。

本教程就到此为止。